|
| 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 | +} |
0 commit comments