solid_queue icon indicating copy to clipboard operation
solid_queue copied to clipboard

Rake Task Support in Recurring Jobs

Open kaka-ruto opened this issue 1 year ago • 2 comments

Would you be open to supporting rake tasks in recurring jobs?

Something like this

production:
  generate_sitemap:
    rake: sitemap:refresh
    flags:
      s: true
      verbose: true
    schedule: every day at 3am

  custom_rake_task:
    rake: my:custom:task
    args: ['arg1', 'arg2']
    flags:
      environment: production
    schedule: every hour

I can give it a jab.

kaka-ruto avatar Oct 09 '24 18:10 kaka-ruto

I like this idea. To solve this for now I created a generic Job that allows me to pass rake tasks in as the argument:

# frozen_string_literal: true

class RakeTaskJob < ApplicationJob
  queue_as :default

  def perform(command)
    require "rake"

    Rake.application.init
    Rake.application.load_rakefile

    Rake::Task[command].invoke
  end
end

Then I can just do this in recurring.yml:

  monday_job:
    class: RakeTaskJob
    args: "scheduled_tasks:monday_jobs"
    schedule: every Monday at 7am UTC

kylekeesling avatar Nov 01 '24 14:11 kylekeesling

Thanks @kylekeesling

# works in Rails 8
class RakeTaskJob < ApplicationJob
  queue_as :default

  def perform(task_name)
    YourAppName::Application.load_tasks

    Rake::Task[task_name].invoke
  end
end
production:
  refresh_sitemap_job:
    class: RakeTaskJob
    args: "sitemap:refresh:no_ping"
    schedule: every day at 16:30

gazeldx avatar Apr 03 '25 04:04 gazeldx

thanks @gazeldx

For me, first execution was okay but the next ones were doing nothing, I had to add a line to reenable the rake task.

class RakeTaskJob < ApplicationJob
  def perform(task_name)
    Rails.application.load_tasks

    Rake::Task[task_name].invoke
    Rake::Task[task_name].reenable
  end
end

nflorentin avatar Jun 05 '25 06:06 nflorentin

I think the solution from @kylekeesling here is a good one so I'm going to close this as not planned right now, but I might come back to it in the future.

rosa avatar Jun 17 '25 10:06 rosa