Aguxez

Aguxez

Implement poolboy for external processes

I have an app that downloads songs from a playlist from Youtube, I’m using Porcelain with Youtube-dl, it was working correctly with a single user but I wanted to make it for more than one user (Still kinda private, so not much people) but I thought that, for example, if the app has 10 users at the same time downloading songs it would end up eating more resoures that desired, that’s where I thought about using Poolboy, I implemented it and it works how it’s supposed to UNTIL I have to download more songs.

Again, here’s how I implemented the download queue, first you pass the playlist ID then it fetch the songs on that playlist and splits them by 10 so each “user” will be downloading 10 songs at a time, when the 10 songs are downloaded then the next 10 ones are passed to the function that downloads them and so on until it’s all done.

Now, the problem, for testing I set up the size of the pool to 10 with a max_overflow of 12. The first 10 songs are downloaded correctly but once the next songs are pushed it doesn’t do anything. I’m guessing that poolboy doesn’t know that the process is already available and can be used by someone else? I’m not sure how I could make something to notify the availability of the process, the thing is that I don’t want any songs to be lost because a process wasn’t available at the time.

This is the GenServer in charge of downloading the songs.

defmodule Palsound.Service.Downloader do
  @moduledoc false

  use GenServer

  alias Porcelain.Process, as: Proc

  # API

  def start_link(_),
    do: GenServer.start_link(__MODULE__, [], [])

  def queue_and_download(songs, songs_path, thumb) do
    Enum.each(songs, fn(x) ->
      :poolboy.transaction(:worker_downloader, fn(pid) ->
        request = {:download, x, songs_path, thumb}
        GenServer.call(pid, request)
      end, :infinity)
    end)
  end

  # Server

  def init(state),
    do: {:ok, state}

  def handle_call({:download, song, songs_path, thumbnail}, _from, state) do
    thumbnail_value =
      case thumbnail do
        :no_thumbnail -> ""
        _ -> "--write-thumbnail"
      end

    %Proc{out: audio} =
      Porcelain.spawn(System.find_executable("youtube-dl"),
        ~w(-i --audio-format mp3 --extract-audio #{thumbnail_value}
          -o #{songs_path} #{song}), out: :stream)

    {:reply, audio, state}
  end
end

Most Liked

gregvaughn

gregvaughn

Don’t pass :infinity to :poolboy.transaction That is masking whatever your underlying problem is.

I’m not quite positive enough to say “never” but you almost never want to use an infinite timeout. Ever.

Aguxez

Aguxez

Thanks, someone pointed that out and suggested a couple of solutions.

  1. Report it back to users telling them to wait
  2. Make a timeout really long so that it exceeds the song download length (Not really recommended, probably like a last resource)
  3. Have each worker, before downloading the song, give me an stimate of how long it’ll take to download the song and use it as a timeout so I can stimate when they’ll be free.

I’d opt for the third solution, now I need to figure out how to get the stimate.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
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

We're in Beta

About us Mission Statement