Is there any way to hold or pause an oban job until it’s param is ready?
I have one Oban job A in a workflow that is inserted but then another Oban job B from some other part of the system is immediately inserted with a param i.e. id
which comes from an external API call from job A and inserted in DB. Since job A may not be finished before job B is inserted, there is a chance the id
can be nil
.
How to ensure that job B does not fail due to nil
argument?
Thought about two different solutions -
- With Retry:
use Oban.Worker, max_attempts: 10
def perform(%Oban.Job{args: %{"id" => id}}) do
case Repo.get(MyApp.MySchema, id) do
%MySchema{external_data: nil} ->
{:error, :not_ready}
%MySchema{external_data: data} ->
do_something_with(data)
:ok
end
end
- With Re-enqueue with delay:
def perform(%Oban.Job{args: %{"id" => id}}) do
case Repo.get(MyApp.MySchema, id) do
%MySchema{external_data: nil} ->
# Data not ready, re-enqueue after a delay
__MODULE__.new(%{"id" => id}, schedule_in: 30)
|> Oban.insert()
:ok
%MySchema{external_data: data} ->
# Data ready, process it
do_something(data)
:ok
end
end
I am wondering which solution is preferable or if there is any other better solution in this case.
Thanks.