Prinzhorn
Prinzhorn

Reputation: 22508

Synchronize multiple node.js dynos

I'm planning to host an express app on Heroku (I'm already experimenting with a single dyno).

I want to use node-cron for maintenance tasks (doing some MongoDB updates). The question is, what's the simplest way to make sure the maintenance only runs once? All dynos would try to run the maintenance at the same time.

My current approach uses MongoDB's atomic upserts as some sort of semaphore (every dyno tries to set the flag for the current maintenance). But that's kinda ugly.

I'd like to not have a separate worker instance since it's really just a simple task that needs to be run once a day.

Upvotes: 1

Views: 533

Answers (1)

Stefan
Stefan

Reputation: 3402

I think that's what the Heroku Scheduler is good for (https://devcenter.heroku.com/articles/scheduler). If the execution of your update isn't taking too long it's the way to go. You write a JS script (e.g. schedule.js) that knows how to update your MongoDB, put it in your root and schedule it using the Heroku Scheduler (it comes with a trivial frontend) to invoke it (node scheduler.js) at the time desired.

Upvotes: 1

Related Questions