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

New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement