dev@cloudburo

Meteor Small Hints #5: Server Code Fragments executed twice on Startup

Recently I got confused with my Meteor application, by having the server code executed twice at startup. So the code was initialising my application twice, as well as duplicated for example my Slack Sysadmin Bot running as part of the instance.

I was first thinking that it has something to do with my IntelliJ Webstorm IDE running in debug mode, but also the local test-, as well the my Modulus remote environment was showing the same symptom.

After some hours of debugging I luckily found the following Github issue entry which showed me the right direction.

As part of my application I introduced Differential meteor-workers, which allows to spawn headless worker meteor processes to work on async jobs. For example, passing a message to my Slack SysAdmin Bot is done via Job.


  class @SlackSysAdminBotJob extends Job 
    handleJob: ()-> 
      if @params.action == "sendMessage" 
        @params.bot.postTo @params.name, @params.text 


Such a async Job can be easily triggered, like shown here (which will hand the work over to a worker process.


    Job.push new SlackSysAdminBotJob 
          action: "sendMessage" 
          slackBot: myBot 
          text:txt 
          name: channel 
        , (error, job) -> 
           # Check it 


Now introducing the Differential async worker resulted in the problem, that not only the master process, BUT also the worker process will be initialised with each Server startup code fragment. Which is not what we want.

So I had to extend each server Meter.startup code fragment by introducing an isClusterMaster check


  exports.Plattform = 
      isClusterMaster: () -> 
        return Meteor.isServer && Npm.require('cluster').isMaster 



As for example:


  Meteor.startup () -> 
    if Plattform.isClusterMaster() 
      # The startup code 


Now the startup initialization will be only executed on the master in the cluster and not for each worker process.

comments powered by Disqus