Only run my sails.js hook on `sails lift`, not `sails run`

Moshe Katz

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

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.)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Grunt error and blank page on `sails lift`

分類Dev

AngularJS + sails.js

分類Dev

RabbitMQとSails.js

分類Dev

Sails JS Model Rest API

分類Dev

Sails js Error in file Upload

分類Dev

SailsJS - using sails.io.js with JWT

分類Dev

Create subfolders in Sails.js /api/controllers

分類Dev

Sails.js / waterline:groupBy + count

分類Dev

Optional file uploads with Sails.js

分類Dev

Windows8のSails.js

分類Dev

Sails.js RESTAPI検証

分類Dev

How to use Validations in sails.js?

分類Dev

喫水線(Sails.js):AND条件

分類Dev

Sails Js view engine with layout sections support?

分類Dev

Sails.js route redirect with a custom policy

分類Dev

Assets not loading in sails.js app

分類Dev

Sails.js - Postgresql Adapter multiple schemas

分類Dev

Sails js SSH DB接続

分類Dev

Sails.js の find() 日付

分類Dev

Sails.Js-sails.Jsでページ付けを行う方法

分類Dev

Sails js: How can I define two MySQL connections to two different databases in a single model in Sails js?

分類Dev

sails.js-hook-scheddule-cronタイミング文字列を指定する方法

分類Dev

Sails.js APIpassport.js認証

分類Dev

Sails JS + Angular JS:ルーティング

分類Dev

Sails.js : post text input and a file in the same time

分類Dev

Automatically append session information to model in Sails.js

分類Dev

Bcrypt: invalid ELF header with Docker and Sails.JS

分類Dev

Sails js restapiの日付範囲

分類Dev

Starting Sails.js inside a Docker container causes a Grunt error