Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect in rails if I am running a rake command?

In Rails, you might want your environment to do different things on startup depending on if you are running a rake task or not. For instance, my use case was having several hundred MB of cache loaded into memory on app start. We obviously don't want this to happen on rake commands.

---update--- The following is reliable solution and works with heroku.

is_rake = (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?)
like image 383
Shawn Deprey Avatar asked Mar 21 '13 02:03

Shawn Deprey


People also ask

How do I see rake tasks?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.

How do I run a rake task in rails?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

What does the rake command do?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.


1 Answers

If you are using heroku AND you are using workers, here is a more reliable way to do this check.

is_rake = (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?)
like image 154
Shawn Deprey Avatar answered Oct 26 '22 19:10

Shawn Deprey