mrcasals

mrcasals

Hi all!

I’m starting my first project with Elixir and Phoenix. I have a function in a context that creates a record and fires some asynchronous code using Task.start. This task fetches data from a local URL and updates the newly created record.

I want to reuse this code for my seed data, but the process seems to finish before the Task.start has finished running, so the seed records are not being updated with the complete data.

The code works as expected from iex -S mix.

Is there any way to force the process to wait until the Tasks are finished, without using Task.async? The only thing that comes to mind is a loop that counts the records in the DB that have not yet been updated, and sleep for a second if there’s any, but I’d like to try to find a better solution.

Thanks!

Showing Posts 1 to 4

idi527

idi527

:waving_hand:

You can enter a receive block, which is what Task.await does. But for that you’d need to send your process a message on task completion to leave that block. The easiest way to do that would probably be Task.async, so why don’t you want to use it?

peerreynders

peerreynders

Refactoring the code so that it can be used with either Task.start/3 or Task.async/3 and Task.await/3 should be the preferred approach - that way async/await could be used in the seeding script.

A more “hackey” solution: start returns {:ok, pid}; monitor the pid with Process.monitor/1 and at the end of the script put a receive/1 for the {:DOWN, ref, :process, object, reason} message.

mrcasals

mrcasals OP

For clarity, here’s my code.

My seeds.exs file:

alias MyApp.Multimedia
urls = [...] # an array of strings representing URLs

# I try to fetch the URL from my DB, or I create it
# To avoid double seeding the dev DB
Enum.map(urls, fn url -> Multimedia.get_url!(url) || Multimedia.create_url(url) end)
defmodule MyApp.Multimedia do
  def create_url(url) do
    %Url{}
    |> Video.creation_changeset(%{url: url})
    |> create_url
  end

  def create_url(%Ecto.Changeset{} = changeset) do
    case Repo.insert(changeset) do
      {:ok, %Url{} = url} ->
        get_basic_url_info(url)
        {:ok, url}

      other_value ->
        other_value
    end
  end

  def get_basic_url_info(%Url{} = url) do
    Task.start(fn -> Devflix.Brain.create(url.url) end)
  end
end

The problem right now is that when the Enum.map section of the seeds file finishes, all other Tasks are deleted.

I read that the Task.async needs to be used together with Task.await, and I don’t need to check the result during the normal life cycle of the app, only in the seeding part. That’s why I didn’t want to use it, but maybe I understood it wrong?

I finally moved to a mini-loop that checks if the seed data is complete to let the tasks run correctly:

defmodule Loops do
  def while(0), do: :ok
  def while(_n) do
    Process.sleep(1000)
    while(Repo.aggregate(query(), :count, :id))
  end

  def query do
    from(v in Url, where: is_nil(v.data)) # v.data is set via a `Task`
  end
end

Loops.while(Repo.aggregate(Loops.query, :count, :id))

I don’t find it a bad solution, although I guess mine is not the most Elixir-esque one…

Thanks to both, @idi527 @peerreynders

dimitarvp

dimitarvp

What about Task.yield?

You will immediately avoid the very ugly pattern of waiting fixed amounts and then re-checking.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews