9
votes

New to Elixir but loving it so far :)

A lot of my mix tasks depend on HTTPotion.

My mix.exs file is declared as such

  def application do
    [
      applications: [:logger, :cowboy, :plug, :httpotion, :poison],
      mod: {BiddingAgent, []}
    ]
  end

So HTTPotion.start is called automatically. However, when I run a task like mix campaign.list which needs to call an http request, I have to manually call HTTPotion.start.

What is the idiomatic way to make sure the right processes are started for my mix tasks?

Thanks!

1
Have you added HTTPotion to your dependency list?Onorio Catenacci
Looking at mix test and seeing that you must have ExUnit.start in one of your exs files when running tests, I'd guess that you'll need to do the same thing with other applications that need to be started when running a mix task. I'd be happy if I was wrong.CoderDennis

1 Answers

19
votes

You're right, when starting the app outside of the startup script you do need to start the dependencies manually.

I prefer to call the Application module instead of each dependency directly.

Add the following code to the run function inside your task module.

{:ok, _started} = Application.ensure_all_started(:httpotion)

If you have any doubt you can take a look at the documentation

Edit: The practice described is being used in Ecto

A module is included in a the mix tasks which provides an ensure_started method.