dod

dod

Hi there!
Which approach would you choose for the following:
App receives events notification from external source. As long as notification received, app needs to collect data from external API. After data from API received the call to API has to be repeated after 10 sec. And repeated again after 20 sec. An again after 40 sec and so on. In other words each notification should trigger N calls each call should be made after 10 x 2^n seconds.
Notifications occurs randomly with random rate. If at some point of time API call result in stop command no further calls has to be done.

What would you use and how would you structure an application? Supervised Tasks, GenStage, GenServer, would you spawn a new process for each call or would you track timings in single process? Could you provide architecture example?

Thanks!

First 7 of 7 Posts Switch mode

marcdel

marcdel

As usual, I think it depends. If I’m understanding the problem correctly though, what I might start with is a DynamicSupervisor for the notification handler that spawns a new GenServer for each notification. Each GenServer could then make its API call and use Process.send_after to queue up the next call in x seconds. If it receives a stop, that one GenServer can terminate without affecting the others.

Qqwy

Qqwy

TypeCheck Core Team

Here are some questions that probably require an answer before a choice can be made:

  • What should happen on app restart or (partial) crash? Is it required to continue the sending of these requests at that case, or is it okay to drop them in cases like these?

  • How expensive will these API calls be? Will there be other work that has to be calculated locally to send it to the API? How much storage will be required to keep track of the notification-data that will be used in some way to invoke the API? (What kind of data are we talking about?)

  • How tight is the time window? Is it a problem if at some point a call is a second late?

  • What should the app do if the API is not responding or crashing when you call it?

  • How will the app receive these notifications? By an external party making a request, or does it have to fetch the data itself?

  • What kind of parties will create these notifications? Will there only be a couple hundreds per minute at best, or possibly millions?

dod

dod OP

I’ve been thinking same way, except spawning Tasks, which would init with delay arg, sleep, send message through Erlang port, await message using receive function and spawn another Task with new delay

kokolegorille

kokolegorille

Maybe a simple GenServer can do this… it can send a message to itself based on incremental time.

defmodule Demo.Worker do
  use GenServer
  alias __MODULE__
  @period 10_000
  defstruct k: 1

  def start_link(), do: GenServer.start_link(__MODULE__, nil)
  def stop(pid), do: GenServer.cast(pid, :stop)

  @impl GenServer
  def init(_args) do
    send(self(), :tick)
    {:ok, %Worker{}}
  end

  @impl GenServer
  def handle_cast(:stop, state), do: {:stop, :normal, state}

  @impl GenServer
  def handle_info(:tick, %{k: k} = state) do
    IO.puts "call API... #{k}"

    Process.send_after(self(), :tick, k * @period)
    {:noreply, %{state | k: k + 1}}
  end
end
dod

dod OP

For simplicity it is OK to drop all work in progress and start with a blank state, awaiting incoming notifications.

Well, the data received from API should be processed and results should be used to determine should there be another messages to API ot not, however nothing too expensive.

Basically, notification exposes new device ID and requests to API are made using this ID to get sensor reading (just an integer). Received data together with previous readings are then used to make a decision: send another request later on with increased time delay, stop further request and dispose device readings or make final request and also dispose data.

Not a problem at all. Such delays wont make a lot of difference and not something I would care of.

Timeout, then repeat attempt again with next time interval. API plays leading role here.

It will use Port to external C lib which is capable to receive additional payload with request and send it back unchanged together with result. I plan to use GenServer as a boundary to this API which would pass PID of the caller to API and once response received send it via message to initial caller. Does that makes sense?

Yup not more than a 100 per minute at peak and 2-5 at average , however each ID received in notification would live for up to 10 hours. Which means that there are approximately 3000-5000 devices scheduled for API calls. Not that much.

dod

dod OP

Yup, thanks, for detailed example :+1:

Qqwy

Qqwy

TypeCheck Core Team

Based on the responses you gave to the answers, I think I would build a bunch of GenServers that send themselves timed messages similar to what @kokolegorille proposed. GenStage is less helpful here because you are not working with a sequence of data transformations (and want to send multiple messages at timed intervals based on a single input).

I’d suggest starting one GenServer per incoming notification (or maybe one per sensor). This allows them to fail/succeed independently from one another.

Calls to the API I would do through a circuit breaker to ensure that the API is not swamped when it has problems.

I don’t think a more complicated architecture is necessary for this situation :slight_smile:.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement