greenz1

greenz1

I am building an Elixir app for prod. It has a :observer.start layout as given below

My app’s flow is as follows.
Step 1: Workers under Elixir.AppName.MQTT.Supervisor receive some message.
Step 2: This message is sent to Elixir.AppName.Server
Step 3: Elixir.AppName.Server starts a worker dynamically under Elixir.AppName.on.Supervisor.
Step 4: The worker receives the message and acts accordingly
Let us call the worker, which is a GenServer, WORKER

My problem is that every time I have to perform some task in WORKERs I end up using send self(), message and having a corresponding handle_info in the WORKER.

I feel there is some issue with my current approach as I cannot utilize functions like GenServer.call or GenServer.cast

How should I remodel the application so that it makes use of other GenServer functions and callbacks?

Showing Posts 1 to 4

kokolegorille

kokolegorille

Without code it is hard to tell, but let me ask You why You would want to call or cast inside the GenServer?

It’s the API side that use those functions.

The server side has access to all private functions, and knows its state, in which case would You need to make intra server calls?

greenz1

greenz1 OP

I want to use call mainly because the worker is doing some CRUD operation in DB. I want to block the worker until the task is done.

By API side you mean something that a client(user or application) can use, right?

No intra-server calls have been planned.

Code structure is as follows

AppName.Server

defmodule AppName.Server do
    use GenServer

    def start_link do
        GenServer.start_link/3
    end

    def init/1 do
        send self(), :start_supervisors
        {:ok, state}
    end

    def handle_info/2 do
        # :start_supervisors is the message being handled
        child_spec = %{...}
        Supervisor.start_child(AppName.Supervisor, child_spec)
        {:noreply, state}
    end
end

AppName.on.Worker

defmodule AppName.on.Worker do
    use GenServer

    def start_link do
        # receives the message as argument
        GenServer.start_link/3
    end

    def init/1 do
        send self(), {:check_args, message_from_mqtt_as_map}
        {:ok, state}
    end

    def handle_info({:check_args, message_from_mqtt_as_map}, state) do
        case message_from_mqtt_as_map["action"] do
            :create ->
                do_create(message_from_mqtt_as_map)
                send self(), :some_task
            :remove ->
                do_remove(message_from_mqtt_as_map)
                send self(), :some_other_task
            :update ->
                do_update(message_from_mqtt_as_map)
                send self(), :yet_some_other_task
        end
        {:noreply, state}
    end

    def handle_info/2 do
        # other handle infos for :some_task, :some_other_task, :yet_some_other_task
    end

    # helper functions for the task
end
kokolegorille

kokolegorille

Maybe You can do like this?

# Maybe there is a typo in your module name :slight_smile: 

defmodule AppName.on.Worker do
    use GenServer

    # THIS IS API

    def start_link do
        # receives the message as argument
        GenServer.start_link/3
    end

    def some_task, do: GenServer.call ...

    # THIS IS SERVER SIDE

    def init/1 do
        send self(), {:check_args, message_from_mqtt_as_map}
        {:ok, state}
    end

    # Wherever You need it...
    def handle_call(:some_task, _from, _state) do
      perform_some_task()
    end

    def handle_info({:check_args, message_from_mqtt_as_map}, state) do
        case message_from_mqtt_as_map["action"] do
            :create ->
                do_create(message_from_mqtt_as_map)
                perform_some_task()
           ...
        end
        {:noreply, state}
    end

    # helper functions for the task
    defp perform_some_task, do: ...
end

BTW Why do You need to do this

send self(), :some_task

instead of a simple function call?

greenz1

greenz1 OP

send self(), :some_task is used so that the same task can be initialized by some other process.

Yes, the module name was obfuscated thus the typo and AppName.on.Worker should be AppName.On.Worker :sweat_smile:

As the some_task() cannot be called from within, so, from which process should I call AppName.On.Worker.some_task?

— 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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews