-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path+handler.ts
51 lines (46 loc) · 1.29 KB
/
+handler.ts
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
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import useQueue from '~/composables/useQueue';
import useWorker from '~/composables/useWorker';
export default (async (app) => {
/*
$ curl --request GET \
--url http://127.0.0.1:3000/api/queues/cron?color=blue
*/
app.get(
'',
{
schema: {
querystring: Type.Object({ color: Type.String() }),
response: { 200: Type.Object({ message: Type.String() }) },
},
},
async (req, reply) => {
// worker.js {%
const paintRenewQueue = useQueue('PaintRenew');
await paintRenewQueue.add(
'wall',
{ color: req.query.color },
{
removeOnComplete: true,
repeat: { pattern: '45 * * * * *' },
},
);
useWorker(
'PaintRenew',
async (job) => {
console.log('[PaintRenew] Starting job:', job.name);
console.log(job.id, job.name, job.data);
console.log('[PaintRenew] Finished job:', job.name);
return;
},
{
removeOnComplete: { count: 0 },
removeOnFail: { count: 0 },
},
);
// %}
return reply.send({ message: 'OK' });
},
);
}) as FastifyPluginAsyncTypebox;