execute a task in running environment

30 views
Skip to first unread message

Thom

unread,
Feb 24, 2015, 3:04:26 PM2/24/15
to sai...@googlegroups.com
I often use rake tasks in rails for things like bootstrapping an environment (e.g. adding data for the first time or something) that usually requires an active database connection and access to the application code from the task's scope. 

For example, I might have a logic that queries an external service, processes the response and adds data to my database.  Normally say this is triggered off a queue or user interaction etc but I want to quickly execute it with specific parameters.  This is really easy to do with a rake task in rails.  Is this easily accomplished in sails?

Thanks!
-Thom

Mike Hostetler

unread,
Feb 24, 2015, 3:36:24 PM2/24/15
to Thom, Sailsjs
Have you considered just using Grunt tasks for this?

You can just include Sails right into a Grunt task.

----------------
Mike Hostetler 
----------------
BLOG | http://mike-hostetler.com
TWITTER | http://twitter.com/mikehostetler


Thom

unread,
Feb 25, 2015, 10:32:49 AM2/25/15
to sai...@googlegroups.com, tmni...@gmail.com
Right so I guess that's my question - can I just `require('sails')` and I'll magically have a bootstrapped environment and database connection inside my grunt task or are there other things I have to do?

Thanks.

Mike Hostetler

unread,
Feb 26, 2015, 9:09:58 AM2/26/15
to Thom, Sailsjs
Whether Sails has been bootstrapped or not will depend on where in the lifecycle you're trying to run the task.  If you want to just run it from the command line cold, you can easily do that.

I believe you can do the following:

require('sails').lift(options,function(err) {
  // Run my stuff here after Sails has been lifted
});


----------------
Mike Hostetler 
----------------
BLOG | http://mike-hostetler.com
TWITTER | http://twitter.com/mikehostetler



Thom Nichols

unread,
Feb 26, 2015, 9:10:42 AM2/26/15
to Mike Hostetler, Sailsjs
Awesome, that's what I was hoping for.

Thanks Mike!
-Thom
Reply all
Reply to author
Forward
0 new messages