- Notifications
You must be signed in to change notification settings - Fork1
Background task queue for TypeScript backed by Redis, a super minimal Celery
License
wakatime/wakaq-ts
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Background task queue for TypeScript backed by Redis, a super minimal Celery.
For the original Python version, seeWakaQ for Python.
- Queue priority
- Delayed tasks (run tasks after a duration eta)
- Scheduled periodic tasks
- Broadcast a task to all workers
- Tasksoft andhard timeout limits
- Optionally retry tasks on soft timeout
- Combat memory leaks with
maxMemPercent
ormaxTasksPerWorker
- Super minimal
Want more features like rate limiting, task deduplication, etc? Too bad, feature PRs are not accepted. Maximal features belong in your app’s worker tasks.
npm i --save wakaq
app.ts
import{Duration}from'ts-duration';import{CronTask,WakaQ,WakaQueue,WakaQWorker}from'wakaq';import{z}from'zod';import{prisma}from'./db';exportconstwakaq=newWakaQ({/* Raise SoftTimeout in a task if it runs longer than 14 minutes. Can also be set per task or queue. If no soft timeout set, tasks can run forever. */softTimeout:Duration.minute(14),/* SIGKILL a task if it runs longer than 15 minutes. Can also be set per queue or when enqueuing a task. */hardTimeout:Duration.minute(15),/* Number of worker processes. Must be an int or str which evaluates to an int. The variable "cores" is replaced with the number of processors on the current machine. */concurrency:'cores*4',/* List your queues and their priorities. */queues:[newWakaQueue('high priority'),newWakaQueue('default'),],/* Redis normally doesn't use TLS, but some cloud providers need it. */tls:process.env.NODE_ENV=='production' ?{cert:'',key:''} :undefined,/* If the task soft timeouts, retry up to 3 times. Max retries comes first from the task decorator if set, next from the Queue's maxRetries, lastly from the option below. If No maxRetries is found, the task is not retried on a soft timeout. */maxRetries:3,/* Schedule two tasks, the first runs every minute, the second once every ten minutes. To run scheduled tasks you must keep `npm run scheduler` running as a daemon. */schedules:[// Runs myTask once every 5 minutes.newCronTask('*/5 * * * *','myTask'),],});exportconstcreateUserInBackground=wakaq.task(async(firstName:string)=>{constname=z.string().safeParse(firstName);if(!result.success){thrownewError(result.error.message);}awaitprisma.User.create({data:{firstName:result.data},});},{name:'createUserInBackground'},);
Add these scripts to yourpackage.json
:
{"scripts": {"worker":"tsx scripts/wakaqWorker.ts","scheduler":"tsx scripts/wakaqScheduler.ts","info":"tsx scripts/wakaqInfo.ts","purge":"tsx scripts/wakaqPurge.ts" }}
Create these files in yourscripts
folder:
scripts/wakaqWorker.ts
import{WakaQWorker}from'wakaq';import{wakaq}from'../app.js';// Can't use tsx directly because it breaks IPC (https://github.com/esbuild-kit/tsx/issues/201)awaitnewWakaQWorker(wakaq,['node','--no-warnings=ExperimentalWarning','--import','tsx','scripts/wakaqChild.ts']).start();process.exit(0);
scripts/wakaqScheduler.ts
import{WakaQScheduler}from'wakaq';import{wakaq}from'../app.js';awaitnewWakaQScheduler(wakaq).start();process.exit(0);
scripts/wakaqChild.ts
import{WakaQChildWorker}from'wakaq';import{wakaq}from'../app.js';// import your tasks so they're registered// also make sure to enable tsc option verbatimModuleSyntaxawaitnewWakaQChildWorker(wakaq).start();process.exit(0);
scripts/wakaqInfo.ts
import{inspect}from'wakaq';import{wakaq}from'../app.js';console.log(JSON.stringify(awaitinspect(awaitwakaq.connect()),null,2));wakaq.disconnect();
scripts/wakaqPurge.ts
import{numPendingTasksInQueue,numPendingEtaTasksInQueue,purgeQueue,purgeEtaQueue}from'wakaq';import{wakaq}from'../app.js';constqueueName=process.argv.slice(2)[0];constqueue=wakaq.queuesByName.get(queueName??'');if(!queue){thrownewError(`Queue not found:${queueName}`);}awaitwakaq.connect();letcount=awaitnumPendingTasksInQueue(wakaq,queue);awaitpurgeQueue(wakaq,queue);count+=awaitnumPendingEtaTasksInQueue(wakaq,queue);awaitpurgeEtaQueue(wakaq,queue);console.log(`Purged${count} tasks from${queue.name}`);wakaq.disconnect();
After runningnpm run worker
when you runcreateUserInBackground.enqueue('alan')
your task executes in the background on the worker server.
See theWakaQ init params for a full list of options, like Redis host and Redis socket timeout values.
When using in production, make sure toincrease the max open ports allowed for your Redis server process.
When using eta tasks a Redis sorted set is used, so eta tasks are automatically deduped based on task name, args, and kwargs.If you want multiple pending eta tasks with the same arguments, just add a throwaway random string or uuid to the task’s args.
Here’s an example systemd config to runwakaq worker
as a daemon:
[Unit]Description=WakaQ Worker Service[Service]WorkingDirectory=/opt/yourappExecStart=npm run workerRemainAfterExit=noRestart=alwaysRestartSec=30sKillSignal=SIGINTLimitNOFILE=99999[Install]WantedBy=multi-user.target
Create a file at/etc/systemd/system/wakaqworker.service
with the above contents, then run:
systemctl daemon-reload && systemctl enable wakaqworker
About
Background task queue for TypeScript backed by Redis, a super minimal Celery