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

Best way to ensure ordering of a queue. #402

Closed
wwahammy opened this issue Oct 5, 2021 · 1 comment
Closed

Best way to ensure ordering of a queue. #402

wwahammy opened this issue Oct 5, 2021 · 1 comment
Projects

Comments

@wwahammy
Copy link

wwahammy commented Oct 5, 2021

Maybe this isn't the exect best place for bringing this up but I'm trying to understand how good_job can help me.

I have a set of jobs that operate on a same objects. I need to record the changes on another system in the exact same order they were sent in. In effect, I need to make sure that if processing a job fails in a queue, that all of the jobs behind that job in the queue would be stuck until the first one was corrected somehow.

Is there a good way to do that using good_job?

@bensheldon bensheldon added this to Inbox in Backlog Oct 5, 2021
@bensheldon
Copy link
Owner

bensheldon commented Oct 7, 2021

@wwahammy this is an interesting challenge. I'm curious about how the sequence of operations is determined.

If I think of systems I have worked on, I would track the state of this outside of GoodJob. For example, if it was maintenance of a car:

class Car
  has_many :maintenance_tasks
  has_many :uncompleted_maintenance_tasks, -> { uncompleted.order(created_at: :asc) }, class_name: "MaintenanceTask"
end

class MaintenanceTask
  belongs_to :car
  scope :uncompleted, -> { where(completed_at: nil) }
end

class CarMaintenanceJob < ApplicationJob
  def perform(car)
    task = car.uncompleted_maintenance_tasks.first
    # do the task, or raise an exception that would prevent the following from running...
    task.touch(:completed_at)
    CarMaintenanceJob.perform_later(car) if car.uncompleted_maintenance_tasks.any?
  end
end

# And the sequence of operations would be determined by calling 
# `car.create_maintenance_task(task_params)` for each task 
# that needs to be performed.

Backlog automation moved this from Inbox to Done Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Backlog
  
Done
Development

No branches or pull requests

2 participants