rails / solid_queue

Database-backed Active Job backend
MIT License
1.95k stars 130 forks source link

Rake Task Support in Recurring Jobs #379

Open kaka-ruto opened 1 month ago

kaka-ruto commented 1 month ago

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.

kylekeesling commented 2 weeks ago

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