Skip to content

Commit

Permalink
266th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Feb 21, 2024
1 parent b9deffa commit c47c8c8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 46 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,21 @@ This seed repository provides the following features:

### Tiny examples

- [x] [Hello World](./src/routes/hello-world)
- [x] [Hello World](./src/routes/hello-world/+handler.ts)
- [x] [CRUD Operations](./src/routes/todos)
- [x] [JWT Authentication](./src/routes/auth)
- [x] [One-time Password](./src/routes/auth/otp)
- [x] [File Uploads](./src/routes/file-uploads)
- [x] [Real-time Data](./src/routes/echo)
- [x] [Real-time Updates](./src/routes/sse)
- [x] [Sending Emails](./src/routes/email)
- [x] [JWT Authentication](./src/routes/auth/+handler.ts)
- [x] [One-time Password](./src/routes/auth/otp/+handler.ts)
- [x] [File Uploads](./src/routes/file-uploads/+handler.ts)
- [x] [Real-time Data](./src/routes/echo/+handler.ts)
- [x] [Real-time Updates](./src/routes/sse/+handler.ts)
- [x] [Sending Emails](./src/routes/email/+handler.ts)
- [x] [Internationalization](./src/routes/hello-i18n)
- [x] [Background Workers](./src/routes/queues)
- [x] [Cron Jobs](./src/routes/queues)
- [x] [Caching](./src/routes/hello-world/caching-memory)
- [x] [Cache Deduplication](./src/routes/hello-world/caching-dedupe/[id]-memory)
- [x] [Background Workers](./src/routes/queues/+handler.ts)
- [x] [Cron Jobs](./src/routes/queues/cron/+handler.ts)
- [x] [Caching](./src/routes/hello-world/caching-memory/+handler.ts)
- [x] [Caching in Redis](./src/routes/hello-world/caching-redis/+handler.ts)
- [x] [Cache Deduplication](./src/routes/hello-world/caching-dedupe/[id]-memory/+handler.ts)
- [ ] [Cache Deduplication in Redis](./src/routes/hello-world/caching-dedupe/[id]-redis/+handler.ts)

## Configuration

Expand Down
52 changes: 17 additions & 35 deletions src/routes/queues/+handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { Type } from '@sinclair/typebox';
import useQueue from '~/composables/useQueue';
import useWorker from '~/composables/useWorker';

const paintQueue = useQueue('Paint');

export default (async (app) => {
/*
curl --request GET \
--url http://127.0.0.1:3000/api/queues?color=blue
$ curl --request GET \
--url http://127.0.0.1:3000/api/queues?color=blue
*/
app.get(
'',
Expand All @@ -20,47 +18,31 @@ export default (async (app) => {
},
},
async (req, reply) => {
const paintQueue = useQueue('Paint');

await paintQueue.add(
'wall',
{ color: req.query.color },
{
removeOnComplete: true,
// repeat: { pattern: '45 * * * * *' },
},
);

return reply.send({ message: 'OK' });
},
);
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 },
},
);

/*
curl --request GET \
--url http://127.0.0.1:3000/api/queues/drain
*/
app.get(
'/drain',
{
schema: {
response: { 200: Type.Object({ message: Type.String() }) },
},
},
async (req, reply) => {
await paintQueue.drain();
return reply.send({ message: 'OK' });
},
);
}) as FastifyPluginAsyncTypebox;

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 },
},
);
49 changes: 49 additions & 0 deletions src/routes/queues/cron/+handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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) => {
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;
24 changes: 24 additions & 0 deletions src/routes/queues/drain/+handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';

import useQueue from '~/composables/useQueue';

export default (async (app) => {
/*
$ curl --request GET \
--url http://127.0.0.1:3000/api/queues/drain
*/
app.get(
'',
{
schema: {
response: { 200: Type.Object({ message: Type.String() }) },
},
},
async (req, reply) => {
const paintRenewQueue = useQueue('PaintRenew');
await paintRenewQueue.drain();
return reply.send({ message: 'OK' });
},
);
}) as FastifyPluginAsyncTypebox;

0 comments on commit c47c8c8

Please sign in to comment.