Skip to content

Commit f46f0dd

Browse files
authored
Merge pull request #45 from csivitu/dev
feat: CLI to spawn workers
2 parents e7412fa + f3cc203 commit f46f0dd

File tree

7 files changed

+92
-1
lines changed

7 files changed

+92
-1
lines changed

bin/bin.ts

Whitespace-only changes.

bin/code-executor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../dist/src/index').cli(process.argv);

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-executor",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "A CLI/library to execute code against test cases in various languages and obtain relevant results.",
55
"main": "dist/src/index.js",
66
"keywords": [
@@ -12,6 +12,9 @@
1212
"parallel-execution",
1313
"code-runner"
1414
],
15+
"bin": {
16+
"code-executor": "bin/code-executor"
17+
},
1518
"scripts": {
1619
"build": "npm run clean && tsc && npm run build:dockerfiles",
1720
"build:dockerfiles": "shx cp -r src/langs dist/src/langs",
@@ -33,6 +36,7 @@
3336
"homepage": "https://github.com/csivitu/code-executor#readme",
3437
"devDependencies": {
3538
"@types/bull": "^3.14.2",
39+
"@types/commander": "^2.12.2",
3640
"@types/dockerode": "^2.5.34",
3741
"@types/node": "^14.6.3",
3842
"@types/randomstring": "^1.1.6",
@@ -56,6 +60,7 @@
5660
},
5761
"dependencies": {
5862
"bull": "^3.18.0",
63+
"commander": "^6.2.0",
5964
"del": "^6.0.0",
6065
"dockerode": "^3.2.1",
6166
"randomstring": "^1.1.5",

src/WorkerCLI.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import commander, { Command } from 'commander';
2+
import logger from './utils/logger';
3+
import { WorkerCLIOptions } from './models';
4+
import Worker from './Worker';
5+
6+
class WorkerCLI {
7+
readonly program: commander.Command;
8+
9+
readonly args: string[];
10+
11+
constructor(args: string[]) {
12+
this.program = new Command();
13+
this.args = args;
14+
this.init();
15+
}
16+
17+
init(): void {
18+
this.program
19+
.command('spawn-worker')
20+
.alias('sw')
21+
.description('spawn worker process')
22+
.action(() => {
23+
const options = this.parseOptions();
24+
WorkerCLI.spawnWorker(options);
25+
});
26+
27+
this.program
28+
.option('-r, --redis <redis>', 'URL for the redis instance')
29+
.option('-q, --queue <queue>', 'name of the redis queue')
30+
.option('-l, --langs <langs...>', 'list of languages to build');
31+
}
32+
33+
parseOptions(): WorkerCLIOptions {
34+
const opts = this.program.opts();
35+
36+
const options: WorkerCLIOptions = {
37+
redis: opts.redis || 'redis://127.0.0.1:6379',
38+
queue: opts.queue || 'myExecutor',
39+
langs: opts.langs || [],
40+
};
41+
42+
return options;
43+
}
44+
45+
static async spawnWorker(options: WorkerCLIOptions): Promise<void> {
46+
logger.info('Spawning Workers...');
47+
48+
const worker = new Worker(options.queue, options.redis);
49+
await worker.build(options.langs.length ? options.langs : undefined);
50+
worker.start();
51+
}
52+
53+
async start(): Promise<void> {
54+
await this.program.parseAsync(this.args);
55+
}
56+
}
57+
58+
export default async function cli(args: string[]): Promise<void> {
59+
const workerCLI = new WorkerCLI(args);
60+
workerCLI.start();
61+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export { default as CodeExecutor, languages } from './CodeExecutor';
22

33
export { default as Worker } from './Worker';
4+
5+
export { default as cli } from './WorkerCLI';

src/models/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ export interface WorkerOptions {
3535
memory?: number;
3636
CPUs?: number;
3737
}
38+
39+
export interface WorkerCLIOptions {
40+
queue: string;
41+
redis: string;
42+
langs: string[];
43+
}

0 commit comments

Comments
 (0)