【问题标题】:Custom mix task in Phoenix appPhoenix 应用中的自定义混音任务
【发布时间】:2017-09-24 10:24:15
【问题描述】:

当我在使用某些外部库(例如 https://github.com/knrz/geocoder)的 Phoenix 应用程序(我认为它甚至与 Phoenix 无关,但仍然)中运行自定义 Mix 任务时,我得到了

** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    :erlang.send(:geocoder_workers, {:"$gen_cast", {:cancel_waiting, #Reference<0.0.1.13074>}}, [:noconnect])

直到我添加

Application.ensure_all_started(:geocoder)

到混合任务。所以我的问题是为什么我的所有依赖项都没有自动启动?是我做错了吗?

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    在 Elixir 1.11 中,您可以使用 @requirements 模块属性,如 docs for Mix.Task 中所述:

    @requirements ["app.start"]
    

    这相当于在您的run 函数中调用Mix.Task.run "app.start"

    【讨论】:

      【解决方案2】:

      您是对的,您的应用程序的依赖项默认情况下不会在 Mix 任务中启动。它们需要手动启动。启动所有应用程序依赖项的最简单方法是调用Mix.Task.run("app.start")(或Application.ensure_all_started(:my_app),如果Mix 不可用)。这样,mix.exs 文件中列出的所有应用程序都将启动(如果它们尚未运行)。

      这记录在 Phoenix Framework 网站上的 Mix Tasks 页面末尾附近:

      如果您想让您的新混音任务使用您应用程序的 基础设施,您需要确保应用程序在何时启动 正在执行混合任务。如果您需要,这特别有用 从 mix 任务中访问您的数据库。值得庆幸的是,混合使 这对我们来说真的很容易:

      def run(_args) do
        Mix.Task.run "app.start"
        Mix.shell.info "Now I have access to Repo and other goodies!"
        ...
      end
      

      【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2019-09-20
      • 2017-02-23
      • 2018-11-09
      • 1970-01-01
      • 2017-10-16
      • 2021-10-03
      • 2019-06-24
      • 1970-01-01
      相关资源
      最近更新 更多