Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Rails Rake task on Heroku Scheduler as detached to capture log output in Papertrail

A known problem with running Rails Rake tasks on Heroku is that they don't submit their logs to Papertrail since the one-off dynos push their output to the console by default. This is solved by running your dyno in "detached" mode by using heroku run:detached rake your:task. Unfortunately, the Heroku Scheduler appears to automatically run tasks as normal instead of in detached mode so these logs are lost.

How can you make the scheduler run a task in "detached" mode so these weekly/daily/hourly tasks get their logs captured by Papertrail as expected?

like image 716
Erik Trautman Avatar asked Apr 11 '17 17:04

Erik Trautman


1 Answers

You can use sidekiq, this gem will help you run any processes with schedule, and inside in your sidekiq you can run rake tasks!

https://github.com/mperham/sidekiq

Example:

class MySidekiqTask
  include Sidekiq::Worker

  def perform
    application_name = Rails.application.class.parent_name
    application = Object.const_get(application_name)
    application::Application.load_tasks
    Rake::Task['db:migrate'].invoke
  end
end

Good instruction how setup Sidekiq in Heroku server

https://itnext.io/sidekiq-overview-and-how-to-deploy-it-to-heroku-b8811fea9347

like image 175
Andriy Kondzolko Avatar answered Nov 03 '22 21:11

Andriy Kondzolko