0

I have a custom sails.js hook which implements a Websocket server (we can't use the built-in socket.js hook because we needed to match an old API).

I also have a custom sails run script that does background processing.

Right now, when the sails run -name-of-my-script command is run, it also runs my hook which makes extra listeners for all of the events used by this hook.

I would like to make it that the hook only starts on a main application startup (sails lift or the equivalent node app.js), not on sails run ....

I have looked through the code and the documentation, but I can't seem to see any official way to do this.

I can think of two options:

  1. In the hook, check for whether a script is being run and don't initialize.
  2. In the script, disable the hook.

Is there any way to do either of those things?

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116

1 Answers1

0

Right now, there is no built-in way to do this because Sails scripts have no way to modify configuration - they use the default sails instance with its default settings.

Here is the hack that I used to make it work:

  1. Add a check for a configuration option in the hook's initialize method:

    if (app.config.notifier.active === false) {
        sails.log.verbose('Notifier disabled');
        return cb();
    }
    
    // Continue with notifier hook setup here
    
  2. Add setting an environment variable to the top of the script (before the module.exports:

    process.env.sails_notifier__active = false;
    

(Note: Step 1 is based on this answer but the package linked there no longer uses this technique. That question and answer are also extremely old -- at least as Sails.js answers go -- from before sails run existed.)

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116