Skip to content

Commit

Permalink
feat(queue): improve factory methods for queue entities
Browse files Browse the repository at this point in the history
BREAKING CHANGE: factory methods require a partial entity as argument.
  • Loading branch information
M.C. van den Boogaart committed Jun 25, 2021
1 parent 41770f3 commit f3d6839
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
22 changes: 12 additions & 10 deletions src/server/entities/queue-run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Queue } from './queue'
import type { QueueRun as QueueRunBase } from './base'
import { createQueue } from './queue'

export interface QueueRun<Options = unknown> extends Required<QueueRunBase> {
/**
Expand Down Expand Up @@ -53,35 +54,36 @@ export interface QueueRun<Options = unknown> extends Required<QueueRunBase> {
options: Options

/**
* The reason the queue run failed.
* The parent queue as Queue.
*/
reason: string | null
queue: Queue<Options>

/**
* The parent queue as Queue.
* The reason the queue run failed.
*/
queue?: Queue<Options>
reason: string | null

/**
* The status.
*/
status: 'err' | 'ok' | 'pending'
}

export function createQueueRun<Options> (queue?: Queue<Options>): QueueRun<Options> {
export function createQueueRun<Options = Record<string, unknown>> (queueRun?: Partial<QueueRun<Options>>): QueueRun<Options> {
return {
aggr_err: 0,
aggr_ok: 0,
aggr_total: 0,
date_created: new Date(),
date_updated: new Date(),
fkey_queue_id: queue?.id ?? 0,
fkey_queue_id: queueRun?.queue?.id ?? 0,
fkey_task_run_id: null,
id: 0,
name: queue?.name ?? 'name',
options: (queue?.options ?? {}) as Options,
queue,
name: queueRun?.queue?.name ?? 'name',
options: (queueRun?.queue?.options ?? {}) as Options,
queue: queueRun?.queue ?? createQueue<Options>(),
reason: null,
status: 'pending'
status: 'pending',
...queueRun
}
}
4 changes: 2 additions & 2 deletions src/server/entities/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ export interface Queue<Options = unknown> extends Required<QueueBase> {
schedule_next: Date | null
}

export function createQueue<Options> (options?: Options): Queue<Options> {
export function createQueue<Options = Record<string, unknown>> (queue?: Partial<Queue<Options>>): Queue<Options> {
return {
database: 'database',
date_created: new Date(),
date_updated: new Date(),
fkey_queue_id: 0,
id: 0,
name: 'name',
options: (options ?? {}) as Options,
options: (queue?.options ?? {}) as Options,
query: '',
schedule: '',
schedule_begin: null,
Expand Down
9 changes: 5 additions & 4 deletions src/server/entities/task-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface TaskRun<Payload = unknown, Options = unknown, Result = unknown>
status: 'err' | 'ok' | 'pending'
}

export function createTaskRun<Payload, Options, Result> (payload?: Payload): TaskRun<Payload, Options, Result> {
export function createTaskRun<Payload = Record<string, unknown>, Options = Record<string, unknown>, Result = Record<string, unknown>> (taskRun?: Partial<TaskRun<Payload, Options, Result>>): TaskRun<Payload, Options, Result> {
return {
date_created: new Date(),
date_queued: null,
Expand All @@ -73,10 +73,11 @@ export function createTaskRun<Payload, Options, Result> (payload?: Payload): Tas
fkey_queue_run_id: 0,
host: null,
id: 0,
payload: (payload ?? {}) as Payload,
queueRun: createQueueRun(),
payload: Object.create(null) as Payload,
queueRun: taskRun?.queueRun ?? createQueueRun<Options>(),
reason: null,
result: Object.create(null) as Result,
status: 'pending'
status: 'pending',
...taskRun
}
}
4 changes: 2 additions & 2 deletions src/server/helpers/queue/queue-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class QueueRunner {
* @param parameters - The parameters for the generator query
*/
public async run (queue: Queue, parameters?: Record<string, unknown>): Promise<void> {
const queueRun: QueueRun = createQueueRun(queue)
const queueRun: QueueRun = createQueueRun({ queue })
const { id: queueRunId } = await this.insertQueueRun(queueRun)
queueRun.id = queueRunId

Expand Down Expand Up @@ -168,7 +168,7 @@ export class QueueRunner {
$(options)
)
`, {
fkey_queue_id: queueRun.queue?.id,
fkey_queue_id: queueRun.queue.id,
name: queueRun.name,
options: queueRun.options
})
Expand Down

0 comments on commit f3d6839

Please sign in to comment.