gpcweb

gpcweb

More than one supervisor?

I’am new on elixir and I’m building an application that downloads some pdf files from s3. It works just fine using a supervisor and poolboy to manage the process, but I would like to retrieve all the links from a database, I’m seeing ecto but when I used It tells me that I need a supervisor.

This is my current model module

defmodule Pdf.Consumption do
  use Ecto.Schema
  alias Pdf.Consumption
  alias Pdf.Repo

  schema "consumptions" do
    field :invoice_url
  end

  def all do
    Consumption
    |> Repo.all
  end
end 

This is my current application module

defmodule Pdf do
  use Application

  def start(_type, _args) do
    poolboy_config = [
      {:name, {:local, pool_name()}},
      {:worker_module, Pdf.Worker},
      {:size, 2},
      {:max_overflow, 0}
    ]

  children = [:poolboy.child_spec(pool_name(), poolboy_config, [])]

  options = [
   strategy: :one_for_one,
   name: Pdf.Supervisor
  ]

  Supervisor.start_link(children, options)
end

  defp pool_name() do
    :pdf_pool
  end

  defp pdf_pool(link) do
     :poolboy.transaction(
       pool_name(),
       fn(pid) -> :gen_server.call(pid, link) end, :infinity
     )
  end

  def parallel_pool(links) do
    Enum.each( links, fn(link) -> spawn( fn() -> pdf_pool(link) end ) end )
  end
end

And I would like to make this on parallel_pool

def parallel_pool do
  Pdf.Consumption.all
  |>Enum.each( links, fn(link) -> spawn( fn() -> pdf_pool(link) end ) end )
end

So my question is, how can achieve this? I need another module that use Application?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

So there are really two questions here:

  1. How do you handle multiple supervisors
  2. What’s the best way to download a lot of files concurrently from S3.

I’m gonna answer #2. Poolboy is not the way to do this. The easiest way is to use the Elixir 1.4 Task.async_stream. function:

links
|> Task.async_stream(fn link ->
  process_link(link)
end, max_concurrency: 20)
|> Stream.run

This will download 20 links at a time. It will not return anything, I’m assuming that you’re writing to disk or something with process_link. If you need the results you would just |> Enum.to_list instead of Stream.run

gpcweb

gpcweb

Can you explain me why poolboy is not the way to do this?

NobbZ

NobbZ

Because it adds a dependency while the work can be done with stuff from the std-lib easily.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement