Is there a way to "return" a value from a workflow?

Is there a way to “return” a value from a sub workflow? Suppose I have a sub workflow that creates a db record and I need to access this record ID in a downstream job in the parent workflow. Here’s an example I have that attempts to recover it through recorded values but with no success:

defmodule ChildJob do
  use Oban.Pro.Worker, recorded: true
  
  alias Oban.Pro.Workflow
  
  def workflow() do
    Workflow.new()
    |> Workflow.add(:child, new(%{}))
  end
  
  @impl true
  def process(job) do
    {:ok, "secret_id"}
  end
end

defmodule ParentJob do
  use Oban.Pro.Worker, recorded: true
  
  alias Oban.Pro.Workflow
  
  def workflow() do
    Workflow.new()
    |> Workflow.add_workflow(:a, ChildJob.workflow())
    |> Workflow.add(:b, new(%{}), deps: :a)
  end
  
  @impl true
  def process(job) do
    Workflow.all_recorded(job) |> IO.inspect()
    :ok
  end
end
iex(3)> ParentJob.workflow() |> Oban.insert_all()
%{"b" => nil}
1 Like

Yes, there’s absolutely a way to do this! It’s fairly essential to how cascades work as well. You need to pass the with_subs: true option to fetch the results of sub-workflows as well.

  @impl true
  def process(job) do
-    Workflow.all_recorded(job) |> IO.inspect()
+    Workflow.all_recorded(job, with_subs: true) |> IO.inspect()
    :ok
  end

It is noted in the documentation for Workflow.all_recorded/3, but should be demonstrated more prominently in the module docs.

2 Likes

Ah thank you, I totally missed this in the docs!

1 Like