You dont need to run bin/orif/ foregound
or bin/orif start
. You only need run your task. In my case, I have this task
defmodule ContentProxy.ReleaseTasks do
@otp_app :content_proxy
defp init(app) do
IO.puts "Starting dependencies.."
{:ok, _} = Application.ensure_all_started(app)
end
def seed do
init(@otp_app)
seed_data = seed_file_path(@otp_app)
|> File.read!()
|> Jason.decode!()
Enum.each seed_data, fn(content) ->
ContentProxy.Resources.create_content(content)
IO.puts("DOCUMENT #{content["name"]} INSERTED")
end
stop()
end
defp seed_file_path(app),
do: priv_dir(app, ["seed_data", "contents.json"])
defp priv_dir(app, path) when is_list(path) do
case :code.priv_dir(app) do
priv_path when is_list(priv_path) or is_binary(priv_path) ->
Path.join([priv_path] ++ path)
{:error, :bad_name} ->
raise ArgumentError, "unknown application: #{inspect app}"
end
end
defp stop do
IO.puts "Success!"
:init.stop()
end
end
inside of the terminal session, I run my task typing _build/prod/rel/content_proxy/bin/content_proxy command "Elixir.ContentProxy.ReleaseTasks" seed
and its works like a charm!
It is worth noting {:ok, _} = Application.ensure_all_started(app)
already start to you all children process that you’ve configured in application.ex
I’ve notice that if I run the app with start
or foreground
before run command
, when I run command "Elixir.ContentProxy.ReleaseTasks" seed
I get the following error:
22:01:18.737 [error] Failed to start Ranch listener ContentProxyWeb.Endpoint.HTTP in :ranch_tcp:listen([{:cacerts, :...}, {:key, :...}, {:cert, :...}, :inet6, {:port, 4000}]) for reason :eaddrinuse (address already in use)
22:01:18.759 [info] Application content_proxy exited: ContentProxy.Application.start(:normal, []) returned an error: shutdown: failed to start child: ContentProxyWeb.Endpoint
** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, ContentProxyWeb.Endpoint.HTTP}
** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
** (EXIT) {:listen_error, ContentProxyWeb.Endpoint.HTTP, :eaddrinuse}
22:01:18.762 [info] Application mongodb exited: :stopped
22:01:18.772 [info] Application db_connection exited: :stopped
22:01:18.772 [info] Application connection exited: :stopped
22:01:18.773 [info] Application gettext exited: :stopped
22:01:18.773 [info] Application poolboy exited: :stopped
22:01:18.773 [info] Application absinthe_relay exited: :stopped
22:01:18.773 [info] Application jason exited: :stopped
22:01:18.774 [info] Application atomic_map exited: :stopped
22:01:18.774 [info] Application absinthe_plug exited: :stopped
22:01:18.774 [info] Application absinthe exited: :stopped
22:01:18.774 [info] Application phoenix exited: :stopped
22:01:18.775 [info] Application eex exited: :stopped
22:01:18.775 [info] Application phoenix_pubsub exited: :stopped
22:01:18.775 [info] Application distillery exited: :stopped
22:01:18.776 [info] Application runtime_tools exited: :stopped
22:01:18.776 [info] Application artificery exited: :stopped
22:01:18.777 [info] Application plug_cowboy exited: :stopped
22:01:18.777 [info] Application cowboy exited: :stopped
22:01:18.777 [info] Application cowlib exited: :stopped
22:01:18.778 [info] Application ranch exited: :stopped
22:01:18.778 [info] Application ssl exited: :stopped
22:01:18.778 [info] Application public_key exited: :stopped
22:01:18.779 [info] Application asn1 exited: :stopped
22:01:18.779 [info] Application phoenix_ecto exited: :stopped
22:01:18.779 [info] Application plug exited: :stopped
22:01:18.780 [info] Application mime exited: :stopped
22:01:18.780 [info] Application plug_crypto exited: :stopped
22:01:18.780 [info] Application ecto exited: :stopped
=INFO REPORT==== 27-Jan-2019::22:01:18.817574 ===
application: logger
exited: stopped
type: temporary
{"init terminating in do_boot",{{badmatch,{error,{content_proxy,{{shutdown,{failed_to_start_child,'Elixir.ContentProxyWeb.Endpoint',{shutdown,{failed_to_start_child,{ranch_listener_sup,'Elixir.ContentProxyWeb.Endpoint.HTTP'},{shutdown,{failed_to_start_child,ranch_acceptors_sup,{listen_error,'Elixir.ContentProxyWeb.Endpoint.HTTP',eaddrinuse}}}}}}},{'Elixir.ContentProxy.Application',start,[normal,[]]}}}}},[{'Elixir.ContentProxy.ReleaseTasks',init,1,[{file,"lib/content_proxy/release_tasks.ex"},{line,7}]},{'Elixir.ContentProxy.ReleaseTasks',seed,0,[{file,"lib/content_proxy/release_tasks.ex"},{line,16}]},{init,start_em,1,[]},{init,do_boot,3,[]}]}}
init terminating in do_boot ({{badmatch,{error,{content_proxy,{_}}}},[{Elixir.ContentProxy.ReleaseTasks,init,1,[{_},{_}]},{Elixir.ContentProxy.ReleaseTasks,seed,0,[{_},{_}]},{init,start_em,1,[]},{init
Crash dump is being written to: erl_crash.dump...done