The AppSignal gem version supports Rake since version
0.11.13 of the gem.
Every exception recorded in a Rake task will be sent to AppSignal and filed under the "Background" namespace. Note that we only track exceptions in Rake tasks. There is no performance monitoring for Rake tasks.
Depending on what version of the AppSignal gem you use and in what context some manual steps are required.
For Rails applications make sure you depend on the
:environment task. This loads the Rails application into memory and starts AppSignal as part of the application.
1 2 3 4 5
# lib/tasks/my_task.rb task :my_task => :environment do # do stuff end
Your Rails application's
Rakefile should look something like the example below. This should already be the case, no need to change it.
1 2 3 4 5 6 7
# Rakefile require File.expand_path("../config/application", __FILE__) # Only require this file for gem version < 1.0 # require "appsignal/integrations/rake" Rails.application.load_tasks
(For older versions of the AppSignal gem, versions
< 1, you will need to require the Rake integration manually. It is automatically loaded for version
1.x and higher.)
For pure Ruby applications some extra steps are required to load AppSignal. AppSignal needs to be required, configured and loaded. See the example below.
1 2 3 4 5 6 7 8 9 10
# Rakefile require "appsignal" Appsignal.config = Appsignal::Config.new(Dir.pwd, "development") Appsignal.start Appsignal.start_logger task :foo do raise "bar" end
When not relying on our automatic exception handling, and instead using
Appsignal.send_error another step is required. To flush the recorded error to our agent it's necessary to call
Appsignal.stop("rake"). This is normally done for you with the AppSignal exception handling, but is necessary in this scenario.
1 2 3 4 5 6 7 8 9 10 11 12 13
# Rakefile require "appsignal" Appsignal.config = Appsignal::Config.new(Dir.pwd, "development") Appsignal.start Appsignal.start_logger task :foo do Appsignal.send_error StandardError.new("bar") # "rake" is the parent process name which is being stopped and the reason why # AppSignal is stopping. Appsignal.stop "rake" end
For more information on how to integrate AppSignal in a pure Ruby application, see our integration guide.
See our example repository for a Ruby + Rake + AppSignal example application.
Add performance monitoring to a continuously running multi-threaded Rake task for
v1.1.6. Based on work of leehambley.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# Rakefile namespace :mycrazyproject do task :do_something => :environment do while true User.where(:active => true).find_in_batches(:batch_size => 20) do |batch_of_users| # We collect a bunch of new threads, one for each # user, each... batch_threads = batch_of_users.collect do |user_outer| Thread.new(user_outer) do |u| transaction.set_action("name.of.background.action.you.want.in.appsignal") begin ActiveSupport::Notifications.instrument( 'perform_job.long_running_task', :class => 'User', :method => 'long_running_task' ) do u.long_running_task end rescue => err transaction.set_error(err) ensure # Complete the transaction Appsignal::Transaction.complete_current! end end end # Joining threads means waiting for them to finish # before moving onto the next batch. batch_threads.map(&:join) end end end end