22

i am using two database adapters with sails.

one for mondoDB and second for mysql.whenever i run command "sails lift".once it gives an error

error: Error: The hook `orm` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set       `sails.config.orm._hookTimeout to a higher value (currently 20000)
at tooLong [as _onTimeout]   (C:\Users\KAMI\AppData\Roaming\npm\node_modules\sails\lib\app\private\loadHooks.js:92:21)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15

when i rerun sails without changes it gives no error then.how can i avoid this error everytime.this is my 1st experience with sailsjs so any help will be apreciated....

N.A
  • 855
  • 4
  • 13
  • 34
  • what version of sails, how many models do you have? Post some code. this is not enough info. – Travis Webb Feb 17 '15 at 16:16
  • sails version is 0.11.0.and i've posted console's output.what else is needed? – N.A Feb 17 '15 at 17:04
  • How many models do you have? – Travis Webb Feb 18 '15 at 04:50
  • i am using two db adpterz one for mysql which is having 5 models n one for mongoDB which contains two models chat and user.actually i shifted my project from node to sails.so why orm for mongodb sometimes create problem. – N.A Feb 18 '15 at 13:58
  • easiest fix is to add a file called hookTimeout.js under `config/` that exports `hookTimeout: value` where `value` is number of milliseconds – Paulo Dec 13 '17 at 12:20

8 Answers8

40

I ran into this problem last night because of a slow internet connection between my laptop and the DB server. My solution was to create a new file in the config directory called orm.js (name doesn't really matter).

Then add the following code:

// config/orm.js
module.exports.orm = {
  _hookTimeout: 60000 // I used 60 seconds as my new timeout
};

I also found I had to change my pubsub timeout but that may not be necessary for you.

// config/pubsub.js
module.exports.pubsub = {
  _hookTimeout: 60000 // I used 60 seconds as my new timeout
};

Note: The other answer recommends changing the sails files inside the node_modules folder. This is almost always a bad idea because any npm update could revert your changes.

davepreston
  • 1,260
  • 1
  • 10
  • 20
  • This worked perfectly for me. In my case, I was running sails on a raspberry pi model B, so I had to set orm and pubsub hook timeouts to 120000ms since it would still get the error at 60000ms – 0x0 Mar 30 '15 at 09:06
  • I had to add config/pubsub.js event for a local mysql DB. I needed it because i have setted migrage: 'alter' to add a column in one table. – Mat'arangéÇa Jul 27 '17 at 00:25
23

It is likely best to do this on a per env basis. Under config directory, you will have something like:

env path

Then enter, inside module.exports of each:

module.exports = {

  hookTimeout: 40000

}

Notice, there is no need for an underscore in front of the attribute name either.

arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • I am doing this, And still get the same error. Could It be the time itself? or should I put the hookTimeout inside an orm object? – Juan Solano Nov 26 '15 at 15:49
10

I realise this is quite an old question, but I also had the same problem. I was convinced it wasn't my connection.

My solution is to change your migration option for your models and you'll have a choice of 3

  1. safe - never auto-migrate my database(s). I will do it myself (by hand)
  2. alter - auto-migrate, but attempt to keep my existing data (experimental)
  3. drop - wipe/drop ALL my data and rebuild models every time I lift Sails

Got to config/models.js and in there put:

migrate: 'safe'

or whatever option from above you want to use.

K20GH
  • 6,032
  • 20
  • 78
  • 118
  • 1
    Just a 'me too!' - I kept getting a timeout, but I changed the migration to safe and the standard timeout of 20 seconds worked – James Apr 18 '16 at 04:04
1

There are two ways, which we can probably call them as:

1- System-wide method: (as @arcseldon has told)

Try to add the hookTimeout key to the project's config/env/development.js or config/env/production.js file. Next almost all the hooks (except some hooks, such as moduleloader) will retrieve the timeout value and consider it for themeselves.

2- Hook specific method: (as @davepreston has told)

create a [module-name].js file in the project's config folder and add _hookTimeout key to it. So, it will lead into assigning the timeout value only to that specific module. (Be careful about the specific json structure for the sails config files.)

Ehsan
  • 1,245
  • 11
  • 20
0

Go to you node_modules folder and browse to \sails\lib\app\private

In your case you should go to this folder: C:\Users\KAMI\AppData\Roaming\npm\node_modules\sails\lib\app\private

Then open the file named loadHooks.js and go to the line that says:

var timeoutInterval = (sails.config[hooks[id].configKey || id] && sails.config[hooks[id].configKey || id]._hookTimeout) || sails.config.hookTimeout || 20000;

Change the last value in this line from 20000 to some higher value and save the file then run your application by "sails lift" as you normally do

NB: you may need to try out a few higher values instead of 20000 until you reach a value that works for you. My application successfully lifted when I changed the value to 50000

Hesham Usama
  • 129
  • 1
  • 5
  • Sometimes the ORM hook (your database adapter/plugin) needs more time to be loaded when the app starts. However, Sails sets a default timeout of 20 seconds when the hook configuration doesn't contain a defined timeout for it to load (like our case here). So what we did was simply increasing the default timeout limit that sails sets. @Niya – Hesham Usama Feb 23 '15 at 16:42
  • 2
    I'd always avoid changing anything in the `node_modules` folder as those are your dependencies, not your code. @HeshamUsama is right about the problem, but the better place (in my opinion) to change the timeout is in the config within your app. I've added an answer with details on how I solved the problem for my app. – davepreston Mar 05 '15 at 21:41
0

Go to models.js file and uncomment migrate: 'alter'

0

while running sails lift run this command in the command line sails lift hookTimeout=75000

Prem Sai
  • 1
  • 1
0

You can also try to add defaults: { timeout: 30000 } to your hook Reference: https://sailsjs.com/documentation/concepts/extending-sails/hooks/hook-specification/defaults

Aliaksei
  • 1,094
  • 11
  • 20