elixir - Which OTP behavior should I use for an "endless" repetition of tasks? -
i want repeatedly run same sequence of operations on , on again next phoenix application (without crashing whole web-app if brakes in worker of course) , don't know wether should use genserver, elixir's tasks, agent or different haven't thought far.
when start phoenix app worker should start well, periodically pulls values of serial-connection, broadcasts them through phoenix channel, collects them until @save_interval
reached , calculates median, broadcasts median via different channel , writes influxdb. right have (kind of working) this:
def do_your_thing(serial_pid) stream.interval(@interval_live) |> get_new_values_from_serial(serial_pid) |> broadcast!("live-channel:#{@name}") |> enum.take(div(@interval_save, @interval_live)) |> calculate_medians() |> broadcast!("update-channel:#{@name}") |> write_to_database() do_your_thing(serial_pid) # repeat end
i'm starting figure otp stuff out , hope of me stumble right direction here.
you should use genserver sends messages after x seconds (60 seconds in example below):
defmodule myapp.worker use genserver def start_link() genserver.start_link(__module__, []) end def init([]) schedule_work() {:ok, []} end def handle_info(:work, state) state = do_work(state) schedule_work() {:noreply, state} end defp do_work(state) # work here , return state end defp schedule_work process.send_after(self(), :work, 60_000) end end
Comments
Post a Comment