Sharkey/packages/backend/src/queue/index.ts

334 lines
10 KiB
TypeScript
Raw Normal View History

2022-02-28 18:36:50 +02:00
import httpSignature from 'http-signature';
2022-04-03 16:36:30 +03:00
import { v4 as uuid } from 'uuid';
import config from '@/config/index.js';
import { envOption } from '../env.js';
2019-03-07 16:07:21 +02:00
import processDeliver from './processors/deliver.js';
import processInbox from './processors/inbox.js';
import processDb from './processors/db/index.js';
import processObjectStorage from './processors/object-storage/index.js';
import processSystemQueue from './processors/system/index.js';
import processWebhookDeliver from './processors/webhook-deliver.js';
import { endedPollNotification } from './processors/ended-poll-notification.js';
import { queueLogger } from './logger.js';
import { DriveFile } from '@/models/entities/drive-file.js';
import { getJobInfo } from './get-job-info.js';
import { systemQueue, dbQueue, deliverQueue, inboxQueue, objectStorageQueue, endedPollNotificationQueue, webhookDeliverQueue } from './queues.js';
import { ThinUser } from './types.js';
import { IActivity } from '@/remote/activitypub/type.js';
2022-04-03 16:36:30 +03:00
import { Webhook, webhookEventTypes } from '@/models/entities/webhook.js';
2019-04-13 20:19:59 +03:00
function renderError(e: Error): any {
return {
2020-01-19 18:52:35 +02:00
stack: e?.stack,
message: e?.message,
2021-12-09 16:58:30 +02:00
name: e?.name,
2019-04-13 20:19:59 +03:00
};
}
const systemLogger = queueLogger.createSubLogger('system');
2019-03-09 03:18:59 +02:00
const deliverLogger = queueLogger.createSubLogger('deliver');
const webhookLogger = queueLogger.createSubLogger('webhook');
2019-03-09 03:18:59 +02:00
const inboxLogger = queueLogger.createSubLogger('inbox');
2019-05-27 10:54:47 +03:00
const dbLogger = queueLogger.createSubLogger('db');
const objectStorageLogger = queueLogger.createSubLogger('objectStorage');
2019-03-09 03:18:59 +02:00
systemQueue
.on('waiting', (jobId) => systemLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => systemLogger.debug(`active id=${job.id}`))
.on('completed', (job, result) => systemLogger.debug(`completed(${result}) id=${job.id}`))
.on('failed', (job, err) => systemLogger.warn(`failed(${err}) id=${job.id}`, { job, e: renderError(err) }))
.on('error', (job: any, err: Error) => systemLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => systemLogger.warn(`stalled id=${job.id}`));
2019-03-09 01:57:55 +02:00
deliverQueue
2019-03-09 03:18:59 +02:00
.on('waiting', (jobId) => deliverLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => deliverLogger.debug(`active ${getJobInfo(job, true)} to=${job.data.to}`))
.on('completed', (job, result) => deliverLogger.debug(`completed(${result}) ${getJobInfo(job, true)} to=${job.data.to}`))
.on('failed', (job, err) => deliverLogger.warn(`failed(${err}) ${getJobInfo(job)} to=${job.data.to}`))
2019-04-14 05:53:47 +03:00
.on('error', (job: any, err: Error) => deliverLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => deliverLogger.warn(`stalled ${getJobInfo(job)} to=${job.data.to}`));
2019-03-09 03:18:59 +02:00
inboxQueue
.on('waiting', (jobId) => inboxLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => inboxLogger.debug(`active ${getJobInfo(job, true)}`))
.on('completed', (job, result) => inboxLogger.debug(`completed(${result}) ${getJobInfo(job, true)}`))
.on('failed', (job, err) => inboxLogger.warn(`failed(${err}) ${getJobInfo(job)} activity=${job.data.activity ? job.data.activity.id : 'none'}`, { job, e: renderError(err) }))
2019-04-14 05:53:47 +03:00
.on('error', (job: any, err: Error) => inboxLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => inboxLogger.warn(`stalled ${getJobInfo(job)} activity=${job.data.activity ? job.data.activity.id : 'none'}`));
2019-03-09 01:57:55 +02:00
2019-05-27 10:54:47 +03:00
dbQueue
.on('waiting', (jobId) => dbLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => dbLogger.debug(`active id=${job.id}`))
.on('completed', (job, result) => dbLogger.debug(`completed(${result}) id=${job.id}`))
.on('failed', (job, err) => dbLogger.warn(`failed(${err}) id=${job.id}`, { job, e: renderError(err) }))
.on('error', (job: any, err: Error) => dbLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => dbLogger.warn(`stalled id=${job.id}`));
objectStorageQueue
.on('waiting', (jobId) => objectStorageLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => objectStorageLogger.debug(`active id=${job.id}`))
.on('completed', (job, result) => objectStorageLogger.debug(`completed(${result}) id=${job.id}`))
.on('failed', (job, err) => objectStorageLogger.warn(`failed(${err}) id=${job.id}`, { job, e: renderError(err) }))
.on('error', (job: any, err: Error) => objectStorageLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => objectStorageLogger.warn(`stalled id=${job.id}`));
webhookDeliverQueue
.on('waiting', (jobId) => webhookLogger.debug(`waiting id=${jobId}`))
.on('active', (job) => webhookLogger.debug(`active ${getJobInfo(job, true)} to=${job.data.to}`))
.on('completed', (job, result) => webhookLogger.debug(`completed(${result}) ${getJobInfo(job, true)} to=${job.data.to}`))
.on('failed', (job, err) => webhookLogger.warn(`failed(${err}) ${getJobInfo(job)} to=${job.data.to}`))
.on('error', (job: any, err: Error) => webhookLogger.error(`error ${err}`, { job, e: renderError(err) }))
.on('stalled', (job) => webhookLogger.warn(`stalled ${getJobInfo(job)} to=${job.data.to}`));
2021-05-08 12:56:21 +03:00
export function deliver(user: ThinUser, content: unknown, to: string | null) {
2019-03-07 16:07:21 +02:00
if (content == null) return null;
2021-05-08 12:56:21 +03:00
if (to == null) return null;
2019-02-06 08:01:43 +02:00
const data = {
user: {
2021-12-09 16:58:30 +02:00
id: user.id,
},
2019-02-06 08:01:43 +02:00
content,
2021-12-09 16:58:30 +02:00
to,
2019-02-06 08:01:43 +02:00
};
2019-03-07 16:07:21 +02:00
return deliverQueue.add(data, {
attempts: config.deliverJobMaxAttempts || 12,
2021-05-19 10:12:31 +03:00
timeout: 1 * 60 * 1000, // 1min
2019-03-07 16:07:21 +02:00
backoff: {
2021-12-09 16:58:30 +02:00
type: 'apBackoff',
2019-03-07 16:07:21 +02:00
},
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
2018-04-04 17:12:35 +03:00
}
2021-05-08 12:56:21 +03:00
export function inbox(activity: IActivity, signature: httpSignature.IParsedSignature) {
2019-02-06 08:01:43 +02:00
const data = {
activity: activity,
2021-12-09 16:58:30 +02:00
signature,
2019-02-06 08:01:43 +02:00
};
2019-03-07 16:07:21 +02:00
return inboxQueue.add(data, {
attempts: config.inboxJobMaxAttempts || 8,
2021-05-19 10:12:31 +03:00
timeout: 5 * 60 * 1000, // 5min
2019-03-07 16:07:21 +02:00
backoff: {
2021-12-09 16:58:30 +02:00
type: 'apBackoff',
2019-03-07 16:07:21 +02:00
},
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
2018-04-05 17:24:51 +03:00
}
2021-05-08 12:56:21 +03:00
export function createDeleteDriveFilesJob(user: ThinUser) {
2019-03-07 16:27:38 +02:00
return dbQueue.add('deleteDriveFiles', {
2021-12-09 16:58:30 +02:00
user: user,
2019-03-07 16:27:38 +02:00
}, {
2019-03-07 16:07:21 +02:00
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
}
export function createExportCustomEmojisJob(user: ThinUser) {
return dbQueue.add('exportCustomEmojis', {
user: user,
}, {
removeOnComplete: true,
removeOnFail: true,
});
2019-02-20 18:30:21 +02:00
}
2021-05-08 12:56:21 +03:00
export function createExportNotesJob(user: ThinUser) {
2019-03-07 16:27:38 +02:00
return dbQueue.add('exportNotes', {
2021-12-09 16:58:30 +02:00
user: user,
2019-03-07 16:27:38 +02:00
}, {
2019-03-07 16:07:21 +02:00
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
}
2019-02-04 06:35:58 +02:00
2021-12-09 18:22:35 +02:00
export function createExportFollowingJob(user: ThinUser, excludeMuting = false, excludeInactive = false) {
2019-03-07 16:27:38 +02:00
return dbQueue.add('exportFollowing', {
2021-12-09 16:58:30 +02:00
user: user,
2021-12-09 18:22:35 +02:00
excludeMuting,
excludeInactive,
2019-03-07 16:27:38 +02:00
}, {
2019-03-07 16:07:21 +02:00
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
2019-02-06 14:21:49 +02:00
}
2021-05-08 12:56:21 +03:00
export function createExportMuteJob(user: ThinUser) {
2019-03-07 16:27:38 +02:00
return dbQueue.add('exportMute', {
2021-12-09 16:58:30 +02:00
user: user,
2019-03-07 16:27:38 +02:00
}, {
2019-03-07 16:07:21 +02:00
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
2019-02-06 14:21:49 +02:00
}
2021-05-08 12:56:21 +03:00
export function createExportBlockingJob(user: ThinUser) {
2019-03-07 16:27:38 +02:00
return dbQueue.add('exportBlocking', {
2021-12-09 16:58:30 +02:00
user: user,
2019-03-07 16:27:38 +02:00
}, {
2019-03-07 16:07:21 +02:00
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-07 16:07:21 +02:00
});
2019-02-06 14:21:49 +02:00
}
2021-05-08 12:56:21 +03:00
export function createExportUserListsJob(user: ThinUser) {
return dbQueue.add('exportUserLists', {
2021-12-09 16:58:30 +02:00
user: user,
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
});
}
2021-05-08 12:56:21 +03:00
export function createImportFollowingJob(user: ThinUser, fileId: DriveFile['id']) {
2019-03-11 17:34:19 +02:00
return dbQueue.add('importFollowing', {
user: user,
2021-12-09 16:58:30 +02:00
fileId: fileId,
2019-03-11 17:34:19 +02:00
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-11 17:34:19 +02:00
});
}
export function createImportMutingJob(user: ThinUser, fileId: DriveFile['id']) {
return dbQueue.add('importMuting', {
user: user,
2021-12-09 16:58:30 +02:00
fileId: fileId,
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
});
}
export function createImportBlockingJob(user: ThinUser, fileId: DriveFile['id']) {
return dbQueue.add('importBlocking', {
user: user,
2021-12-09 16:58:30 +02:00
fileId: fileId,
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
});
}
2021-05-08 12:56:21 +03:00
export function createImportUserListsJob(user: ThinUser, fileId: DriveFile['id']) {
2019-03-11 12:43:58 +02:00
return dbQueue.add('importUserLists', {
user: user,
2021-12-09 16:58:30 +02:00
fileId: fileId,
2019-03-11 12:43:58 +02:00
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-03-11 12:43:58 +02:00
});
}
2022-01-12 17:48:58 +02:00
export function createImportCustomEmojisJob(user: ThinUser, fileId: DriveFile['id']) {
return dbQueue.add('importCustomEmojis', {
user: user,
fileId: fileId,
}, {
removeOnComplete: true,
removeOnFail: true,
});
}
2021-09-23 16:41:02 +03:00
export function createDeleteAccountJob(user: ThinUser, opts: { soft?: boolean; } = {}) {
return dbQueue.add('deleteAccount', {
user: user,
2021-12-09 16:58:30 +02:00
soft: opts.soft,
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
});
}
2019-05-27 10:54:47 +03:00
export function createDeleteObjectStorageFileJob(key: string) {
return objectStorageQueue.add('deleteFile', {
2021-12-09 16:58:30 +02:00
key: key,
2019-05-27 10:54:47 +03:00
}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
2019-05-27 10:54:47 +03:00
});
}
export function createCleanRemoteFilesJob() {
return objectStorageQueue.add('cleanRemoteFiles', {}, {
removeOnComplete: true,
2021-12-09 16:58:30 +02:00
removeOnFail: true,
});
}
2022-04-03 16:36:30 +03:00
export function webhookDeliver(webhook: Webhook, type: typeof webhookEventTypes[number], content: unknown) {
const data = {
2022-04-03 16:36:30 +03:00
type,
content,
webhookId: webhook.id,
to: webhook.url,
secret: webhook.secret,
2022-04-03 16:36:30 +03:00
createdAt: Date.now(),
eventId: uuid(),
};
return webhookDeliverQueue.add(data, {
attempts: 4,
timeout: 1 * 60 * 1000, // 1min
backoff: {
type: 'apBackoff',
},
removeOnComplete: true,
removeOnFail: true,
});
}
2019-02-04 06:35:58 +02:00
export default function() {
if (envOption.onlyServer) return;
deliverQueue.process(config.deliverJobConcurrency || 128, processDeliver);
inboxQueue.process(config.inboxJobConcurrency || 16, processInbox);
endedPollNotificationQueue.process(endedPollNotification);
webhookDeliverQueue.process(64, processWebhookDeliver);
processDb(dbQueue);
2022-02-08 15:18:39 +02:00
processObjectStorage(objectStorageQueue);
2022-02-10 10:45:12 +02:00
systemQueue.add('tickCharts', {
}, {
repeat: { cron: '55 * * * *' },
removeOnComplete: true,
2022-02-10 10:45:12 +02:00
});
systemQueue.add('resyncCharts', {
}, {
2021-12-09 16:58:30 +02:00
repeat: { cron: '0 0 * * *' },
});
systemQueue.add('cleanCharts', {
}, {
repeat: { cron: '0 0 * * *' },
});
2022-03-04 13:23:53 +02:00
systemQueue.add('checkExpiredMutings', {
}, {
repeat: { cron: '*/5 * * * *' },
removeOnComplete: true,
2022-03-04 13:23:53 +02:00
});
processSystemQueue(systemQueue);
2019-02-04 06:35:58 +02:00
}
2019-02-06 08:24:59 +02:00
export function destroy() {
2019-03-07 16:36:08 +02:00
deliverQueue.once('cleaned', (jobs, status) => {
2019-03-09 03:18:59 +02:00
deliverLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
2019-03-07 16:36:08 +02:00
});
2019-03-14 14:51:33 +02:00
deliverQueue.clean(0, 'delayed');
2019-03-07 16:36:08 +02:00
inboxQueue.once('cleaned', (jobs, status) => {
2019-03-09 03:18:59 +02:00
inboxLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
2019-03-07 16:36:08 +02:00
});
2019-03-14 14:51:33 +02:00
inboxQueue.clean(0, 'delayed');
2019-02-06 08:24:59 +02:00
}