Skip to content

Commit

Permalink
Merge pull request #45 from csivitu/dev
Browse files Browse the repository at this point in the history
feat: CLI to spawn workers
  • Loading branch information
roerohan authored Nov 12, 2020
2 parents e7412fa + f3cc203 commit f46f0dd
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
Empty file removed bin/bin.ts
Empty file.
3 changes: 3 additions & 0 deletions bin/code-executor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../dist/src/index').cli(process.argv);
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "code-executor",
"version": "0.2.4",
"version": "0.2.5",
"description": "A CLI/library to execute code against test cases in various languages and obtain relevant results.",
"main": "dist/src/index.js",
"keywords": [
Expand All @@ -12,6 +12,9 @@
"parallel-execution",
"code-runner"
],
"bin": {
"code-executor": "bin/code-executor"
},
"scripts": {
"build": "npm run clean && tsc && npm run build:dockerfiles",
"build:dockerfiles": "shx cp -r src/langs dist/src/langs",
Expand All @@ -33,6 +36,7 @@
"homepage": "https://github.com/csivitu/code-executor#readme",
"devDependencies": {
"@types/bull": "^3.14.2",
"@types/commander": "^2.12.2",
"@types/dockerode": "^2.5.34",
"@types/node": "^14.6.3",
"@types/randomstring": "^1.1.6",
Expand All @@ -56,6 +60,7 @@
},
"dependencies": {
"bull": "^3.18.0",
"commander": "^6.2.0",
"del": "^6.0.0",
"dockerode": "^3.2.1",
"randomstring": "^1.1.5",
Expand Down
61 changes: 61 additions & 0 deletions src/WorkerCLI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import commander, { Command } from 'commander';
import logger from './utils/logger';
import { WorkerCLIOptions } from './models';
import Worker from './Worker';

class WorkerCLI {
readonly program: commander.Command;

readonly args: string[];

constructor(args: string[]) {
this.program = new Command();
this.args = args;
this.init();
}

init(): void {
this.program
.command('spawn-worker')
.alias('sw')
.description('spawn worker process')
.action(() => {
const options = this.parseOptions();
WorkerCLI.spawnWorker(options);
});

this.program
.option('-r, --redis <redis>', 'URL for the redis instance')
.option('-q, --queue <queue>', 'name of the redis queue')
.option('-l, --langs <langs...>', 'list of languages to build');
}

parseOptions(): WorkerCLIOptions {
const opts = this.program.opts();

const options: WorkerCLIOptions = {
redis: opts.redis || 'redis://127.0.0.1:6379',
queue: opts.queue || 'myExecutor',
langs: opts.langs || [],
};

return options;
}

static async spawnWorker(options: WorkerCLIOptions): Promise<void> {
logger.info('Spawning Workers...');

const worker = new Worker(options.queue, options.redis);
await worker.build(options.langs.length ? options.langs : undefined);
worker.start();
}

async start(): Promise<void> {
await this.program.parseAsync(this.args);
}
}

export default async function cli(args: string[]): Promise<void> {
const workerCLI = new WorkerCLI(args);
workerCLI.start();
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as CodeExecutor, languages } from './CodeExecutor';

export { default as Worker } from './Worker';

export { default as cli } from './WorkerCLI';
6 changes: 6 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ export interface WorkerOptions {
memory?: number;
CPUs?: number;
}

export interface WorkerCLIOptions {
queue: string;
redis: string;
langs: string[];
}

0 comments on commit f46f0dd

Please sign in to comment.