-
Notifications
You must be signed in to change notification settings - Fork 613
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
base: master
Are you sure you want to change the base?
Add a Rake.application.running? predicate #243
Conversation
Interesting, I haven't had the need like this. Could you elaborate on this? |
standard_exception_handling do | ||
init "rake", argv | ||
load_rakefile | ||
top_level | ||
end | ||
@running = false |
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
assert [email protected]? | ||
assert was_running | ||
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?
Done - sorry for the huge delay. To elaborate on this:
to give one example, we mark changes made from a rake task for audit:
Another places I can recall:
(All of the above assumed a webapp context.) |
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 defined?(Rake) && Rake.running? |
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: