chitalepushkar

chitalepushkar

Process associated to Task.Supervisor cannot checkout DB connection in test

I am using the Sage library to perform a set of operations involving DB inserts and calling external APIs. The Sage.transaction uses Ecto.Repo.transaction to wrap all actions performed on each step (similar to Ecto.Multi)

One of the step uses Task.Supervisor.async_nolink/5 to spawn a supervised process which inserts an Oban job. Something like:

defmodule SageModule do
  @supervisor MyApp.Supervisor

  def sage_function(_) do
    Sage.new()
    |> Sage.run(:do_something, &insert_background_job/2)
    |> Sage.transaction(MyApp.Repo)
  end

  defp insert_background_job(attrs, _) do
    task = Task.Supervisor.async_nolink(@supervisor, fn ->
      %{attrs: attrs}
      |> MyApp.NotifyExternalAPIWorker.new()
      |> Oban.insert()

      case Task.yield(task) || Task.shutdown(task) do
        {:ok, _result} ->
          :ok
        error ->
          # Log something
        end
  end
end

The code runs fine and executes the Oban job. But while testing I am unable to assert that the job was enqueued.

test "enqueues background job to notify external API" do
  assert _ = SageModule.sage_function(params)
 
  # Assertion fails with the following error
  #   Expected a job matching:

  #   %{worker: MyApp.NotifyExternalAPIWorker}

  #   to be enqueued in the "oban" schema. Instead found:

  #   []
  assert_enqueued worker: MyApp.NotifyExternalAPIWorker
end

If I try to inspect the task output from the Task.yield(task) || Task.shutdown(task) case statement, I get the following error

{:exit,
 {%DBConnection.ConnectionError{
    message: "could not checkout the connection owned by #PID...

Is there a way I can checkout the connection for the Task.Supervisor process? I have tried using the shared mode as well but no luck.

:ok = Sandbox.checkout(MyApp.Repo)

Sandbox.mode(MyApp.Repo, {:shared, self()})

First Post!

sorentwo

sorentwo

Oban Core Team

Running the sandbox in shared mode should allow the task process to access the connection. Are you sure you need to wrap inserting the Oban job in a task at all? Maybe try without the task and verify that it’s working?

Where Next?

Popular in Questions Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement