How to determine in rails if I run the rake command?

In Rails, you may want your environment to do different things at startup, depending on whether you are running the rake command or not. For example, in my use case, several hundred MB of cache was loaded into memory at application startup. We clearly do not want this to happen in rake teams.

--- --- update The following is a reliable solution and works with the hero.

is_rake = (ENV['RACK_ENV'].blank? || ENV['RAILS_ENV'].blank? || !("#{ENV.inspect}" =~ /worker/i).blank?) 
+4
source share
2 answers

Finding out if your environment on the rake team is pretty simple, but it took me a while to figure this out. Hope this helps someone out there!

 #In environment.rb, I do the following is_rake = !("#{ENV.inspect}" =~ /rake/i).blank? puts "Is Rake? #{is_rake}" 
+1
source

If you use heroku And you use 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?) 
+1
source

All Articles