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

Last Post!

lessless

lessless

Neat-o! That’s what I call outside the box thinking :brain::+1:

Where Next?

Popular in Questions Top

New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement