Skip to content

Commit

Permalink
fix(@nftx/queue): generate custom job ids
Browse files Browse the repository at this point in the history
if a job is emitted multiple times, it gets picked up multiple times as well
there aren't any signals that would need to be worked on multiple times simultaneously
to avoid this, we now generate the jobId using the signal data, bullmq will automatically
de-dupe jobs with the same id so this should help reduce the number of duplicate jobs
  • Loading branch information
jackmellis committed Jul 16, 2024
1 parent 4655e9d commit 3e0e1dc
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/queue/src/actions/sendSignal.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { getSignalQueue } from '../queues';
import { Signals } from '../types';

const createJobId = <S extends keyof Signals>({
payload,
type,
}: {
type: S;
payload: Signals[S];
}) => {
const params = Object.entries(payload)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, value]) => `${key}=${value}`)
.join('&');

const header = `${type}__`;

return `${header}${params}`.replace(/:/g, ''); // jobId cannot contain colons
};

const sendSignal = <S extends keyof Signals>({
payload,
type,
}: {
type: S;
payload: Signals[S];
}) => {
const jobId = createJobId({ type, payload });
const queue = getSignalQueue();
return queue.add(type, payload);
return queue.add(type, payload, { jobId });
};

export default sendSignal;

0 comments on commit 3e0e1dc

Please sign in to comment.