I compile and deploy my Phoenix projects manually, with no third-party library such distillery, and with a help of the commands “mix compile” and “mix release”, basically.
I need a way to run custom commands or tasks on a server in the production mode. It’d be similar to running migrations. But I’ve bewildered as for how it’s done. A project itself may or may not be up and running when a command or task is being executed. And I need to be able to pass some arguments to a said command-task.
How to do it?
(1) I’m aware of
defmodule Mix.Tasks.MyTask1 do .... but how would I do it in production, on a server? Is this even a proper and recommended way to run them on a server in production?
(2) I’m aware of bin/my_app eval 'MyProject.MyTask1' but why does it exist if there’s the other abovementioned approach? Again, is this one a proper and recommended way to run them on a server in production?
(3) Are there other approaches?
I’m bewildered. Which one to use, in what circumstances? How to use them properly?
There are a few ways to accomplish this that I'm aware of, each has their pros/cons. Defining custom mix tasks, for example, is simple, but in your case, Mix
is not available on a built+compiled release.
The solution I've usually landed on is to support a finite number of tasks (e.g. migrations): these are things that must be run occasionally/manually on a live production instance. This is a bit more structured than a regular mix task, but it's written a way that works without mix. See the Phoenix docs for an example, but it boils down to calling the necessary functions from a regular module instead of via a mix task. This assumes that you are able to connect to a running instance of your app via iex
(e.g. by SSH'ing into the box and running ./bin/myapp remote
or similar).
This question may not generate high quality answers because any solutions are somewhat subjective.