Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to schedule many instances of a recurring task with different args #216

Closed
zavan opened this issue May 1, 2024 · 3 comments
Closed

Comments

@zavan
Copy link

zavan commented May 1, 2024

I've been using the whenever gem to do something like for a lot of my recurring jobs:

every 3.hours do
  runner "UpdateCustomerStatsJob.enqueue_all"
end
class UpdateCustomerStatsJob < ApplicationJob
  def self.enqueue_all
    Customer.in_batches do |batch|
      jobs = batch.map { |customer| new(customer) }
      ActiveJob.perform_all_later(jobs)
    end
  end

  def perform(customer)
    customer.update_stats
  end
end

I wanted to use solid_queue recurring tasks to do this. However since it only allows passing a job class on which it always calls perform_later, if I want to do this I think I have to create a separate job responsible solely for enqueuing the individual jobs:

class EnqueueAllUpdateCustomerStatsJob < ApplicationJon
  def perform
    Customer.in_batches do |batch|
      jobs = batch.map { |customer| UpdateCustomerStatsJob.new(customer) }
      ActiveJob.perform_all_later(jobs)
    end
  end
end

Do you think there could be a simpler way to do this with solid_queue? I guess the difficult part would be to avoid duplicate jobs in case of multiple dispatchers (for 'whenever' I only run it on one host right now and I use 'activejob-uniqueness'). Not sure how it could handle doing things in batches also (I'm thinking I don't want to create hundreds of thousands of job instances in memory before enqueuing them), maybe some yielding mechanism.

I don't know if it's just me doing things this way or if it could be useful for more people, but I guess that's the only way to know :)

@zavan zavan changed the title A way to batch schedule recurring tasks A way to schedule many instances of a recurring task May 1, 2024
@zavan zavan changed the title A way to schedule many instances of a recurring task A way to schedule many instances of a recurring task with different args May 1, 2024
@rosa
Copy link
Member

rosa commented May 3, 2024

Hey @zavan! Yes, I think that'd be the way 🤔 Perhaps if I had more cases like UpdateCustomerStatsJob, I'd do something more generic like:

class RunnerJob < ApplicationJob
  def perform(command)
    eval(command)
  end
end
      recurring_tasks:
        update_customer_stats:
          class: "RunnerJob"
          schedule: "every 3 hours"
          args: "UpdateCustomerStatsJob.enqueue_all"

but if it's only one case, I'd go with your solution, I think.

@idrista
Copy link

idrista commented May 6, 2024

Hello, I agree with @rosa, take a look at the configuration of simple_scheduler is very simple and clear. https://github.com/simplymadeapps/simple_scheduler

@zavan
Copy link
Author

zavan commented May 16, 2024

I will go that route. Thanks!

@zavan zavan closed this as completed May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants