简体   繁体   English

在生产中运行混合任务

[英]Running mix tasks in production

I have a number of tasks and jobs that I would like to be able to execute manually on a production elixir application. 我有许多任务和工作,我希望能够在生产工具上手动执行。 Currently, I have been performing them by running the code inside the remote_console . 目前,我一直在通过运行remote_console的代码来执行它们。 I Attempted to run them by using /bin/my_app command MyModule my_func , but I get errors about resources being used by another Erlang node. 我尝试使用/bin/my_app command MyModule my_func来运行它们,但是我收到了另一个Erlang节点正在使用的资源的错误。

I would prefer to use the /bin/my_app command MyModule my_func style so I can run the bash scripts using ssh. 我更喜欢使用/bin/my_app command MyModule my_func样式,所以我可以使用ssh运行bash脚本。

What's is the recommended way to run remote tasks on Elixir application? 在Elixir应用程序上运行远程任务的推荐方法是什么?

Running mix tasks in production is not possible, since mix is not part of the application release. 由于mix不是应用程序版本的一部分,因此无法在生产中运行mix任务。

However, it is obviously possible to include any code in the final application release, so you can add the code that would run migrations on app start. 但是,显然可以在最终的应用程序版本中包含任何代码,因此您可以添加将在app start上运行迁移的代码。

Documentation for Ecto.Migrator module contains a sample migration runner : Ecto.Migrator模块的文档包含一个示例迁移运行器

defmodule MyApp.Release do
  @app :my_app

  def migrate do
    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  defp repos do
    Application.load(@app)
    Application.fetch_env!(@app, :ecto_repos)
  end
end

Then, a command to run migration can be included as part of the release start up: 然后,作为发布启动的一部分,可以包含运行迁移的命令:

bin/my_app eval "MyApp.Release.migrate"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM