generated from csivitu/Template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from csivitu/dev
feat: CLI to spawn workers
- Loading branch information
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters