Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/bump-bullmq-v6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crypt.fyi/server': patch
---

Upgrade bullmq to v6 to pick up security fixes.
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@opentelemetry/sdk-node": "^0.223.0",
"@opentelemetry/sdk-trace-base": "^2.0.1",
"@opentelemetry/semantic-conventions": "^1.36.0",
"bullmq": "^5.58.3",
"bullmq": "^6.7.1",
"bytes": "^3.1.2",
"fastify": "^5.12.5",
"fastify-type-provider-zod": "^5.0.3",
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/rotate-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Queue } from 'bullmq';
import { machine } from '@crypt.fyi/core';
import { config } from './config.js';
import { rotateVaultKeys, countWebhookJobsOnPreviousKeys } from './vault/rotate.js';
import { WEBHOOK_QUEUE_NAME } from './webhook.js';
import { WEBHOOK_QUEUE_NAME, toBullMQConnection } from './webhook.js';

const JOB_PAGE_SIZE = 500;

Expand All @@ -26,12 +26,14 @@ const main = async () => {

let webhookJobsOnPreviousKeys = 0;
if (config.webhookSender === 'bullmq') {
const queue = new Queue<string>(WEBHOOK_QUEUE_NAME, { connection: redis.options });
const queue = new Queue<string>(WEBHOOK_QUEUE_NAME, {
connection: toBullMQConnection(redis),
});
try {
// Paged per state so a large backlog never has to fit in memory at once.
// Jobs moving between states mid-count can be missed or counted twice;
// rerunning before removing a key covers that.
for (const state of ['waiting', 'delayed', 'active', 'paused', 'prioritized'] as const) {
for (const state of ['waiting', 'delayed', 'active', 'prioritized'] as const) {
for (let start = 0; ; start += JOB_PAGE_SIZE) {
const jobs = await queue.getJobs([state], start, start + JOB_PAGE_SIZE - 1);
webhookJobsOnPreviousKeys += await countWebhookJobsOnPreviousKeys(
Expand Down
18 changes: 15 additions & 3 deletions packages/server/src/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Queue, Worker } from 'bullmq';
import { Queue, Worker, type RedisOptions } from 'bullmq';
import { machine, isRetryableFetchError } from '@crypt.fyi/core';
import type { Logger } from './logging.js';
import { Redis } from 'ioredis';
Expand All @@ -18,6 +18,17 @@ const messageSchema = z.object({
export type Message = z.infer<typeof messageSchema>;

export const WEBHOOK_QUEUE_NAME = 'webhooks';

/**
* Connection options for a BullMQ Queue/Worker derived from an ioredis client.
* Passing options rather than the client keeps each Queue/Worker on its own
* connections, so closing them never races with the caller quitting `redis`.
* ioredis types `retryStrategy` as nullable but BullMQ v6 does not accept null.
*/
export const toBullMQConnection = (redis: Redis): RedisOptions => {
const { retryStrategy, ...options } = redis.options;
return { ...options, retryStrategy: retryStrategy ?? undefined };
};
const JOB_NAME = 'webhook';

export interface WebhookSender {
Expand Down Expand Up @@ -66,8 +77,9 @@ export const createBullMQWebhookSender = ({
fetchFn = fetch,
shouldRetry = isRetryableFetchError,
}: BullMQWebhookSenderOptions): { webhookSender: WebhookSender; cleanup: () => Promise<void> } => {
const connection = toBullMQConnection(redis);
const queue = new Queue<string>(WEBHOOK_QUEUE_NAME, {
connection: redis.options,
connection,
streams: {
events: {
maxLen: streamEventsMaxLength,
Expand Down Expand Up @@ -138,7 +150,7 @@ export const createBullMQWebhookSender = ({
clearTimeout(timeoutId);
}
},
{ connection: redis.options, concurrency, drainDelay: Math.round(drainDelayMs / 1000) },
{ connection, concurrency, drainDelay: Math.round(drainDelayMs / 1000) },
);

worker.on('failed', async (job, error) => {
Expand Down
117 changes: 68 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading