How to retrieve a job struct inside a decorator

I have the following scenario I want to solve with Oban:

A job will be created that will fetch a list of users
For that list, I want to chunk them and create a job for each job that will do send an email for the users.

This seems like a good use of workflows, basically I start my workflow with just one job:

    Workflow.new()
    |> Workflow.add(:chunk_investors, new_chunk_investors(buy_lists_ids, actor_id))
    |> Oban.insert_all()

Now, inside the job, I will have the chunked lists and append jobs to the workflow, something like this:

  @job [queue: :email]
  def chunk_investors(buy_lists_ids, actor_id) do
    lists = ...

    {:ok, jobs} = Workflow.all_jobs(job)

    lists
    Enum.with_index()
    Enum.reduce(Workflow.append(jobs), fn {list, index}, jobs ->
      Workflow.add("send_email_#{index}", new_send_email(list, actor_id))
    end)
    |> Oban.insert_all()

    :ok
  end

I think this should probably work fine, the issue is that i don’t have the job struct to call Workflow.all_jobs when my job is decorated… Is there some way to retrieve it so this can work? If not, is there some other way to recover the workflow?

Thank you for posting here on the forum! :star2: For your scenario:

There’s a new current_job/0 function in Pro v1.6 for this exact purpose! From within your decorated job:

@job queue: :email
def chunk_investors(buy_lists_ids, actor_id) do
  job = current_job()

There’s also an add_many/4 function to streamline adding a group of jobs as a sub-workflow and add_cascade/4 for the new cascade-mode.

1 Like

I see that the 1.6 version is still in RC, do you think it is fine to use in production code already or should I hold back until it is fully released?

It’s still an rc because want people to be aware of the changes when they upgrade and to be sure to do the db migrations. We use it in production, others do, too.

If you proceed, you’ll surely follow the upgrade guide and let us know if you have any issues.

1 Like