Implement queue work and listen commands
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-01-27 10:34:01 -06:00
parent e098a5edb7
commit 16e5fa00aa
19 changed files with 271 additions and 17 deletions

View File

@@ -0,0 +1,13 @@
import {Queueable} from '../types'
import {Awaitable} from '../../../util'
import {Inject, Injectable} from '../../../di'
import {Logging} from '../../../service/Logging'
import {CanonicalItemClass} from '../../CanonicalReceiver'
@Injectable()
export abstract class BaseJob extends CanonicalItemClass implements Queueable {
@Inject()
protected readonly logging!: Logging
abstract execute(): Awaitable<void>
}

View File

@@ -16,9 +16,9 @@ export abstract class Queue implements BusQueue {
async dispatch<T extends Queueable>(item: T): Promise<void> {
if ( shouldQueue(item) ) {
await this.bus.push(new PushingToQueue(item))
await this.bus.push(new PushingToQueue(item, this.name))
await this.push(item)
await this.bus.push(new PushedToQueue(item))
await this.bus.push(new PushedToQueue(item, this.name))
return
}

View File

@@ -5,7 +5,7 @@ import {uuid4} from '../../../../util'
* Event fired after an item is pushed to the queue.
*/
export class PushedToQueue<T extends ShouldQueue<Queueable>> implements Event {
public readonly eventName = '@extollo/lib:PushedToQueue'
public readonly eventName = '@extollo/lib.PushedToQueue'
public readonly eventUuid = uuid4()
@@ -13,5 +13,6 @@ export class PushedToQueue<T extends ShouldQueue<Queueable>> implements Event {
constructor(
public readonly item: T,
public readonly queueName: string,
) {}
}

View File

@@ -5,7 +5,7 @@ import {uuid4} from '../../../../util'
* Event fired before an item is pushed to the queue.
*/
export class PushingToQueue<T extends ShouldQueue<Queueable>> implements Event {
public readonly eventName = '@extollo/lib:PushingToQueue'
public readonly eventName = '@extollo/lib.PushingToQueue'
public readonly eventUuid = uuid4()
@@ -13,5 +13,6 @@ export class PushingToQueue<T extends ShouldQueue<Queueable>> implements Event {
constructor(
public readonly item: T,
public readonly queueName: string,
) {}
}

View File

@@ -0,0 +1,43 @@
import {PushedToQueue} from './PushedToQueue'
import {Queueable, SerialPayload, ShouldQueue} from '../../types'
import {PushingToQueue} from './PushingToQueue'
import {BaseSerializer} from '../../serial/BaseSerializer'
import {JSONState} from '../../../../util'
import {ObjectSerializer} from '../../serial/decorators'
export type QueueEvent = PushedToQueue<ShouldQueue<Queueable>> | PushingToQueue<ShouldQueue<Queueable>>
export interface QueueEventSerialPayload extends JSONState {
eventName: '@extollo/lib.PushedToQueue' | '@extollo/lib.PushingToQueue'
itemPayload: SerialPayload<Queueable, JSONState>
queueName: string
}
@ObjectSerializer()
export class QueueEventSerializer extends BaseSerializer<QueueEvent, QueueEventSerialPayload> {
protected async decodeSerial(serial: QueueEventSerialPayload): Promise<QueueEvent> {
const item = await this.getSerialization().decode(serial.itemPayload)
if ( serial.eventName === '@extollo/lib.PushedToQueue' ) {
return new PushedToQueue(item as ShouldQueue<Queueable>, serial.queueName)
} else {
return new PushingToQueue(item as ShouldQueue<Queueable>, serial.queueName)
}
}
protected async encodeActual(actual: QueueEvent): Promise<QueueEventSerialPayload> {
return {
eventName: actual.eventName,
queueName: actual.queueName,
itemPayload: await this.getSerialization().encode(actual.item),
}
}
protected getName(): string {
return '@extollo/lib.QueueEventSerializer'
}
matchActual(some: QueueEvent): boolean {
return some instanceof PushedToQueue || some instanceof PushingToQueue
}
}