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

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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
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
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
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

We're in Beta

About us Mission Statement