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?

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement