1 defmodule Restarter.Pleroma do
6 @init_state %{need_reboot: false, rebooted: false, after_boot: false}
9 GenServer.start_link(__MODULE__, [], name: __MODULE__)
12 def init(_), do: {:ok, @init_state}
15 GenServer.call(__MODULE__, :rebooted?)
19 GenServer.cast(__MODULE__, :rebooted)
23 GenServer.call(__MODULE__, :need_reboot?)
27 GenServer.cast(__MODULE__, :need_reboot)
31 GenServer.cast(__MODULE__, :refresh)
34 def restart(env, delay) do
35 GenServer.cast(__MODULE__, {:restart, env, delay})
38 def restart_after_boot(env) do
39 GenServer.cast(__MODULE__, {:after_boot, env})
42 def handle_call(:rebooted?, _from, state) do
43 {:reply, state[:rebooted], state}
46 def handle_call(:need_reboot?, _from, state) do
47 {:reply, state[:need_reboot], state}
50 def handle_cast(:rebooted, state) do
51 {:noreply, Map.put(state, :rebooted, true)}
54 def handle_cast(:need_reboot, %{need_reboot: true} = state), do: {:noreply, state}
56 def handle_cast(:need_reboot, state) do
57 {:noreply, Map.put(state, :need_reboot, true)}
60 def handle_cast(:refresh, _state) do
61 {:noreply, @init_state}
64 # Don't actually restart during tests.
65 # We just check if the correct call has been done.
66 # If we actually restart, we get errors during the tests like
67 # (RuntimeError) could not lookup Ecto repo Pleroma.Repo because it was not started or
69 # See tests in Pleroma.Config.TransferTaskTest
70 def handle_cast({:restart, :test, _}, state) do
71 Logger.debug("pleroma manually restarted")
72 {:noreply, Map.put(state, :need_reboot, false)}
75 def handle_cast({:restart, _, delay}, state) do
78 {:noreply, Map.put(state, :need_reboot, false)}
81 def handle_cast({:after_boot, _}, %{after_boot: true} = state), do: {:noreply, state}
83 # Don't actually restart during tests.
84 # We just check if the correct call has been done.
85 # If we actually restart, we get errors during the tests like
86 # (RuntimeError) could not lookup Ecto repo Pleroma.Repo because it was not started or
88 # See tests in Pleroma.Config.TransferTaskTest
89 def handle_cast({:after_boot, :test}, state) do
90 Logger.debug("pleroma restarted after boot")
91 state = %{state | after_boot: true, rebooted: true}
95 def handle_cast({:after_boot, _}, state) do
97 state = %{state | after_boot: true, rebooted: true}
101 defp do_restart(app) do
102 :ok = Application.ensure_started(app)
103 :ok = Application.stop(app)
104 :ok = Application.start(app)