greenz1

greenz1

Help with Elixir app management

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?

Most Liked

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?

Last Post!

greenz1

greenz1

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?

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement