Add a Rake.application.running? predicate#243
Add a Rake.application.running? predicate#243leonid-shevtsov wants to merge 2 commits intoruby:masterfrom
Conversation
Interesting, I haven't had the need like this. Could you elaborate on this? |
| load_rakefile | ||
| top_level | ||
| end | ||
| @running = false |
There was a problem hiding this comment.
This should be wrapped with an ensure block:
def run(...)
..
ensure
@running = false
end| assert !@app.running? | ||
| assert was_running | ||
| end | ||
|
|
There was a problem hiding this comment.
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: