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 def handle_cast({:restart, :test, _}, state) do
65 Logger.debug("pleroma manually restarted")
66 {:noreply, Map.put(state, :need_reboot, false)}
69 def handle_cast({:restart, _, delay}, state) do
72 {:noreply, Map.put(state, :need_reboot, false)}
75 def handle_cast({:after_boot, _}, %{after_boot: true} = state), do: {:noreply, state}
77 def handle_cast({:after_boot, :test}, state) do
78 Logger.debug("pleroma restarted after boot")
79 state = %{state | after_boot: true, rebooted: true}
83 def handle_cast({:after_boot, _}, state) do
85 state = %{state | after_boot: true, rebooted: true}
89 defp do_restart(app) do
90 :ok = Application.ensure_started(app)
91 :ok = Application.stop(app)
92 :ok = Application.start(app)