-
Notifications
You must be signed in to change notification settings - Fork 628
Add a Rake.application.running? predicate #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Rake.application.running? predicate #243
Conversation
yuki24
commented
Dec 19, 2017
It is most useful to fence off code that is only needed for Rake tasks, or conversely, should not run inside a Rake task.
Interesting, I haven't had the need like this. Could you elaborate on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be wrapped with an ensure block:
def run(...) .. ensure @running = false end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a test case where the task execution fails but it correctly sets @running back to false after failure?
leonid-shevtsov
commented
May 2, 2018
Done - sorry for the huge delay.
To elaborate on this:
It is most useful to fence off code that is only needed for Rake tasks, or conversely, should not run inside a Rake task.
to give one example, we mark changes made from a rake task for audit:
if File.basename($PROGRAM_NAME) == "rake"
PaperTrail.whodunnit = "#{`whoami`.strip}: rake #{ARGV.join ' '}"
end
Another places I can recall:
- extended logging inside Rake tasks - logs that would be spammy in the server process.
- do not start project-specific subsystems that are known to make Rake tasks slow and are not necessary in Rake tasks.
(All of the above assumed a webapp context.)
booch
commented
Aug 30, 2018
I'm the author of one of those Stack Overflow questions (many years ago).
I think this is a worthwhile addition to Rake, but I have 1 suggestion and 1 concern:
First, I think it would be simpler to just have it be Rake.running?. Second, to use this, you'd have to check that the Rake constant is defined:
defined?(Rake) && Rake.running?
Uh oh!
There was an error while loading. Please reload this page.
I have added a
Rake.application.running?method, that returns true if Rake is running currently, and false otherwise. It is most useful to fence off code that is only needed for Rake tasks, or conversely, should not run inside a Rake task.This is a common need in Rails apps and the most popular way to see if the code is running within a rake task is checking the command line argument (see SO answers at the bottom). Which might work, but it is messy, and leads to copy-paste of the
File.basename(0ドル) == 'rake'idiom. Our project uses that code snippet in three distinct places, but instead of fixing this on a project level, in my opinion, it is a capability that Rake should have out of the box.StackOverflow answers that suggest checking the script filename: