在Elixir应用程序中配置多个repos

时间:2016-03-15 10:30:29

标签: elixir supervisor ecto

我正在尝试使用Ecto在我的Elixir应用中配置两个回购 我需要帮助来配置它们,以便使用one_for_one策略独立监督它们;我认为这是正确的,并且意味着使用它们的进程将重新启动

Repo-A和Repo-B

mix.exs setup

def application do
  [applications: [:logger, :tds, :tds_ecto, :ecto, :httpoison, :csvlixir],
   mod: {MyApp, []}]
end

MyApp_app.ex

以下代码段:

def start(_type, _args) do
    import Supervisor.Spec, warn: false

   children = [
   supervisor(MyApp.Repo-A, []), 
   worker(Task, [MyAppModule, :work, []], restart: :temporary),
   supervisor(MyApp.Repo-B, []), 
   worker(Task, [MyAppModule, :work, []], restart: :temporary)
 ]

 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
 # for other strategies and supported options
 opts = [strategy: :one_for_one, name: MyApp.Supervisor]
 Supervisor.start_link(children, opts)
end

defmodule Repo-A do
  use Ecto.Repo, otp_app: :myapp
end

defmodule Repo-B do
  use Ecto.Repo, otp_app: :myapp
end

当我mix run时,我得到以下内容 - 我不确定如何正确定义 id

** (Mix) Could not start application myapp: exited in: MyApp.start(:normal, [])
 ** (EXIT) an exception was raised:
     ** (ArgumentError) duplicated id Task found in the supervisor  specification, please explicitly pass the :id option when defining this worker/supervisor

1 个答案:

答案 0 :(得分:4)

您可以将id arg添加到opts关键字列表中,例如:

worker(Task, [MyAppModule, :work, []], restart: :temporary, id: :my_app_module_1)