Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better internal configuration handling #84

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 21 additions & 40 deletions server/src/testrunners.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,34 @@
import merge from 'lodash.merge';
const testRunners = {};

const testRunners = [];
const testRunnersById = [];

export function addTestRunner(config) {
const index = testRunners.findIndex(
testRunner => testRunner.name == config.name
);
testRunnersById.push(config);
if (index === -1) {
testRunners.push(config);
function mergeByHostname(target, source) {
if (target[source.hostname]) {
target[source.hostname].setup = [
...target[source.hostname].setup,
...source.setup
];
} else {
// Maybe we have multiple testrunners for the same location etc
merge(testRunners[index], config);
target[source.hostname] = { ...source };
}
}

export function removeTestRunner(config) {
// Runners need to have uniqie ids.
// First remove the runner
testRunnersById.splice(
testRunnersById.findIndex(
testRunner => testRunner.hostname === config.hostname
),
1
);
function removeByHostname(hostnameToRemove) {
delete testRunners[hostnameToRemove];
}

// Update the merged version
const index = testRunners.findIndex(
testRunner => testRunner.name == config.name
);
let updatedSetup = {};
for (let testRunner of testRunnersById) {
if ((testRunner.name = config.name)) {
merge(updatedSetup, testRunner);
}
}
export function addTestRunner(config) {
mergeByHostname(testRunners, config);
}

if (Object.keys(updatedSetup) > 0) {
testRunners[index] = updatedSetup;
} else {
testRunners.splice(index, 1);
}
export function removeTestRunner(config) {
removeByHostname(config.hostname);
}

export function getTestRunners() {
return testRunners;
return Object.values(testRunners);
}

export function getTestRunnersConfiguration(name) {
const index = testRunners.findIndex(testRunner => testRunner.name === name);
return testRunners[index];
return Object.values(testRunners).find(
testRunner => testRunner.name === name
);
}
6 changes: 4 additions & 2 deletions server/views/includes/script.pug
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ script.
document.getElementById('connectivity').add(new Option(connectivity,connectivity), undefined);
}

for (let testType of location.setup.testTypes) {
document.getElementById('testType').add(new Option(testType,testType), undefined);
for (let setup of location.setup) {
if (setup.type) {
document.getElementById('testType').add(new Option(setup.type, setup.type), undefined);
}
}

for (let setup of location.setup) {
Expand Down
3 changes: 1 addition & 2 deletions testrunner/src/queue/queuehandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QueueHandler {
this.queues = {};
}

async start() {
async start(serverConfig) {
const port = nconf.get('redis:port') ?? 6379;
const host = nconf.get('redis:host') ?? '127.0.0.1';
const password = nconf.get('redis:password');
Expand Down Expand Up @@ -72,7 +72,6 @@ class QueueHandler {
});

// Lets tell the world (or the testsrunners queue) that we are live
const serverConfig = nconf.get('location');
const testRunnersQueue = await this.getQueue('testrunners');
testRunnersQueue.add({ type: 'start', serverConfig: serverConfig });
}
Expand Down
9 changes: 7 additions & 2 deletions testrunner/src/sitespeedio-testrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class SitespeedioTestRunner {

// If hostname isn't configured add it
const hostname = os.hostname();
if (nconf.get('hostname') === undefined) {
if (serverConfig.hostname === undefined) {
serverConfig.hostname = hostname;
logger.info('No hostname found in configuration. Will use %s', hostname);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export class SitespeedioTestRunner {
}
}

await queueHandler.start();
await queueHandler.start(serverConfig);

process.on('uncaughtException', error => {
// ioredis configuration is tricky to get right
Expand All @@ -82,6 +82,11 @@ export class SitespeedioTestRunner {
try {
const serverConfig = nconf.get('location');

const hostname = os.hostname();
if (serverConfig.hostname === undefined) {
serverConfig.hostname = hostname;
}

const testRunnerQueue = await queueHandler.getQueue('testrunners');

if (testRunnerQueue.client.status === 'ready') {
Expand Down
1 change: 1 addition & 0 deletions testrunner/src/validateconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Joi from 'joi';
const setupSchema = Joi.array().items(
Joi.object({
name: Joi.string().required(),
hostname: Joi.string().optional(),
type: Joi.string().valid('desktop', 'emulatedMobile', 'android').required(),
browsers: Joi.array()
.items(Joi.string().valid('chrome', 'firefox', 'edge', 'safari'))
Expand Down