Arsenalist

Arsenalist

Database queries not executed inside Task.Supervisor

I have the following in my application.ex:

  def start(_type, _args) do
    children = [
      ...
      {Task.Supervisor, name: Amplify.UpdateMetaAudiencesSupervisor}
    ]
    opts = [strategy: :one_for_one, name: Amplify.Supervisor]
    Supervisor.start_link(children, opts)
  end

Then in my controller method I have:

    Task.Supervisor.async(Amplify.UpdateMetaAudiencesSupervisor, fn ->
      IO.inspect("Inside async") # this prints fine
      Amplify.Context.Audiences.get_audiences() # this makes a DB call
     IO.inspect("After DB call") # never gets printed and no SQL logs 
      end)
    end)

I don’t see any SQL statements being printed for the DB call and even if I put raise "error" inside the get_audiences(), I see no error messages in the logs.

How can I perform DB operations inside a Task.Supervisor (and have them printed to logs)?

Marked As Solved

Arsenalist

Arsenalist

I got this to work with Task.Supervisor.async_nolink() instead of Task.Supervisor.async() but I’m not fully understanding why that is working for me.

Also Liked

al2o3cr

al2o3cr

My guess: Task.Supervisor.async still links the Task to the calling process - in the case of the controller, that’s the request handler process. When the request finishes, that process exits and the Task is terminated.

dimitarvp

dimitarvp

You are better off having a dedicated supervisor just for these tasks. As @al2o3cr pointed out, you were linking them to the process of the request and that ends quickly very often.

Where Next?

Popular in Questions Top

jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement