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

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
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
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement