Skip to content

Instantly share code, notes, and snippets.

@elandesign
Last active March 3, 2016 12:29
Show Gist options
  • Save elandesign/fc11277e23587ecb1c72 to your computer and use it in GitHub Desktop.
Save elandesign/fc11277e23587ecb1c72 to your computer and use it in GitHub Desktop.
Add some metrics to your Rakefiles
task :environment do
puts "environment"
end
namespace :metrics do
task :before do
# Get time task started
@started_at = Time.now.to_i
end
task :after, :name do |t, args|
# Do something with the time taken
puts "Task #{args[:name]} - took #{Time.now.to_i - @started_at}s"
end
end
namespace :foo do
task :bar => :environment do
puts "bar"
sleep(rand(5))
end
task :quick => :environment do
puts "quick"
end
end
# Wrap each task in the foo namespace in with metrics before/after
Rake.application.tasks_in_scope(Rake::Scope.new(:foo)).each do |task|
task.enhance(["metrics:before"]) do
Rake::Task["metrics:after"].invoke(task.name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment