Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How can I handle timezones in BullMQ?

Answer

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?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost