开始混合任务过程依赖性

编程入门 行业动态 更新时间:2024-10-27 16:29:49
本文介绍了开始混合任务过程依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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.

更多推荐

开始混合任务过程依赖性

本文发布于:2023-11-30 21:17:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:依赖性   过程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!