I am trying to make an HTTP request each second.
I have created this genserver.
defmodule Recorder do
use GenServer
def start_link(args) do
id = Map.get(args, :id)
GenServer.start_link(__MODULE__, args, name: id)
end
def init(state) do
schedule_fetch_call()
{:ok, state}
end
def handle_info(:jpeg_fetch, state) do
spawn(fn ->
IO.inspect("I am being called")
IO.inspect(DateTime.utc_now())
Fincher.request(:get, state.url) |> IO.inspect()
end)
schedule_fetch_call()
{:noreply, Map.put(state, :run, true)}
end
defp schedule_fetch_call do
Process.send_after(self(), :jpeg_fetch, 1000)
end
end
I am starting genserver with such state
defp get_children do
Enum.map([
%{
id: :hdipc,
url: "http://77.66.206.55/jpgmulreq/1/image.jpg?key=1516975535684&lq=1&COUNTER"
}
], fn(camera) ->
Supervisor.child_spec({Recorder, camera}, id: camera.id)
end)
end
end
Code is quite simple so I am posting everything.
Questions:
If I don’t spawn each request, this doesn’t even work how it supposed to work as each second. What If I don’t spawn it then how it will work how it suppose to work? each second?
suppose there are 10 seconds. (there will be more than that, infinite as it will run as time grows)
each second shooting an HTTP call and of course, it will take time to complete, what will happen to the next second? it simply means it will lose all the next seconds until the request is getting completed?
Is there any way possible to make each second a request and also handle the back pressure?