2023-01-13 05:40:33 +01:00
|
|
|
import Bull from "bull";
|
|
|
|
import config from "@/config/index.js";
|
2021-03-18 03:17:05 +01:00
|
|
|
|
2021-05-08 11:56:21 +02:00
|
|
|
export function initialize<T>(name: string, limitPerSec = -1) {
|
|
|
|
return new Bull<T>(name, {
|
2021-03-18 03:17:05 +01:00
|
|
|
redis: {
|
|
|
|
port: config.redis.port,
|
|
|
|
host: config.redis.host,
|
2022-06-24 12:22:19 +02:00
|
|
|
family: config.redis.family == null ? 0 : config.redis.family,
|
2021-03-18 03:17:05 +01:00
|
|
|
password: config.redis.pass,
|
|
|
|
db: config.redis.db || 0,
|
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
prefix: config.redis.prefix ? `${config.redis.prefix}:queue` : "queue",
|
|
|
|
limiter:
|
|
|
|
limitPerSec > 0
|
|
|
|
? {
|
|
|
|
max: limitPerSec,
|
|
|
|
duration: 1000,
|
|
|
|
}
|
|
|
|
: undefined,
|
2021-08-20 14:59:03 +02:00
|
|
|
settings: {
|
2023-05-24 20:21:15 +02:00
|
|
|
stalledInterval: 60,
|
|
|
|
maxStalledCount: 2,
|
2021-08-20 14:59:03 +02:00
|
|
|
backoffStrategies: {
|
2021-12-09 15:58:30 +01:00
|
|
|
apBackoff,
|
|
|
|
},
|
|
|
|
},
|
2021-03-18 03:17:05 +01:00
|
|
|
});
|
|
|
|
}
|
2021-08-20 14:59:03 +02:00
|
|
|
|
|
|
|
// ref. https://github.com/misskey-dev/misskey/pull/7635#issue-971097019
|
|
|
|
function apBackoff(attemptsMade: number, err: Error) {
|
2023-01-13 05:40:33 +01:00
|
|
|
const baseDelay = 60 * 1000; // 1min
|
|
|
|
const maxBackoff = 8 * 60 * 60 * 1000; // 8hours
|
2021-08-20 14:59:03 +02:00
|
|
|
let backoff = (Math.pow(2, attemptsMade) - 1) * baseDelay;
|
|
|
|
backoff = Math.min(backoff, maxBackoff);
|
|
|
|
backoff += Math.round(backoff * Math.random() * 0.2);
|
|
|
|
return backoff;
|
|
|
|
}
|