Skip to content

Running rake commands directly via command: in recurring tasks configuration #587

Open
@marelons1337

Description

@marelons1337

Hi!
I am moving my workers from http://github.com/javan/whenever schedule to solid_queue and the command key in recurring tasks could be improved with rake tasks handling.
Here's a simple example based on my https://github.com/ankane/pghero setup

# config/schedule.rb
every 10.minutes do
  rake "pghero:capture_query_stats"
end

I believe this would be easy to implement by simply letting users do:

# config/schedule.yml
  capture_query_stats:
    command: "rake pghero:capture_query_stats"
    schedule: every 10.minutes

Currently it produces below error

undefined local variable or method 'capture_query_stats' for an instance of SolidQueue::RecurringJob

Is there something I am missing here and it's possible to execute the above? Right now we are using a simple workaround to get this to work:

# config/schedule.yml
  capture_query_stats:
    class: Rake::TaskJob
    args: "pghero:capture_query_stats"
    schedule: every 10 minutes
    queue: default
# app/jobs/rake/task_job.rb
class Rake::TaskJob < ApplicationJob
  queue_as :default
  self.queue_adapter = :solid_queue
  retry_on StandardError, attempts: 0

  def perform(command)
    require "rake"
    Rake.application.init
    Rake.application.load_rakefile
    Rake::Task[command].invoke
  end
end

But it does seem a bit ovet the top. I am happy to open a PR for this improvement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions