Sails: Turn off grunt in production

Created on 11 Jul 2014  Â·  11Comments  Â·  Source: balderdashy/sails

I have a large angular js app, and the build process in production takes a long time because of the uglify step. Is there a way to first build the app, commit it, and then skip the grunt build process in production env ? (Something like _locking_ the production build)

Most helpful comment

Beginning with the next patch release of Sails, you should be able to turn off hooks via environment vars, e.g.:

sails_hooks__grunt=false NODE_ENV=production sails lift

In the meantime, check out this comment about turning off Grunt tasks in production.

All 11 comments

@prateekbhatt Take a look at http://beta.sailsjs.org/#/documentation/anatomy/myApp/tasks/README.html if your using sails v0.10

sails www
sails www --prod

@animedbz16 yes, I am using sails v0.10
Thanks for the suggestion.

sails www needs the creation of a assets/index.html file.

Do you know if there is any grunt task to create the assets/index.html file and link all the production assets in there ? Also, change the config/routes file to not serve the homepage.ejs file. This would enable us to keep the production build separate by running the development env using the views/layout.ejs file, and the production env using the assets/index.html file.

Just clarifying before trying to create it myself.

your app.js should have something like this:

sails.lift(rc('sails'))

change this line to something like this when you are in production mode:

if (process.env.NODE_ENV === 'production')
    sails.lift(rc('sails', {hooks:{grunt:false}})
else
    sails.lift(rc('sails'))

@edy thanks, that solved one of my problems!

config/env/production.js is a good place for this too, but hooks config doesn't apply from there.

module.exports = {
    hooks: {
        grunt: false // doesn't work
    },
    log: {
        level: 'silly' // doesn't work
    },
    port: 12345 // works
};

I did it by using @edy 's advice, and also modifying the sail-linker production tasks to update the assets/index.html instead of the views/layout.ejs file. Thanks. Closing this now

I think this should be reopened since it does not work in config/env/production.js as @edy wrote. The if-statement workaround feels like a hack. Inlining configuration makes it impossible to find for other developers.

I agree with @marcusnielsen. The ability to set more of these things in config/env would be better than it if-statement workaround. Right now I have a .sailsrc file different in my test and production branches than my local, because i wanted turn off grunt hooks and set a custom path for assets, but it would be great to be able to add those configs to config/env/production instead of app.js.

Is this a possible change?

:+1: +1 for edy's feature/enhancement request @mikermcneil

config/env/production.js is a good place for this too, but hooks config doesn't apply from there.

module.exports = {
    hooks: {
        grunt: false // doesn't work
    },
    log: {
        level: 'silly' // doesn't work
    },
    port: 12345 // works
};

In a prod env, one may want to lift additional apps in cluster mode without running the grunt tasks ie: pm2 start app.js -i 2 --name "sails-app" (add 2 more cluster instances to 'sails-app').

For me, I'd like to do this with a command line arg since i have a grunt task that hashes my assets in prod each time the app lifts and the linker will get all haywire when delivering the layout referencing different hashed assets.

Anyways, just an idea.. thanks for listening!

Beginning with the next patch release of Sails, you should be able to turn off hooks via environment vars, e.g.:

sails_hooks__grunt=false NODE_ENV=production sails lift

In the meantime, check out this comment about turning off Grunt tasks in production.

:+1: +1 for crobinson42's idea
How about cmd line argument switch(--no-grunt) for disabling grunt at specific launch?
For example I have automatic deployment system (which clones repo from GitHub & sends it to production server) and configured load balancer using Nginx:

upstream sails_cluster {
    ip_hash;                    # WebSockets support
    server localhost:2001;      # Instance 1
    server localhost:2002;      # Instance 2
    server localhost:2003;      # Instance 3
}

For zero downtime while upgrading code from git, I launching Sails app like this (ssh queue after repo was cloned locally to server):

pm2 delete app-2001 -f && echo = 1 INSTANCE DELETED
pm2 start app.js --name app-2001 -x -f -- --prod --port=2001 && sleep 15 && echo = 1 INSTANCE RELOAED AND LIFTED ON 2001 PORT
pm2 delete app-2002 -f && echo = 2 INSTANCE DELETED
pm2 start app.js --name app-2002 -x -f -- --prod --port=2002 && sleep 15 && echo = 2 INSTANCE RELOAED AND LIFTED ON 2002 PORT
pm2 delete app-2003 -f && echo = 3 INSTANCE DELETED
pm2 start app.js --name app-2003 -x -f -- --prod --port=2003 && sleep 00 && echo = 3 INSTANCE RELOAED AND LIFTED ON 2003 PORT

But while some of instances starting after code upgrading, it does grunt tasks & removing .tmp/public dir, and when user refreshing a page, instance â„–2(for example) successfully processed request (while â„–1 still lifting and doing grunt tasks), but no assets loaded, cause .tmp/public dir temporary removed or doesn't contain all/any minified files for production.

Please, pay attention for this feature/enhancement request - it's very important for real production needs.
THANKS :wink:


UPDATE: If you want to make something, what I described above, here is little sails-hack (thanks @edy for idea) :
Edit your app.js in root of Sails app:
Replace:

// Start server
sails.lift(rc('sails'));

With:

var args = process.argv.slice(2);
if (args.indexOf('--no-grunt') != -1) {
    // console.log('Running with --no-grunt switch');
    sails.lift(rc('sails', {hooks: {grunt: false}}));
} else {
    sails.lift(rc('sails'))
}

And now you can start Sails app like this:
node app.js --prod --no-grunt

Or you can just edit default Sails grunt task compileAssets.js - simply comment out line 'clean:dev', and .tmp/public directory won't be removed by task.

Was this page helpful?
0 / 5 - 0 ratings