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

Redesigning API for concurrency #120

Open
lguminski opened this issue Jul 17, 2021 · 0 comments
Open

Redesigning API for concurrency #120

lguminski opened this issue Jul 17, 2021 · 0 comments

Comments

@lguminski
Copy link

lguminski commented Jul 17, 2021

For my use case, before placing a new job in the queue, I want to cancel all jobs in the queue with the same arguments.

I can see that Honeydew example suggests the following

Honeydew.filter(:my_queue, %{task: {:run, [10]}})
|> List.first
|> Honeydew.cancel

I believe that there is a flaw in this code, because filter and cancel are two consecutive commands and after filter returned data and before cancel gets executed there are many things that can happen in a concurrent system. Jobs can get cancelled, start getting processed, complete, or new jobs can start that match the search criterion. Shortly speaking cancel is likely to operate based on outdated input.

One of possible solutions to this problem is something similar to Map.get_and_update/3. What I mean is that the filter function could be extended with one more parameter - a function executed in its context

filter(queue_name(), filter(), function()) :: :ok | {:error, :failed}
 Honeydew.filter(:my_queue, %{task: {:run, [10]}}, fn job -> Honeydew.cancel(job) end)

or even better adding also an accumulator as a parameter (similar to Enum.reduce/3)

filter(queue_name(), filter(), acc(), function()) :: acc()

the accumulator could be used to keep track on cancellations, which is needed for scenario described in examples/filter_and_cancel.exs

 was_executed = Honeydew.filter(:my_queue, %{task: {:run, [10]}}, false, fn job, was_executed ->
        if not was_executed do
          Honeydew.cancel(job)
        end
        true
      end)

Another option are transactions.

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

1 participant