Rake task monitoring
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.
(To manually integrate performance monitoring in select Rake tasks please see our integration guide and custom instrumentation guide.)
Depending on what version of the AppSignal gem you use and in what context some manual steps are required.
Integrations
Rails applications
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.
# lib/tasks/my_task.rb task :my_task => :environment do # do stuff end
Rakefile
Your Rails application's Rakefile
should look something like the example below. This should already be the case, no need to change it.
# 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.)
Ruby applications
For pure Ruby applications some extra steps are required to load AppSignal. AppSignal needs to be required, configured and loaded. See also our integration guide.
# Rakefile require "appsignal" Appsignal.start task :foo do raise "bar" end
Monitoring Rake task performance
To monitor Rake task performance and all events that occur in the task, configure AppSignal with the enable_rake_performance_instrumentation
config option set to true
.
With this feature enabled, all Rake tasks will instrumented and counted towards your plan.
Stop requirement
For Rake tasks running on short-lived hosts, it is required to stop the AppSignal Ruby gem before the Rake process exits. This will ensure it reports the data from the Rake tasks to AppSignal before the host shuts down.
This behavior is only needed in such scenarios like:
- When a scheduled Rake task (a cron job) is run by a hosting service.
- When a Rake task is run on a container that starts to run this Rake task and shuts down when the Rake task exits.
- When a Rake task is run on a Heroku runner, or similar hosting providers.
It is not needed to stop the AppSignal Ruby gem if the host on which the script is started, continues to run after the script has exited.
AppSignal is stopped automatically when the Rake task exits and a container or hosting environment like Heroku is detected.
This behavior is controlled by the enable_at_exit_hook
option.
Ensure this option is set to true for scripts that require AppSignal to be stopped, if it's not automatically detected.
It may be needed to add an additional sleep
call of a couple seconds to ensure there's enough time to send all the data. Use the at_exit
usage example from the code below if needed.
# Short-lived host example task # This block is called after all tasks are ran # Ensures the AppSignal agent has enough time to send the data to the AppSignal servers at_exit { sleep 10 } task :foo do # Your Rake task code end
Stop requirement legacy
When running a single Rake task on a short-lived host (e.g. with Docker containers, Kubernetes or Heroku schedulers) there are three requirements. This guarantees that the app's AppSignal extension has time to flush the data to the agent and the agent has time to send the data to our API before shutting (the container) down.
The requirements are:
Appsignal.stop
must be called in the Rake task.- When Rake task performance monitoring is enabled, this is done automatically for all tasks.
running_in_container
must be set to true in the config.- For most containers types
running_in_container
is automatically set to true when detected, for others manual configuration is required.
- For most containers types
- A sleep of e.g. 10 seconds to give AppSignal agent time to sent the data
An example of how Appsignal.stop
is called in the Rakefile:
# Short-lived host example task # This block is called after all tasks are ran # The argument "rake" is the name of the parent process name which is being # stopped and logs it as the reason why AppSignal is stopping. at_exit do Appsignal.stop "rake" sleep 10 # For short-lived hosts, give the AppSignal agent time to send the data end task :foo do # Tracking data with AppSignal in your task Appsignal.increment_counter "my_custom_counter" end
Note: A sleep of 10 seconds was added to the end of the Rake task example in the example above.
This is required for tasks that are run on short-lived hosts.
When the task completes, the process stops.
The Appsignal.stop
call flushes all the transaction data currently in the AppSignal extension to our agent.
It then sleeps for 10 seconds to allow the agent to send the data before shutting down.