summaryrefslogtreecommitdiff
path: root/src/queue/index.ts
blob: cf8af17a48a1f18c5fafe55b468f768c86c919c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as Queue from 'bee-queue';
import config from '../config';

import { ILocalUser } from '../models/user';
import { program } from '../argv';
import handler from './processors';

const enableQueue = config.redis != null && !program.disableQueue;

const queue = initializeQueue();

function initializeQueue() {
	if (enableQueue) {
		return new Queue('misskey', {
			redis: {
				port: config.redis.port,
				host: config.redis.host,
				password: config.redis.pass
			},

			removeOnSuccess: true,
			removeOnFailure: true,
			getEvents: false,
			sendEvents: false,
			storeJobs: false
		});
	} else {
		return null;
	}
}

export function createHttpJob(data: any) {
	if (enableQueue) {
		return queue.createJob(data)
			.retries(4)
			.backoff('exponential', 16384) // 16s
			.save();
	} else {
		return handler({ data }, () => {});
	}
}

export function deliver(user: ILocalUser, content: any, to: any) {
	if (content == null) return;

	createHttpJob({
		type: 'deliver',
		user,
		content,
		to
	});
}

export function createExportNotesJob(user: ILocalUser) {
	if (!enableQueue) throw 'queue disabled';

	return queue.createJob({
		type: 'exportNotes',
		user: user
	})
		.save();
}

export default function() {
	if (enableQueue) {
		queue.process(128, handler);
	}
}