fractaledmind / acidic_job

Elegant, resilient, durable workflows for Rails apps
https://fractaledmind.github.io/acidic_job/
MIT License
492 stars 10 forks source link

Using a Custom Idempotent Key AND an iterable step = run once only #75

Closed fabiengagne closed 1 year ago

fabiengagne commented 1 year ago

Using a Custom Idempotent Key and an iterable step (using a for_each:), results in the job queue not running any jobs after the first job is completed. The AcidicJob will only run the first job, including all its steps, but all others jobs enqueued after this are only initialized but never run. In effect, this Custom Indempotent queue is stuck.

Sample code :

class MyJob < ApplicationJob
  # custom idempotent key so that all transactions with the distant server are sequential
  acidic_by do
    distant_server_config = arguments.first   # First parameter to the .perform method
    "DistantServer-" + distant_server_config.api_key
  end

  def perform(distant_server_config)
    @distant_server_config = distant_server_config

    with_acidic_workflow persisting: { collection: [1, 2, 3, 4, 5] } do |workflow|
      workflow.step :step1, for_each: :collection
      workflow.step :step2
    end

  def step1(num)
    p "in step1 with " + num.to_s
    true
  end

  def step2
    p "in step2"
    true
  end
end

Then do dist_server.api_key = "abcdef" MyJob.perform_later(dist_server) sleep 2 MyJob.perform_later(dist_server)

Result step1 is displayed 5 times, followed by step2 (the second job is never executed)

Expected result step1 is displayed 5 times, followed by step2 step1 is displayed 5 times, followed by step2

fabiengagne commented 1 year ago

Disregard this PR. I now understand that this is the actual expected result. Idempotent keys does not make jobs with two identical keys execute serially (one after the other), it make them execute only the first time. I misunderstood the goal of this package.