summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/queue/jobs.ts
blob: 5ab78e3c4843dfe8427623d332c2699cb8f7984e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import $ from 'cafy';
import define from '../../../define';
import { deliverQueue, inboxQueue, dbQueue, objectStorageQueue } from '../../../../../queue';

export const meta = {
	desc: {
		'ja-JP': 'ジョブ一覧を表示します。',
		'en-US': 'Display the job list.'
	},

	tags: ['admin'],

	requireCredential: true as const,
	requireModerator: true,

	params: {
		domain: {
			validator: $.str.or(['deliver', 'inbox', 'db', 'objectStorage']),
		},

		state: {
			validator: $.str.or(['active', 'waiting', 'delayed']),
		},

		limit: {
			validator: $.optional.num,
			default: 50
		},
	},

	res: {
		type: 'array' as const,
		optional: false as const, nullable: false as const,
		items: {
			type: 'object' as const,
			optional: false as const, nullable: false as const,
			properties: {
				id: {
					type: 'string' as const,
					optional: false as const, nullable: false as const,
					format: 'id'
				},
				data: {
					type: 'object' as const,
					optional: false as const, nullable: false as const
				},
				attempts: {
					type: 'number' as const,
					optional: false as const, nullable: false as const
				},
				maxAttempts: {
					type: 'number' as const,
					optional: false as const, nullable: false as const
				},
				timestamp: {
					type: 'number' as const,
					optional: false as const, nullable: false as const
				}
			}
		}
	}
};

export default define(meta, async (ps) => {
	const queue =
		ps.domain === 'deliver' ? deliverQueue :
		ps.domain === 'inbox' ? inboxQueue :
		ps.domain === 'db' ? dbQueue :
		ps.domain === 'objectStorage' ? objectStorageQueue :
		null as never;

	const jobs = await queue.getJobs([ps.state], 0, ps.limit!);

	return jobs.map(job => {
		const data = job.data;
		delete data.content;
		delete data.user;
		return {
			id: job.id,
			data,
			attempts: job.attemptsMade,
			maxAttempts: job.opts ? job.opts.attempts : 0,
			timestamp: job.timestamp,
		};
	});
});