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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement