2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-10 08:05:08 +02:00
|
|
|
import { Inject, Module, OnApplicationShutdown } from '@nestjs/common';
|
2023-05-29 04:54:49 +02:00
|
|
|
import * as Bull from 'bullmq';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2024-06-08 08:34:19 +02:00
|
|
|
import { baseQueueOptions, QUEUE } from '@/queue/const.js';
|
2024-01-08 04:28:13 +01:00
|
|
|
import { allSettled } from '@/misc/promise-tracker.js';
|
2024-06-08 08:34:19 +02:00
|
|
|
import {
|
|
|
|
DeliverJobData,
|
|
|
|
EndedPollNotificationJobData,
|
|
|
|
InboxJobData,
|
|
|
|
RelationshipJobData,
|
|
|
|
UserWebhookDeliverJobData,
|
|
|
|
SystemWebhookDeliverJobData,
|
|
|
|
} from '../queue/types.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { Provider } from '@nestjs/common';
|
|
|
|
|
|
|
|
export type SystemQueue = Bull.Queue<Record<string, unknown>>;
|
|
|
|
export type EndedPollNotificationQueue = Bull.Queue<EndedPollNotificationJobData>;
|
|
|
|
export type DeliverQueue = Bull.Queue<DeliverJobData>;
|
|
|
|
export type InboxQueue = Bull.Queue<InboxJobData>;
|
2023-05-10 08:05:08 +02:00
|
|
|
export type DbQueue = Bull.Queue;
|
2023-04-12 02:13:58 +02:00
|
|
|
export type RelationshipQueue = Bull.Queue<RelationshipJobData>;
|
2023-05-10 08:05:08 +02:00
|
|
|
export type ObjectStorageQueue = Bull.Queue;
|
2024-06-08 08:34:19 +02:00
|
|
|
export type UserWebhookDeliverQueue = Bull.Queue<UserWebhookDeliverJobData>;
|
|
|
|
export type SystemWebhookDeliverQueue = Bull.Queue<SystemWebhookDeliverJobData>;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
const $system: Provider = {
|
|
|
|
provide: 'queue:system',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.SYSTEM, baseQueueOptions(config, QUEUE.SYSTEM)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $endedPollNotification: Provider = {
|
|
|
|
provide: 'queue:endedPollNotification',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.ENDED_POLL_NOTIFICATION, baseQueueOptions(config, QUEUE.ENDED_POLL_NOTIFICATION)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $deliver: Provider = {
|
|
|
|
provide: 'queue:deliver',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.DELIVER, baseQueueOptions(config, QUEUE.DELIVER)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $inbox: Provider = {
|
|
|
|
provide: 'queue:inbox',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.INBOX, baseQueueOptions(config, QUEUE.INBOX)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $db: Provider = {
|
|
|
|
provide: 'queue:db',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.DB, baseQueueOptions(config, QUEUE.DB)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
const $relationship: Provider = {
|
|
|
|
provide: 'queue:relationship',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.RELATIONSHIP, baseQueueOptions(config, QUEUE.RELATIONSHIP)),
|
2023-04-12 02:13:58 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const $objectStorage: Provider = {
|
|
|
|
provide: 'queue:objectStorage',
|
2023-05-29 04:54:49 +02:00
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.OBJECT_STORAGE, baseQueueOptions(config, QUEUE.OBJECT_STORAGE)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2024-06-08 08:34:19 +02:00
|
|
|
const $userWebhookDeliver: Provider = {
|
|
|
|
provide: 'queue:userWebhookDeliver',
|
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.USER_WEBHOOK_DELIVER, baseQueueOptions(config, QUEUE.USER_WEBHOOK_DELIVER)),
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $systemWebhookDeliver: Provider = {
|
|
|
|
provide: 'queue:systemWebhookDeliver',
|
|
|
|
useFactory: (config: Config) => new Bull.Queue(QUEUE.SYSTEM_WEBHOOK_DELIVER, baseQueueOptions(config, QUEUE.SYSTEM_WEBHOOK_DELIVER)),
|
2022-09-17 20:27:08 +02:00
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
@Module({
|
|
|
|
imports: [
|
|
|
|
],
|
|
|
|
providers: [
|
|
|
|
$system,
|
|
|
|
$endedPollNotification,
|
|
|
|
$deliver,
|
|
|
|
$inbox,
|
|
|
|
$db,
|
2023-04-12 02:13:58 +02:00
|
|
|
$relationship,
|
2022-09-17 20:27:08 +02:00
|
|
|
$objectStorage,
|
2024-06-08 08:34:19 +02:00
|
|
|
$userWebhookDeliver,
|
|
|
|
$systemWebhookDeliver,
|
2022-09-17 20:27:08 +02:00
|
|
|
],
|
|
|
|
exports: [
|
|
|
|
$system,
|
|
|
|
$endedPollNotification,
|
|
|
|
$deliver,
|
|
|
|
$inbox,
|
|
|
|
$db,
|
2023-04-12 02:13:58 +02:00
|
|
|
$relationship,
|
2022-09-17 20:27:08 +02:00
|
|
|
$objectStorage,
|
2024-06-08 08:34:19 +02:00
|
|
|
$userWebhookDeliver,
|
|
|
|
$systemWebhookDeliver,
|
2022-09-17 20:27:08 +02:00
|
|
|
],
|
|
|
|
})
|
2023-05-10 08:05:08 +02:00
|
|
|
export class QueueModule implements OnApplicationShutdown {
|
|
|
|
constructor(
|
|
|
|
@Inject('queue:system') public systemQueue: SystemQueue,
|
|
|
|
@Inject('queue:endedPollNotification') public endedPollNotificationQueue: EndedPollNotificationQueue,
|
|
|
|
@Inject('queue:deliver') public deliverQueue: DeliverQueue,
|
|
|
|
@Inject('queue:inbox') public inboxQueue: InboxQueue,
|
|
|
|
@Inject('queue:db') public dbQueue: DbQueue,
|
|
|
|
@Inject('queue:relationship') public relationshipQueue: RelationshipQueue,
|
|
|
|
@Inject('queue:objectStorage') public objectStorageQueue: ObjectStorageQueue,
|
2024-06-08 08:34:19 +02:00
|
|
|
@Inject('queue:userWebhookDeliver') public userWebhookDeliverQueue: UserWebhookDeliverQueue,
|
|
|
|
@Inject('queue:systemWebhookDeliver') public systemWebhookDeliverQueue: SystemWebhookDeliverQueue,
|
2023-05-10 08:05:08 +02:00
|
|
|
) {}
|
|
|
|
|
2023-05-29 06:21:26 +02:00
|
|
|
public async dispose(): Promise<void> {
|
2024-01-08 04:28:13 +01:00
|
|
|
// Wait for all potential queue jobs
|
|
|
|
await allSettled();
|
|
|
|
// And then close all queues
|
2023-05-10 08:05:08 +02:00
|
|
|
await Promise.all([
|
|
|
|
this.systemQueue.close(),
|
|
|
|
this.endedPollNotificationQueue.close(),
|
|
|
|
this.deliverQueue.close(),
|
|
|
|
this.inboxQueue.close(),
|
|
|
|
this.dbQueue.close(),
|
|
|
|
this.relationshipQueue.close(),
|
|
|
|
this.objectStorageQueue.close(),
|
2024-06-08 08:34:19 +02:00
|
|
|
this.userWebhookDeliverQueue.close(),
|
|
|
|
this.systemWebhookDeliverQueue.close(),
|
2023-05-10 08:05:08 +02:00
|
|
|
]);
|
|
|
|
}
|
2023-05-29 06:21:26 +02:00
|
|
|
|
|
|
|
async onApplicationShutdown(signal: string): Promise<void> {
|
|
|
|
await this.dispose();
|
|
|
|
}
|
2023-05-10 08:05:08 +02:00
|
|
|
}
|