开始混合任务过程依赖性 [英] Starting mix task process dependencies

查看:54
本文介绍了开始混合任务过程依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

https://github.com/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.

这篇关于开始混合任务过程依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆