sezaru

sezaru

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?

Marked As Solved

sorenone

sorenone

Oban Core Team

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.

Also Liked

sorenone

sorenone

Oban Core Team

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.

Last Post!

sorenone

sorenone

Oban Core Team

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

We're in Beta

About us Mission Statement