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

1

10

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?

Grisham answered 11/4, 2017 at 17:46 Comment(2)
+1 I would love something like this. With Jenkins and the like, you can view the log output for past runs. If available, I would use an add on to allow this.Dotson
If you have a worker dyno, you could encapsulate the current task code into a job and have the rake task enqueue that job.Demon
M
1

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

Moraceous answered 24/4, 2019 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.