Maciej Kravchyk
Maciej Kravchyk

Reputation: 16647

Node.js/Koa2 server - execute a job minutes after request

I have a Koa2/Node.js app (using async/await) and I want to execute a job X minutes after request, where X is a random number of minutes ranging from 20 - 100 (I want to use it to send automatic welcome emails to users who sign up and make it appear like it was sent personally by me).

So can I just use setTimeout to do this, is it reasonable to set a timer for 200 minutes? Of course, if my app crashes, the email wouldn't be sent, but I will track all signups in database, so in a rare case of crash I will send the email myself.

Upvotes: 1

Views: 440

Answers (1)

Damaged Organic
Damaged Organic

Reputation: 8467

Command queue such as rabbitMQ or wraps around Redis (with pub/sub or SETEX) seems like an overhead for such simple task.

Timeouts for relatively long periods with potentially N requests do feel a bit uncomfortable, but Node.js setTimout() is actually a wrapper around uv_timer function in libuv which is designed to handle a huge amount of timers over the underlying OS notification mechanism.

Still, to keep things as simple as possible I would use a good old cron job with some wrapper library which hides exact implementation. With something like node-schedule it could be done in just few lines of code.

const schedule = require('node-schedule');

const getRandomMsAmount = (from, to) => {
  const minutes = Math.floor(Math.random() * to) + from;
  return minutes * 60 * 1000;
};

const getDateAfterMs = (ms) => {
  const nowTimestamp = Number(new Date());
  return new Date(nowTimestamp + ms);
};

const dueDate = getDateAfterMs(getRandomMsAmount(20, 100));
const emailJob = schedule.scheduleJob(dueDate, () => {
  // logging, updating database, etc.
});

If something goes wrong during processing, referenced job can be cancelled:

emailJob.cancel();

I hope your email contains random content as well, because even single user can get multiple messages in case of different signup attempts - so this delay could become just an annoying experience.

Upvotes: 3

Related Questions