-
Notifications
You must be signed in to change notification settings - Fork 18
/
+handler.ts
52 lines (47 loc) · 1.3 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
52
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/delay?color=blue
*/
app.get(
'',
{
schema: {
querystring: Type.Object({ color: Type.String() }),
response: { 200: Type.Object({ message: Type.String() }) },
},
},
async (req, reply) => {
const paintQueue = useQueue('Paint');
// Add a job that will be delayed by at least 3 seconds
await paintQueue.add(
'wall',
{ color: req.query.color },
{
removeOnComplete: true,
delay: 3000,
},
);
// worker.js {%
useWorker(
'Paint',
async (job) => {
console.log('[Paint] Starting job:', job.name);
console.log(job.id, job.name, job.data);
console.log('[Paint] Finished job:', job.name);
return;
},
{
removeOnComplete: { count: 0 },
removeOnFail: { count: 0 },
},
);
// %}
return reply.send({ message: 'OK' });
},
);
}) as FastifyPluginAsyncTypebox;