Viktor
Viktor

Reputation: 3080

How to run server code on a single container

I have two containers in my meteor app on galaxy servers. I have some background jobs that I want to be executing only on a single container to avoid duplication.

What's the easiest way to achieve this? Is there some procId or the like that I can retrieve during runtime?

Upvotes: 2

Views: 79

Answers (2)

evolross
evolross

Reputation: 533

I was thinking about this more... one solution (which I'm kind of borrowing from meteor-migrations) is to make a simple database collection/entry that holds a startupCodeHasRun flag. Then in a Meteor.startup() block you can check if the flag has been set, if not, set the flag and run the code. This would cause the code to only be run once on only one of your containers that share the same database.

The gotcha is you would have to reset this flag manually before redeploying or else the code would never be run again on redeploy.

Not an ideal solution but could work. This is the same way the above database migrations package works in a multi-container environment. And since it's a one-time database migration, you don't have to worry about redeploy.

Upvotes: 0

Mikkel
Mikkel

Reputation: 7777

If the two servers have their own settings files, you could use a setting to nominate one of the servers as the one that does the background jobs.

There is a package called node-cron that can be used for setting up regular jobs https://www.npmjs.com/package/node-cron

Use a Meteor.startup method to look at the settings file, and if it is the designated server, it can schedule the jobs for itself.

Another technique is to work out the ID of each server, and have database entry containing the id of the nominated server.

On checking further, https://github.com/percolatestudio/meteor-synced-cron supports multiple servers, which should do what you need :)

Upvotes: 1

Related Questions