How to create and run a custom task in Phoenix? Like "rake something" in Ruby

What prevents those users from doing a ./bin/myapp stop? I mean you can prevent typos by letting the user use distilleries custom commands, which you created. You can obscure the actual shell command from the user by wrapping the distillery commands further into custom shell commands. In the end you still need to send rpc commands to the running erlang vm.

1 Like

Absence of intention to do so.


How is bin/myapp foo different from rake foo?

Both are wrappers around a single function, which gets called and then calls more functions which calls more, and so on. At the end the result is determined by the functions called in this process.

In both cases you could have done the same by manually connecting to your database, issuing some SQL, altering values by hand or editing files by hand, but you used a command to make it easier.

If you prefer rake, then use it to wrap the calls to bin/myapp

With rake foo I can only run the tasks that exist in your app.

bin/myapp foo – isn’t this a REPL?

What do you mean by “like has”? How to get it to possess it?

With bin/myapp foo you can as well only call those custom commands that you have configured to be available.

To be honest, I think using the bin/myapp foo approach via custom commands is even cleaner than rake. Since in rake you can in theory invoke private tasks or intermediate tasks directly. You can not do this with distilleries approach.

And no, bin/myapp foo does not exist at all, unless you have created a custom command for it. So its not a REPL unless you want it to. its just a command you invoke from your shell.

1 Like

[quote=“NobbZ, post:26, topic:10805”]
And no, bin/myapp foo does not exist at all, unless you have created a custom command for it.[/quote]

How to do so?

And what’s “bin/myapp” exactly? There’s no bin/… directory in my app

We provided links which build such a custom command with ecto migration as an example straight from the distillery docs. You confirmed that you use distillery. Where that file lives and how it is named exactly depends on your exact configuration.

1 Like

Then “remote_console” isn’t equivalent to ruby “rake”. It’s equivalent to “rails console”. And therefore, both “remote_console” and “rails console” are hacking as they’ll allow me to call any method of any module in my application. Unlike “rake”.

I don’t want that. I want a way to call a method or run a task of my app similar to “rake”

Distillery’s custom commands are not a remote console (unless you want them to). remote_console is one of the commands built-in with distillery (bin/myapp remote_console). Another built-in is running any module/fun combinations by bin/myapp command Elixir.Some.Module fun, which will only run the function and not open any console session. Now you can use “custom commands” to alias predefined mod/fun combinations to shorter commands. E.g. bin/myapp migrate, which could alias the longer command version. Custom commands trigger shell scripts so you could even be more elaborate than plain aliasing another distillery command.

So you definitely can do things like rake sometask by adding custom commands to distillery, which are called like bin/myapp sometask and those can do whatever you implement in the shell script with simply calling a function on a module being the simplest.

All this is described in even more detail in the distillery docs, which were linked earlier in this topic. Maybe take a look at those as well.


Do note too, you don’t need the bin part if you are in the bin directory. ^.^