Modifying Rake Tasks - Dan Manges's Blog
For custom Rake tasks, you shouldn't need to modify them after the original definition. However, if you want to add behavior to some vendor tasks (such as those defined with Rails), this blog post will cover how to do that.
Related cards:
Hide a Rake task from the `rake -T` list
A Rake task appears in rake -T
if it has a description:
desc 'Compile assets'
task :compile do
...
end
To not list it, simply omit the description:
task :compile do
...
end
You can also hide a Rake task that has been defi...
When Rails does not recognize Rake tasks in lib/tasks
When you put a Rake task into lib/tasks
, but running it fails with...
Don't know how to build task name:of:task
... does your Rake task's filename end in .rb
? It needs to end in .rake
instead.
Rails logs are not flushed automatically (in Rake tasks)
The Rails logger will store its content in a buffer and write it into the file system every 1000 lines. This will come back to bite you when using Rails.logger.info
to write log output during Rake tasks or on a production console.
You often won...
Adjust cron jobs to allow full backtraces for rake tasks
As we get an exception notification, when a cron job fails, we wish to have the full backtrace in this mail. A rake task doesn't output the full backtrace by default, so you need the --backtrace
option.
Trigger
You will find fail mails wit...
Monitor a Rake task with God
In order to monitor a Rake task using God your Rake file must write a file with its process ID (PID) to a path determined by God. This way God can check whether the Rake process is still alive.
Here is how to do this:...
Run code before or after an existing Rake task
Before
To run additional code before an existing Rake tasks you can add a dependency like this:
task :before_task do
# runs before :existing_task
end
Rake::Task[:existing_task].enhance [:before_task]
The new dependency will be...
Rspec: Expecting a Rake task to be called
This seems to be obvious, but you can expect Rake tasks to be called in RSpec.
it 'deletes all Users' do
FactroyBot.create(:user)
expect(Rake::Task['notify:critical_operation']).to receive(:invoke)
expect { described_class.clea...
Find Where a Rake Task is Defined
You can use rake --where task
to find the source location that defines task
:
bundle exec rake --where assets:precompile
rake assets:precompile /home/henning/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/precompiled_assets-0....