So I have a phoneix app and its bundled in a container so by default it has no mix and using the procedure defined here https://hexdocs.pm/phoenix/releases.html for running migration using eval but for cases where I would like to trigger some task from the same image for example interact with data to fetch or import etc is there a way to just start the Ecto Repo and not all children for the app. The only thing that has worked for me is to do Application.ensure_all_started(@app)
before interacting with Ecto Repo but this also start the Web app which is not needed. In Ruby on Rails under rails rake would not start the webserver but load the environment to have DB available for interaction. Is it possible to have something like that ? Or is it ok to have the Web running (one of the concern is it will require its own resource while being idle). Ecto.Migator
seems to be special where load works for it but to execute some module intreracting with db need to do ensure_all_started/1
. I have added get_applications
to show a simple use case. Also if I had scheduled workers or some background jobs they might get triggered in this approach as well. so whats the best practice for this ? To have a separate container for tasks ? Use Mix instead of Elixir releases but then there is this Always use Releases
defmodule GhostRider.Release do
@moduledoc """
Release.
"""
@app :ghost_rider
def migrate do
load_app()
for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end
def rollback(repo, version) do
load_app()
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
def get_applications() do
Application.ensure_all_started(@app)
GhostRider.Repo.all(GhostRider.Integrated.Application) |> IO.inspect()
IO.puts("Finished task")
end
def create do
load_app()
end
defp repos do
Application.fetch_env!(@app, :ecto_repos)
end
defp load_app do
Application.load(@app)
end
end