-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a06bd51
commit 4d88550
Showing
19 changed files
with
503 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import type { QueueConfig } from 'zemble-plugin-bull/utils/setupQueues' | ||
import { ZembleQueue } from 'zemble-plugin-bull' | ||
|
||
const config: QueueConfig = { | ||
worker: (job) => { | ||
console.log(job.data) | ||
}, | ||
} | ||
|
||
export default config | ||
export default new ZembleQueue((job) => { | ||
console.log(job.data) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Queue, Worker } from 'bullmq' | ||
|
||
import type { | ||
Job, JobsOptions, QueueOptions, RedisOptions, RepeatOptions, | ||
} from 'bullmq' | ||
|
||
export interface BullPluginConfig extends Zemble.GlobalConfig { | ||
/** | ||
* The url of the redis instance to use for pubsub | ||
*/ | ||
readonly redisUrl?: string | ||
/** | ||
* Redis config to use for pubsub | ||
*/ | ||
readonly redisOptions?: RedisOptions | ||
} | ||
|
||
export type ZembleQueueConfig = { | ||
readonly repeat?: RepeatOptions | ||
readonly defaultJobOptions?: JobsOptions | ||
} | ||
|
||
export class ZembleQueue<DataType = unknown, ReturnType = unknown> { | ||
readonly #worker: (job: Job<DataType, ReturnType>) => Promise<void> | void | ||
|
||
readonly #config?: ZembleQueueConfig | ||
|
||
constructor( | ||
readonly worker: (job: Job<DataType, ReturnType>) => Promise<void> | void, | ||
config?: ZembleQueueConfig, | ||
) { | ||
this.#worker = worker | ||
this.#config = config | ||
} | ||
|
||
// eslint-disable-next-line functional/prefer-readonly-type | ||
#queueInternal: Queue<DataType, ReturnType> | undefined | ||
|
||
get #queue() { | ||
if (!this.#queueInternal) throw new Error('Queue not initialized, something is wrong!') | ||
return this.#queueInternal | ||
} | ||
|
||
async _initQueue(queueName: string, connection: QueueOptions['connection']) { | ||
const queue = new Queue(queueName, { | ||
connection, | ||
defaultJobOptions: this.#config?.defaultJobOptions, | ||
}) | ||
|
||
// eslint-disable-next-line functional/immutable-data | ||
this.#queueInternal = queue | ||
|
||
const repeatJobKey = `zemble-plugin-bull-repeat-${queue.name}` | ||
await queue.removeRepeatableByKey(repeatJobKey) | ||
if (this.#config?.repeat) { | ||
await queue.add(repeatJobKey, {} as DataType, { | ||
repeatJobKey, | ||
repeat: this.#config?.repeat, | ||
}) | ||
} | ||
|
||
// @ts-expect-error if this cannot be a promise I'm not sure how stuff will work | ||
const worker = new Worker(queueName, this.#worker, { | ||
connection, | ||
}) | ||
|
||
return { queue, worker } | ||
} | ||
|
||
async add(...args: Parameters<Queue<DataType, ReturnType>['add']>) { | ||
return this.#queue.add(...args) | ||
} | ||
|
||
async addBulk(...args: Parameters<Queue<DataType, ReturnType>['addBulk']>) { | ||
return this.#queue.addBulk(...args) | ||
} | ||
} | ||
|
||
export default ZembleQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Queue } from 'bullmq' | ||
|
||
import type { MutationResolvers } from '../schema.generated' | ||
|
||
const addJob: MutationResolvers['addRepeatableJob'] = async (_, { | ||
queue, data, repeatJobKey, pattern, | ||
}) => { | ||
const q = new Queue(queue, { | ||
|
||
}) | ||
|
||
const job = await q.add(queue, data, { | ||
repeatJobKey: repeatJobKey ?? undefined, | ||
repeat: { | ||
pattern, | ||
}, | ||
}) | ||
|
||
return job | ||
} | ||
|
||
export default addJob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Queue } from 'bullmq' | ||
|
||
import type { MutationResolvers } from '../schema.generated' | ||
|
||
const cleanQueue: MutationResolvers['cleanQueue'] = async (_, { | ||
queue, limit, grace, type, | ||
}) => { | ||
const q = new Queue(queue, { | ||
|
||
}) | ||
|
||
const res = await q.clean(grace, limit, type ?? undefined) | ||
|
||
return res | ||
} | ||
|
||
export default cleanQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Queue } from 'bullmq' | ||
|
||
import type { MutationResolvers } from '../schema.generated' | ||
|
||
const drainQueue: MutationResolvers['drainQueue'] = async (_, { queue }) => { | ||
const q = new Queue(queue, { | ||
|
||
}) | ||
|
||
await q.drain() | ||
|
||
return true | ||
} | ||
|
||
export default drainQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Queue } from 'bullmq' | ||
|
||
import type { MutationResolvers } from '../schema.generated' | ||
|
||
const removeJob: MutationResolvers['removeJob'] = async (_, { queue, jobId }) => { | ||
const q = new Queue(queue, { | ||
|
||
}) | ||
|
||
const success = await q.remove(jobId) | ||
|
||
return success === 1 | ||
} | ||
|
||
export default removeJob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Queue } from 'bullmq' | ||
|
||
import type { MutationResolvers } from '../schema.generated' | ||
|
||
const removeRepeatableJob: MutationResolvers['removeRepeatableJob'] = async (_, { | ||
queue, repeatJobKey, | ||
}) => { | ||
const q = new Queue(queue, { | ||
|
||
}) | ||
|
||
const job = await q.removeRepeatableByKey(repeatJobKey) | ||
|
||
return job | ||
} | ||
|
||
export default removeRepeatableJob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Resolvers } from '../schema.generated' | ||
import type { Queue } from 'bullmq' | ||
|
||
const BullQueueResolvers: Resolvers['BullQueue'] = { | ||
name: ({ name }) => name, | ||
count: async (queue) => queue.count(), | ||
completedCount: async (queue) => queue.getCompletedCount(), | ||
activeCount: async (queue) => queue.getActiveCount(), | ||
waitingCount: async (queue) => queue.getWaitingCount(), | ||
waitingChildrenCount: async (queue) => queue.getWaitingChildrenCount(), | ||
failedCount: async (queue) => queue.getFailedCount(), | ||
isPaused: async (queue) => queue.isPaused(), | ||
repeatableJobs: async (queue, { | ||
start, | ||
end, | ||
asc, | ||
}) => { | ||
const repeatableJobs = await queue.getRepeatableJobs( | ||
start ?? undefined, | ||
end ?? undefined, | ||
asc ?? undefined, | ||
) | ||
|
||
return repeatableJobs | ||
}, | ||
jobs: async (queue: Queue, { | ||
start, end, asc, type, | ||
}) => { | ||
const jobs = await queue.getJobs( | ||
// @ts-expect-error readonly stuff | ||
Array.isArray(type) ? [...type] : type, | ||
start, | ||
end, | ||
asc, | ||
) | ||
|
||
// filter out repeatable jobs (they're undefined, weird but yeah) | ||
return jobs.filter((j) => !!j) ?? [] | ||
}, | ||
} | ||
|
||
export default BullQueueResolvers |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.