0

When starting an application the return value needs to be a pid or an error.

Is it possible to use applications for programs that are meant to only run through there processing once. Something like.

defmodule MyApp do
  use Application

  def start(_type, _args) do
    # Do stuff
    {:done, :normal}
  end
end
Peter Saxton
  • 4,466
  • 5
  • 33
  • 51
  • Applications are for long running programs that need to be robust... if this is meant as an one-off, like for example a console script, why wouldn't you model it as a script (.exs?). And if it is meant to be robust, why does the Application model trouble you? – bottlenecked Oct 03 '16 at 09:37
  • Why do you need an OTP application for it? – Aleksei Matiushkin Oct 03 '16 at 09:37
  • So the answer is to not use an application and make an escript? I'm not exactly sure what makes a correct mix project. perhaps the question is what does a mix project look like for a single run program. – Peter Saxton Oct 03 '16 at 09:40
  • 2
    You probably don't need to change the way mix sets your project up. If you need your app to stop, why not just [Application.stop](http://elixir-lang.org/docs/stable/elixir/Application.html#stop/1) it when you're done? – bottlenecked Oct 03 '16 at 09:44

1 Answers1

1

You're overcomplicating things @Peter. What you want is an Elixir script (exs file). Start with this example:

defmodule MyApp do
  def my_test_func do
    IO.puts "Hello world!"
  end
end

MyApp.my_test_func

Save that code as test1.exs. Then you can run it from the command prompt via elixir test1.exs What I'm saying is that you don't need a gen_server for a simple script.

You can find out more about interacting with the OS in the System docs and you can see a bit more on the topic of Elixir scripts here: http://elixir-lang.org/getting-started/introduction.html#running-scripts

s3cur3
  • 2,749
  • 2
  • 27
  • 42
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132