Implement queue work and listen commands
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -468,4 +468,22 @@ export abstract class Directive extends AppClass {
|
||||
protected nativeOutput(...outputs: any[]): void {
|
||||
console.log(...outputs) // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a promise that resolves after SIGINT is received, executing a
|
||||
* callback beforehand.
|
||||
* @param callback
|
||||
* @protected
|
||||
*/
|
||||
protected async untilInterrupt(callback?: () => unknown): Promise<void> {
|
||||
return new Promise<void>(res => {
|
||||
process.on('SIGINT', async () => {
|
||||
if ( callback ) {
|
||||
await callback()
|
||||
}
|
||||
|
||||
res()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
64
src/cli/directive/queue/ListenDirective.ts
Normal file
64
src/cli/directive/queue/ListenDirective.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import {Directive, OptionDefinition} from '../../Directive'
|
||||
import {Inject, Injectable} from '../../../di'
|
||||
import {Bus, PushedToQueue, Queue} from '../../../support/bus'
|
||||
import {Queueables} from '../../../service/Queueables'
|
||||
|
||||
@Injectable()
|
||||
export class ListenDirective extends Directive {
|
||||
@Inject()
|
||||
protected readonly queue!: Queue
|
||||
|
||||
@Inject()
|
||||
protected readonly queueables!: Queueables
|
||||
|
||||
@Inject()
|
||||
protected readonly bus!: Bus
|
||||
|
||||
getDescription(): string {
|
||||
return 'listen for jobs pushed to the queue and attempt to execute them'
|
||||
}
|
||||
|
||||
getKeywords(): string | string[] {
|
||||
return 'queue-listen'
|
||||
}
|
||||
|
||||
getOptions(): OptionDefinition[] {
|
||||
return []
|
||||
}
|
||||
|
||||
async handle(): Promise<void> {
|
||||
this.info('Subscribing to queue events...')
|
||||
await this.bus.subscribe(PushedToQueue, async () => {
|
||||
// A new job has been pushed to the queue, so try to pop it and execute it.
|
||||
// We may get undefined if some other worker is running and picked up this job first.
|
||||
await this.tryExecuteJob()
|
||||
})
|
||||
|
||||
this.info('Setting periodic poll...')
|
||||
const handle = setInterval(async () => {
|
||||
await this.tryExecuteJob()
|
||||
}, 5000)
|
||||
|
||||
this.info('Listening for jobs...')
|
||||
await this.untilInterrupt()
|
||||
|
||||
this.info('Shutting down...')
|
||||
clearInterval(handle)
|
||||
}
|
||||
|
||||
protected async tryExecuteJob(): Promise<void> {
|
||||
try {
|
||||
const job = await this.queue.pop()
|
||||
if ( !job ) {
|
||||
return // Some other worker already picked up this job
|
||||
}
|
||||
|
||||
this.info(`Executing: ${job.constructor?.name || 'unknown job'}`)
|
||||
await job.execute()
|
||||
this.success('Execution finished.')
|
||||
} catch (e: unknown) {
|
||||
this.error('Failed to execute job.')
|
||||
this.error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
import {Directive, OptionDefinition} from '../../Directive'
|
||||
import {Inject, Injectable} from '../../../di'
|
||||
import {Queue} from '../../../support/bus'
|
||||
import {Queueables} from '../../../service/Queueables'
|
||||
|
||||
@Injectable()
|
||||
export class WorkDirective extends Directive {
|
||||
@Inject()
|
||||
protected readonly queue!: Queue
|
||||
|
||||
@Inject()
|
||||
protected readonly queueables!: Queueables
|
||||
|
||||
getDescription(): string {
|
||||
return 'pop a single item from the queue and execute it'
|
||||
}
|
||||
|
||||
@@ -13,3 +13,6 @@ export * from './directive/TemplateDirective'
|
||||
export * from './directive/UsageDirective'
|
||||
|
||||
export * from './decorators'
|
||||
|
||||
export * from './directive/queue/ListenDirective'
|
||||
export * from './directive/queue/WorkDirective'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {Unit} from '../../lifecycle/Unit'
|
||||
import {Logging} from '../../service/Logging'
|
||||
import {Singleton, Inject} from '../../di/decorator/injection'
|
||||
import {Singleton, Inject} from '../../di'
|
||||
import {CommandLine} from './CommandLine'
|
||||
import {UsageDirective} from '../directive/UsageDirective'
|
||||
import {Directive} from '../Directive'
|
||||
@@ -10,6 +10,7 @@ import {RunDirective} from '../directive/RunDirective'
|
||||
import {RoutesDirective} from '../directive/RoutesDirective'
|
||||
import {RouteDirective} from '../directive/RouteDirective'
|
||||
import {WorkDirective} from '../directive/queue/WorkDirective'
|
||||
import {ListenDirective} from '../directive/queue/ListenDirective'
|
||||
|
||||
/**
|
||||
* Unit that takes the place of the final unit in the application that handles
|
||||
@@ -48,6 +49,7 @@ export class CommandLineApplication extends Unit {
|
||||
this.cli.registerDirective(RoutesDirective)
|
||||
this.cli.registerDirective(RouteDirective)
|
||||
this.cli.registerDirective(WorkDirective)
|
||||
this.cli.registerDirective(ListenDirective)
|
||||
|
||||
const argv = process.argv.slice(2)
|
||||
const match = this.cli.getDirectives()
|
||||
|
||||
Reference in New Issue
Block a user