Skip to content

Commit

Permalink
feat: allow mutiple specs per agent (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val authored Aug 25, 2019
1 parent 3ee425a commit de23d70
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
11 changes: 5 additions & 6 deletions packages/@best/agent-hub/src/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export interface AgentConfig {
path: string
proxy?: string
},
spec: {
browser: string,
version: string,
}
specs: Spec[],
remoteRunner: string,
remoteRunnerConfig?: object
}
Expand Down Expand Up @@ -107,8 +104,10 @@ export class Agent extends EventEmitter {
}

canRunJob(spec: Spec): boolean {
const jobHasSameSpec = spec.browser == this._config.spec.browser &&
spec.version === this._config.spec.version;
const specs = this._config.specs;
const jobHasSameSpec = specs.some(({ browser, version }) => {
browser === spec.browser && version === spec.version
});

return jobHasSameSpec && this.status !== AgentStatus.Offline;
}
Expand Down
42 changes: 32 additions & 10 deletions packages/@best/agent-hub/src/agents-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {Application, NextFunction, Request, Response} from "express";
import {AgentManager} from "./AgentManager";
import {Agent, AgentStatus} from "./Agent";
import {Agent, AgentStatus, AgentConfig} from "./Agent";
import AgentLogger from "@best/agent-logger";

function authenticateAgentApi(tokenSecret: string, req: Request, res: Response, next: NextFunction) {
Expand All @@ -25,24 +25,46 @@ function authenticateAgentApi(tokenSecret: string, req: Request, res: Response,
}
}

function addAgentToHub(agentManager: AgentManager, logger: AgentLogger, req: Request, res: Response) {
// @todo: validate payload.
const agentConfig = {
host: req.body.host,
options: req.body.options,
spec: req.body.spec,
remoteRunner: req.body.remoteRunner,
remoteRunnerConfig: req.body.remoteRunnerConfig
function validateSpec(maybeSpecObject: any): boolean {
if (!maybeSpecObject.browser || !maybeSpecObject.version) {
throw new Error('Invalid browser specification. Must contain "browser" and "version" properties');
}

return true;
}

function normalizeAndValidateAgentConfig(rawAgentConfig: any): AgentConfig {
const { host, options, spec: specConfig, remoteRunner, remoteRunnerConfig } = rawAgentConfig;
if (!host) {
throw new Error('Host for Agent must be provided');
}

if (!remoteRunner) {
throw new Error('Remote runner must be provided');
}

const specs = Array.isArray(specConfig) ? specConfig : [specConfig];
specs.forEach(validateSpec);

return {
host,
options,
specs,
remoteRunner,
remoteRunnerConfig
};
}

function addAgentToHub(agentManager: AgentManager, logger: AgentLogger, req: Request, res: Response) {
const agentConfig = normalizeAndValidateAgentConfig(req.body);
const agent = new Agent(agentConfig, logger);
agent.status = AgentStatus.Offline;

agentManager.addAgent(agent);

agent.status = AgentStatus.Idle;

logger.info('', 'agent added', [agentConfig.host, agentConfig.spec]);
logger.info('', 'agent added', [agentConfig.host, agentConfig.specs]);

return res.status(201).send({
success: 'true',
Expand Down
6 changes: 3 additions & 3 deletions packages/@best/agent/src/benchmark-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import SocketIOFile from "socket.io-file";
import path from "path";
import SocketIOFile from "socket.io-file";
import { cacheDirectory } from '@best/utils';
import * as SocketIO from "socket.io";
import { Socket } from "socket.io";
import AgentLogger from "@best/agent-logger";

// This is all part of the initialization
Expand All @@ -23,7 +23,7 @@ const LOADER_CONFIG = {

const UPLOAD_START_TIMEOUT = 5000;

export async function loadBenchmarkJob(socketConnection: SocketIO.Socket, logger: AgentLogger): Promise<any> {
export async function loadBenchmarkJob(socketConnection: Socket, logger: AgentLogger): Promise<any> {
return new Promise(async (resolve, reject) => {
const socket = socketConnection;
let uploaderTimeout: any = null;
Expand Down
10 changes: 6 additions & 4 deletions packages/@best/agent/src/hub-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

import axios from "axios";

export interface Spec {
browser: string,
version: string
}

export interface HubConfig {
hub: {
host: string,
authToken: string,
pingTimeout: number,
},
agentConfig: {
spec: {
browser: string,
version: string
},
spec: Spec | Spec[],
host: string,
options: { path: string },
remoteRunner: string,
Expand Down

0 comments on commit de23d70

Please sign in to comment.