如何在Ecto Postgrex驱动程序中设置连接重试计时器

时间:2017-05-20 08:34:00

标签: phoenix-framework ecto

当我的应用程序启动,并且数据库主机名无法解析时,Ecto / Postgrex驱动程序现在在同一秒内重试20次,然后Phoenix退出。这是错误:

failed to connect: ** (DBConnection.ConnectionError) tcp connect (odoodb:5432): non-existing domain - :nxdomain

我想控制重试之间的时间以及重试次数。我怎么做?在文档中找不到任何内容。

BTW:在docker中运行,因此DNS可以突然开始工作。

以下是我使用的软件版本:

  {:phoenix, "~> 1.3.0-rc"},
  {:phoenix_pubsub, "~> 1.0"},
  {:phoenix_ecto, "~> 3.0"},
  {:postgrex, ">= 0.0.0"},
  {:phoenix_html, "~> 2.6"},
  {:phoenix_live_reload, "~> 1.0", only: :dev},
  {:gettext, "~> 0.11"},
  {:cowboy, "~> 1.0"},
  {:distillery, "~> 1.0"},
  {:xain, "~> 0.6"},
  {:comeonin, "~> 3.0"},
  {:ueberauth_identity, "~> 0.2"},
  {:ueberauth_github, "~> 0.4"},
  {:ueberauth_google, "~> 0.5"},
  {:ueberauth_facebook, "~> 0.6"},
  {:bamboo, "~> 0.8"},
  {:timex, "~> 3.0"},
  {:scrivener_ecto, "~> 1.0"},

以下是退出的代码:

defmodule Backend.Application do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec

    # Define workers and child supervisors to be supervised
    children = [
      # Start the Ecto repository
      supervisor(Backend.Web.Repo, []),
      # Start the endpoint when the application starts
      supervisor(Backend.Web.Endpoint, []),
      # Start your own worker by calling: Backend.Worker.start_link(arg1, arg2, arg3)
      # worker(Backend.Worker, [arg1, arg2, arg3]),
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Backend.Supervisor]
    res = Supervisor.start_link(children, opts)
    IO.puts "######### run migrations..."
    Ecto.Migrator.run(Backend.Web.Repo, Path.join(["#{:code.priv_dir(:backend)}", "repo", "migrations"]), :up, all: true)
    IO.puts "######### migrations done..."
    res
  end
end

我认为它实际上是在Migrator.run中退出。这是错误:

 ######### run migrations...
 08:31:22.774 [info] Application backend exited: exited in: Backend.Application.start(:normal, [])
     ** (EXIT) an exception was raised:
         ** (DBConnection.ConnectionError) connection not available because of disconnection
             (db_connection) lib/db_connection.ex:925: DBConnection.checkout/2
             (db_connection) lib/db_connection.ex:741: DBConnection.run/3
             (db_connection) lib/db_connection.ex:1132: DBConnection.run_meter/3
             (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
             (ecto) lib/ecto/adapters/postgres/connection.ex:93: Ecto.Adapters.Postgres.Connection.execute/4
             (ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
             (ecto) lib/ecto/adapters/sql.ex:193: Ecto.Adapters.SQL.query!/5
             (ecto) lib/ecto/adapters/postgres.ex:86: anonymous fn/4 in Ecto.Adapters.Postgres.execute_ddl/3
             (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
             (ecto) lib/ecto/adapters/postgres.ex:86: Ecto.Adapters.Postgres.execute_ddl/3
             (ecto) lib/ecto/migrator.ex:43: Ecto.Migrator.migrated_versions/2
             (ecto) lib/ecto/migrator.ex:142: Ecto.Migrator.run/4
             (backend) lib/backend/application.ex:24: Backend.Application.start/2
             (kernel) application_master.erl:273: :application_master.start_it_old/4

所以我认为我的问题现在变成:我如何等待有效的连接,或者如何让迁移者等待呢?

此外,我仍然希望能够让司机在重试之间等待更长时间。

0 个答案:

没有答案