Skip to content

Instantly share code, notes, and snippets.

@shairyar
Last active September 28, 2021 11:22
Show Gist options
  • Save shairyar/0226249e32d3d383d4febf67ea5da7a9 to your computer and use it in GitHub Desktop.
Save shairyar/0226249e32d3d383d4febf67ea5da7a9 to your computer and use it in GitHub Desktop.
Rake task initializer
if ENV["APPSIGNAL_PUSH_API_KEY"]
require "rake/task"
class AppsignalRakeHelper
class << self
# Mark a Rake task that it should wait for AppSignal to transmit all the data
# when the task is done. It should only do this on Heroku.
def wait_on_exit
return unless ENV["DYNO"] # Only activate on Heroku
at_exit do
# We need to make sure AppSignal is stopped here so that all the
# transaction data is flushed to the agent by the extension.
Appsignal.stop("rake")
# Wait for a bit, because AppSignal.stop doesn't wait for the agent to
# transmit all its data. Since it has been told to stop, it should shut
# down in maximum 5 seconds. Giving it time to transmit the data to AppSignal.
# Increased to 10 seconds because some rakes are not tracked
sleep 10
end
end
end
end
module Appsignal
module Integrations
module CustomRakeIntegration
def execute(*args)
AppsignalRakeHelper.wait_on_exit
Appsignal.monitor_single_transaction("perform_job.rake_tasks", method: name) do
super
end
end
end
end
end
::Rake::Task.prepend(Appsignal::Integrations::CustomRakeIntegration)
end
namespace :test_task do
desc 'This is a test rake task'
task run: :environment do
sleep(20.minutes)
puts 'Do something complex'
end
task run1: :environment do
sleep(20.minutes)
puts 'Do something complex 1'
end
task run2: :environment do
sleep(20.minutes)
puts 'Do something complex 2'
end
task run3: :environment do
sleep(20.minutes)
puts 'Do something complex 3'
end
task run4: :environment do
sleep(20.minutes)
puts 'Do something complex 4'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment