Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom job data formatter #204

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/@types/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as Redis from 'ioredis'

import { Job, JobOptions } from 'bull'
import { Job as JobMq, JobsOptions } from 'bullmq'

import React from 'react'
import * as Redis from 'ioredis'
import { Status } from '../ui/components/constants'

export type JobCleanStatus =
Expand All @@ -11,12 +13,17 @@ export type JobCleanStatus =
| 'delayed'
| 'failed'

export type AdapterOptions = {
dataFormatter(job: Job | JobMq): AppJob
}

export type JobStatus = Status

export type JobCounts = Record<JobStatus, number>

export interface QueueAdapter {
readonly client: Promise<Redis.Redis>
readonly options?: AdapterOptions
getName(): string

getJob(id: string): Promise<Job | JobMq | undefined | null>
Expand Down Expand Up @@ -65,6 +72,8 @@ export interface AppJob {
returnValue: string | Record<string | number, any> | null
}

export type DataFormatter = (data: JobMq['data']) => JobMq['data']

export interface AppQueue {
name: string
counts: Record<Status, number>
Expand Down
5 changes: 3 additions & 2 deletions src/queueAdapters/bull.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Job, Queue } from 'bull'
import {
AdapterOptions,
JobCleanStatus,
JobCounts,
JobStatus,
QueueAdapter,
} from '../@types/app'
import { Job, Queue } from 'bull'

export class BullAdapter implements QueueAdapter {
public get client() {
return Promise.resolve(this.queue.client)
}

constructor(public queue: Queue) {}
constructor(public queue: Queue, public options?: AdapterOptions) {}

public getName(): string {
return this.queue.name
Expand Down
6 changes: 4 additions & 2 deletions src/queueAdapters/bullMQ.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Job, Queue } from 'bullmq'
import * as Redis from 'ioredis'

import {
AdapterOptions,
JobCleanStatus,
JobCounts,
JobStatus,
QueueAdapter,
} from '../@types/app'
import { Job, Queue } from 'bullmq'

export class BullMQAdapter implements QueueAdapter {
private readonly LIMIT = 1000
Expand All @@ -14,7 +16,7 @@ export class BullMQAdapter implements QueueAdapter {
return (this.queue.client as unknown) as Promise<Redis.Redis>
}

constructor(private queue: Queue) {}
constructor(private queue: Queue, public options?: AdapterOptions) {}

public getName(): string {
return this.queue.toKey('~')
Expand Down
49 changes: 26 additions & 23 deletions src/routes/queues.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Job } from 'bull'
import { Job as JobMq } from 'bullmq'
import { Request, RequestHandler, Response } from 'express-serve-static-core'
import { parse as parseRedisInfo } from 'redis-info'

import * as api from '../@types/api'
import * as app from '../@types/app'

import { Request, RequestHandler, Response } from 'express-serve-static-core'

import { Job } from 'bull'
import { Job as JobMq } from 'bullmq'
import { JobStatus } from '../@types/app'
import { Status } from '../ui/components/constants'
import { parse as parseRedisInfo } from 'redis-info'

type MetricName = keyof app.ValidMetrics

Expand Down Expand Up @@ -39,23 +40,25 @@ const getStats = async ({
return validMetrics
}

const formatJob = (job: Job | JobMq): app.AppJob => {
const jobProps = job.toJSON()
const formatJob = (dataFormatter: app.DataFormatter = (d) => d) => {
return (job: Job | JobMq): app.AppJob => {
const jobProps = job.toJSON()

return {
id: jobProps.id,
timestamp: jobProps.timestamp,
processedOn: jobProps.processedOn,
finishedOn: jobProps.finishedOn,
progress: jobProps.progress,
attempts: jobProps.attemptsMade,
delay: job.opts.delay,
failedReason: jobProps.failedReason,
stacktrace: jobProps.stacktrace,
opts: jobProps.opts,
data: jobProps.data,
name: jobProps.name,
returnValue: jobProps.returnvalue,
return {
id: jobProps.id,
timestamp: jobProps.timestamp,
processedOn: jobProps.processedOn,
finishedOn: jobProps.finishedOn,
progress: jobProps.progress,
attempts: jobProps.attemptsMade,
delay: job.opts.delay,
failedReason: jobProps.failedReason,
stacktrace: jobProps.stacktrace,
opts: jobProps.opts,
data: dataFormatter(jobProps.data),
name: jobProps.name,
returnValue: jobProps.returnvalue,
}
}
}

Expand Down Expand Up @@ -88,11 +91,11 @@ const getDataForQueues = async (
const status =
query[name] === 'latest' ? statuses : (query[name] as JobStatus[])
const jobs = await queue.getJobs(status, 0, 10)

const formatter = formatJob(queue.options?.dataFormatter)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is better to incapsulate the formatting part inside the adapters (inside getJobs), therefore, making AdapterOptions as private field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yasss, maybe this is more correct. Will update 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do this but reached the extent of my typescript knowledge and am bumbling around trying to figure it out. Sorry no more time to spend on it now.

return {
name,
counts: counts as Record<Status, number>,
jobs: jobs.map(formatJob),
jobs: jobs.map(formatter),
}
}),
)
Expand Down