lessless

lessless

Ensuring per user workflow uniqueness

Hi,

Is there a way to ensure that only one workflow will be scheduled per user in a 3-hour window?

Unlike Unique Workflows (no overlap per user) - #4 by sorenone , this is an idempotency/uniqueness question.

I noted in Oban.Pro.Workflow — Oban Pro v1.6.12 that workflow_id must be unique, and tried to set it to “sync-account-transactions #{account.id}”, but that didn’t prevent insertion.

Best,

Yevhenii

Marked As Solved

sorentwo

sorentwo

Oban Core Team

Sure! The trick is to put a single job with uniqueness in front of the workflow creation. Here’s an example:

defmodule MyApp.Flow do
  # The worker doesn't normally have unique enabled
  use Oban.Pro.Worker

  alias Oban.Pro.Workflow

  # Gate the workflow behind a job that's unique by the user id
  def invoke(user) do
    %{user_id: user.id}
    |> new(unique: [period: {3, :hours}])
    |> Oban.insert()
  end

  @impl Oban.Pro.Worker
  def process(job) do
    user = MyApp.Repo.get(MyApp.User, job.args["user_id"])

    # Build up the workflow within the invoked job. Using context and cascade purely as an example.
    Workflow.new()
    |> Workflow.put_context(%{user: user})
    |> Workflow.add_cascade(:a, ...)
    |> Workflow.add_cascade(:b, ...)
    |> Workflow.add_cascade(:c, ...)
    |> Oban.insert_all()
  end
end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement