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
Thank you for posting here on the forum!
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
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
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.
Popular in Questions
Other popular topics
Latest Oban Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









