wktdev

wktdev

I tried to learn Elixir a few years ago and stopped. I am trying again.
In the code below I have a GenServer that is started by a Supervisor. When I compile the code it works as expected - if the GenServer is manually killed the Supervisor restarts it.

This is good and does what I expect.

I now want to wrap my code as an “Application”.

Thus I go to mix.exs and in the application method I update the code to reference my Module

def application do
    [
      extra_applications: [:logger],
      mod: {App, []}
    ]
  end

When I compile my code I get the following error:

18:49:32.437 [notice] Application todos exited: exited in: App.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function App.start/2 is undefined (module App is not available)
            App.start(:normal, [])
            (kernel 8.5) application_master.erl:293: :application_master.start_it_old/4
** (Mix) Could not start application todos: exited in: App.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function App.start/2 is undefined (module App is not available)
            App.start(:normal, [])
            (kernel 8.5) application_master.erl:293: :application_master.start_it_old/4

I am not sure what the error is inferring is wrong nor do I know how to fix it. I would like to know how to fix it and why the error occurs.

My full code is below

todos/lib/todos.ex




defmodule App.Service do
  use GenServer

  def start_link(state) do
    GenServer.start_link(__MODULE__, state, name: __MODULE__)
  end

  def init(state) do
     {:ok, state}
  end

  def get_state(pid) do
     GenServer.call(pid, :get_state)
  end

  def set_state(pid,state) do
     GenServer.call(pid, {:set_state, state})
  end


  def handle_call(:get_state, _from, state) do
     {:reply, state, state}
  end


  def handle_call({:set_state, new_state}, _from, state)do
    {:reply,state,[new_state | state]}
  end
   
end

defmodule App.Supervisor do
  use Supervisor

  def start do
    Supervisor.start_link(__MODULE__, [])
  end

  def init(_) do
     children = [
      {App.Service,[]}
     ]
  Supervisor.init(children, strategy: :one_for_one)
  end
end

todos/lib/mix.exs


defmodule Todos.MixProject do
  use Mix.Project

  def project do
    [
      app: :todos,
      version: "0.1.0",
      elixir: "~> 1.14",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  def application do
    [
      extra_applications: [:logger],
      mod: {App, []}
    ]
  end

  defp deps do
    [
    ]
  end
end

Showing Posts 1 to 5

derek-zhou

derek-zhou

An application need to implement the Application behavior. You may want to see what the mix generator generate for you:

mix new hello_world --sup

And start from there.

wktdev

wktdev OP

Just curious, Is there a way to do that retroactively to a preexisting mix instance?

wktdev

wktdev OP

btw, thank you. I updated the application.exs file that came with the install and it seems to have fixed my problem

dimitarvp

dimitarvp

Yeah you can. Just make two identical projects – one without --sup and one with – and check the differences. You can then manually apply them to any project without the supervisor / application switch.

MohamedTharwat

MohamedTharwat

Hello Dear,

Can you share with me how you solved this issue as i face it from couple of days and cannot find a solution till now?

Thanks.

— All posts loaded —

Where Next? Top

Trending in Questions Top

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
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
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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
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
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
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews