ojinari
How to run custom tasks in production by manually calling them?
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?
- 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?
-
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? -
Are there other approaches?
I’m bewildered. Which one to use, in what circumstances? How to use them properly?
Most Liked
gpreston
Add-on to this which is not super clear from this section of the docs:
I was trying to forward command-line arguments directly to my Elixir code being run. Doing it the naive way doesn’t work for arbitrary unexpected numbers of arguments and other reasons:
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc(\"$1\", \"$2\")" # this doesn't even properly escape double quotes, and ways to do that within bash are not consistent across systems
There is a supported way to forward the command line arguments though:
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc" "$@"
The command line arguments can then be accessed in your Elixir code using System.argv(). Hope this helps anyone facing the same problem. This seems like a better solution to me for anything more complex than passing hard-coded values from the shell script.
John-Goff
Just pass them? Eg
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc(1, \"str\", :atom)"
You’re basically calling a function the same way as you would in IEx, so you can pass any elixir data type.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









