Implement poolboy for external processes

how-to-question

#1

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

#2

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.


#3

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.