Elixir的新手,但到目前为止为止仍然很喜欢:)
New to Elixir but loving it so far :)
我的很多混合任务都依赖于HTTPotion。
A lot of my mix tasks depend on HTTPotion.
我的 mix.exs 文件被声明为
def application do [ applications: [:logger, :cowboy, :plug, :httpotion, :poison], mod: {BiddingAgent, []} ] end所以HTTPotion.start是自动调用的。但是,当我运行诸如 mix campaign.list 之类的任务需要调用http请求时,我必须手动调用 HTTPotion.start 。
So HTTPotion.start is called automatically. However, when I run a task like mix campaign.list which needs to call an http request, I have to manually call HTTPotion.start.
确保我的混合任务启动正确流程的惯用方式是什么?
What is the idiomatic way to make sure the right processes are started for my mix tasks?
谢谢!
推荐答案您是对的,在启动脚本之外启动应用程序时,您确实需要启动
You're right, when starting the app outside of the startup script you do need to start the dependencies manually.
我更喜欢调用 Application 模块,而不是直接调用每个依赖项。
I prefer to call the Application module instead of each dependency directly.
将以下代码添加到任务模块内的 run 函数中。
Add the following code to the run function inside your task module.
{:ok, _started} = Application.ensure_all_started(:httpotion)如果有任何疑问,可以看看文档
If you have any doubt you can take a look at the documentation
编辑: T他描述的实践正在Ecto中使用
The practice described is being used in Ecto
-
> github/elixir-lang/ecto/blob/master/lib/mix/ecto.ex#L73
github/elixir-lang/ecto/blob/df13b1c64f8edd128cec1316336b20f3153eafa3/lib/mix/tasks/ecto.migrate.ex#L67
mix模块中包含一个模块,该模块提供了suresure_started方法。
A module is included in a the mix tasks which provides an ensure_started method.
更多推荐
开始混合任务过程依赖性
发布评论