Преглед на файлове

Do not start the Repo by default

theenglishway (time) преди 4 години
родител
ревизия
50ca31b77f
променени са 2 файла, в които са добавени 25 реда и са изтрити 2 реда
  1. 1 2
      lib/toy/application.ex
  2. 24 0
      lib/toy/features.ex

+ 1 - 2
lib/toy/application.ex

@@ -7,8 +7,7 @@ defmodule Toy.Application do
 
   def start(_type, _args) do
     children = [
-      # Start the Ecto repository
-      Toy.Repo,
+      Toy.Features,
       # Start the Telemetry supervisor
       ToyWeb.Telemetry,
       # Start the PubSub system

+ 24 - 0
lib/toy/features.ex

@@ -0,0 +1,24 @@
+defmodule Toy.Features do
+  use DynamicSupervisor
+  require Logger
+
+  def start_link(init_arg) do
+    DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
+  end
+
+  def start_repo() do
+    case DynamicSupervisor.start_child(__MODULE__, Toy.Repo) do
+      ok = {:ok, _} ->
+        ok
+
+      e = {:error, reason} ->
+        Logger.warning("Could not start Toy.Repo because : #{inspect(reason)}")
+        e
+    end
+  end
+
+  @impl true
+  def init(_init_arg) do
+    DynamicSupervisor.init(strategy: :one_for_one)
+  end
+end