Rake Task Support in Recurring Jobs
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.
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
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
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
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.