Skip to content
Snippets Groups Projects
index.ts 6.56 KiB
Newer Older
syuilo's avatar
syuilo committed
import * as httpSignature from 'http-signature';
import config from '@/config/index';
import { program } from '../argv';
syuilo's avatar
syuilo committed

import processDeliver from './processors/deliver';
import processInbox from './processors/inbox';
import processDb from './processors/db/index';
import procesObjectStorage from './processors/object-storage/index';
import { queueLogger } from './logger';
import { DriveFile } from '@/models/entities/drive-file';
import { getJobInfo } from './get-job-info';
import { dbQueue, deliverQueue, inboxQueue, objectStorageQueue } from './queues';
import { ThinUser } from './types';
import { IActivity } from '@/remote/activitypub/type';
syuilo's avatar
syuilo committed
function renderError(e: Error): any {
	return {
MeiMei's avatar
MeiMei committed
		stack: e?.stack,
		message: e?.message,
		name: e?.name
syuilo's avatar
syuilo committed
	};
}

syuilo's avatar
syuilo committed
const deliverLogger = queueLogger.createSubLogger('deliver');
const inboxLogger = queueLogger.createSubLogger('inbox');
syuilo's avatar
syuilo committed
const dbLogger = queueLogger.createSubLogger('db');
const objectStorageLogger = queueLogger.createSubLogger('objectStorage');
syuilo's avatar
syuilo committed

deliverQueue
syuilo's avatar
syuilo committed
	.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}`))
syuilo's avatar
syuilo committed
	.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}`));
syuilo's avatar
syuilo committed

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) }))
syuilo's avatar
syuilo committed
	.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'}`));
syuilo's avatar
syuilo committed
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}`));

MeiMei's avatar
MeiMei committed
export function deliver(user: ThinUser, content: unknown, to: string | null) {
syuilo's avatar
syuilo committed
	if (content == null) return null;
MeiMei's avatar
MeiMei committed
	if (to == null) return null;
syuilo's avatar
syuilo committed

	const data = {
		user: {
			id: user.id
		},
syuilo's avatar
syuilo committed
		content,
		to
	};

syuilo's avatar
syuilo committed
	return deliverQueue.add(data, {
		attempts: config.deliverJobMaxAttempts || 12,
MeiMei's avatar
MeiMei committed
		timeout: 1 * 60 * 1000,	// 1min
syuilo's avatar
syuilo committed
		backoff: {
			type: 'apBackoff'
syuilo's avatar
syuilo committed
		},
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
wip
syuilo committed
}

MeiMei's avatar
MeiMei committed
export function inbox(activity: IActivity, signature: httpSignature.IParsedSignature) {
syuilo's avatar
syuilo committed
	const data = {
		activity: activity,
		signature
	};
syuilo's avatar
syuilo committed
	return inboxQueue.add(data, {
		attempts: config.inboxJobMaxAttempts || 8,
MeiMei's avatar
MeiMei committed
		timeout: 5 * 60 * 1000,	// 5min
syuilo's avatar
syuilo committed
		backoff: {
			type: 'apBackoff'
syuilo's avatar
syuilo committed
		},
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed
}
MeiMei's avatar
MeiMei committed
export function createDeleteDriveFilesJob(user: ThinUser) {
syuilo's avatar
syuilo committed
	return dbQueue.add('deleteDriveFiles', {
syuilo's avatar
syuilo committed
		user: user
syuilo's avatar
syuilo committed
	}, {
syuilo's avatar
syuilo committed
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed
}

MeiMei's avatar
MeiMei committed
export function createExportNotesJob(user: ThinUser) {
syuilo's avatar
syuilo committed
	return dbQueue.add('exportNotes', {
syuilo's avatar
syuilo committed
	}, {
syuilo's avatar
syuilo committed
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed

MeiMei's avatar
MeiMei committed
export function createExportFollowingJob(user: ThinUser) {
syuilo's avatar
syuilo committed
	return dbQueue.add('exportFollowing', {
syuilo's avatar
syuilo committed
		user: user
syuilo's avatar
syuilo committed
	}, {
syuilo's avatar
syuilo committed
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed
}

MeiMei's avatar
MeiMei committed
export function createExportMuteJob(user: ThinUser) {
syuilo's avatar
syuilo committed
	return dbQueue.add('exportMute', {
syuilo's avatar
syuilo committed
		user: user
syuilo's avatar
syuilo committed
	}, {
syuilo's avatar
syuilo committed
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed
}

MeiMei's avatar
MeiMei committed
export function createExportBlockingJob(user: ThinUser) {
syuilo's avatar
syuilo committed
	return dbQueue.add('exportBlocking', {
syuilo's avatar
syuilo committed
		user: user
syuilo's avatar
syuilo committed
	}, {
syuilo's avatar
syuilo committed
		removeOnComplete: true,
		removeOnFail: true
	});
syuilo's avatar
syuilo committed
}

MeiMei's avatar
MeiMei committed
export function createExportUserListsJob(user: ThinUser) {
	return dbQueue.add('exportUserLists', {
		user: user
	}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

MeiMei's avatar
MeiMei committed
export function createImportFollowingJob(user: ThinUser, fileId: DriveFile['id']) {
syuilo's avatar
syuilo committed
	return dbQueue.add('importFollowing', {
		user: user,
		fileId: fileId
	}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

MeiMei's avatar
MeiMei committed
export function createImportUserListsJob(user: ThinUser, fileId: DriveFile['id']) {
syuilo's avatar
syuilo committed
	return dbQueue.add('importUserLists', {
		user: user,
		fileId: fileId
	}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

export function createDeleteAccountJob(user: ThinUser) {
	return dbQueue.add('deleteAccount', {
		user: user
	}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

syuilo's avatar
syuilo committed
export function createDeleteObjectStorageFileJob(key: string) {
	return objectStorageQueue.add('deleteFile', {
		key: key
	}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

export function createCleanRemoteFilesJob() {
	return objectStorageQueue.add('cleanRemoteFiles', {}, {
		removeOnComplete: true,
		removeOnFail: true
	});
}

syuilo's avatar
syuilo committed
export default function() {
syuilo's avatar
syuilo committed
	if (!program.onlyServer) {
MeiMei's avatar
MeiMei committed
		deliverQueue.process(config.deliverJobConcurrency || 128, processDeliver);
		inboxQueue.process(config.inboxJobConcurrency || 16, processInbox);
syuilo's avatar
syuilo committed
		processDb(dbQueue);
syuilo's avatar
syuilo committed
		procesObjectStorage(objectStorageQueue);
syuilo's avatar
syuilo committed
	}
}
syuilo's avatar
syuilo committed

export function destroy() {
syuilo's avatar
syuilo committed
	deliverQueue.once('cleaned', (jobs, status) => {
syuilo's avatar
syuilo committed
		deliverLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
syuilo's avatar
syuilo committed
	});
syuilo's avatar
syuilo committed
	deliverQueue.clean(0, 'delayed');
syuilo's avatar
syuilo committed

	inboxQueue.once('cleaned', (jobs, status) => {
syuilo's avatar
syuilo committed
		inboxLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
syuilo's avatar
syuilo committed
	});
syuilo's avatar
syuilo committed
	inboxQueue.clean(0, 'delayed');
syuilo's avatar
syuilo committed
}