Question: How can I handle timezones in BullMQ?


BullMQ, an advanced queue-based job and message system for Node.js, does not inherently understand or manage timezones. It schedules jobs based on the server's local time by default. However, you can manage timezones manually, typically by converting all dates/times to UTC before scheduling jobs.

When you create a job that needs to be run at a specific time considering timezone, ensure that the date/time you set is in UTC. JavaScript's Date object uses the local server's timezone when creating new instances but can be easily converted to UTC.

Here an example:

var Queue = require('bullmq').Queue; var myQueue = new Queue('myQueue'); // Get current date in UTC var now = new Date(); var utcNow = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); // Schedule job after an hour in UTC var anHourLater = new Date(utcNow.getTime() + 60 * 60 * 1000); myQueue.add( 'myJob', { foo: 'bar' }, { delay: anHourLater.getTime() - utcNow.getTime() } );

In this code, we get the current time in UTC and schedule a job to be processed an hour later (also in UTC). The delay is calculated as the difference between the UTC times of anHourLater and utcNow.

If you're dealing with user-specific timezones, convert the user's local time to UTC when scheduling jobs. Libraries like Moment.js or Luxon might help with these conversions.

Remember, when working with timezones, it's usually best to convert and store times in UTC, then convert to the user's local timezone when displaying times to the user.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.