Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.8KB

  1. defmodule Restarter.Pleroma do
  2. use GenServer
  3. require Logger
  4. def start_link(_) do
  5. GenServer.start_link(__MODULE__, [], name: __MODULE__)
  6. end
  7. def init(_), do: {:ok, %{need_reboot?: false}}
  8. def need_reboot? do
  9. GenServer.call(__MODULE__, :need_reboot?)
  10. end
  11. def need_reboot do
  12. GenServer.cast(__MODULE__, :need_reboot)
  13. end
  14. def refresh do
  15. GenServer.cast(__MODULE__, :refresh)
  16. end
  17. def restart(env, delay) do
  18. GenServer.cast(__MODULE__, {:restart, env, delay})
  19. end
  20. def restart_after_boot(env) do
  21. GenServer.cast(__MODULE__, {:after_boot, env})
  22. end
  23. def handle_call(:need_reboot?, _from, state) do
  24. {:reply, state[:need_reboot?], state}
  25. end
  26. def handle_cast(:refresh, _state) do
  27. {:noreply, %{need_reboot?: false}}
  28. end
  29. def handle_cast(:need_reboot, %{need_reboot?: true} = state), do: {:noreply, state}
  30. def handle_cast(:need_reboot, state) do
  31. {:noreply, Map.put(state, :need_reboot?, true)}
  32. end
  33. def handle_cast({:restart, :test, _}, state) do
  34. Logger.debug("pleroma manually restarted")
  35. {:noreply, Map.put(state, :need_reboot?, false)}
  36. end
  37. def handle_cast({:restart, _, delay}, state) do
  38. Process.sleep(delay)
  39. do_restart(:pleroma)
  40. {:noreply, Map.put(state, :need_reboot?, false)}
  41. end
  42. def handle_cast({:after_boot, _}, %{after_boot: true} = state), do: {:noreply, state}
  43. def handle_cast({:after_boot, :test}, state) do
  44. Logger.debug("pleroma restarted after boot")
  45. {:noreply, Map.put(state, :after_boot, true)}
  46. end
  47. def handle_cast({:after_boot, _}, state) do
  48. do_restart(:pleroma)
  49. {:noreply, Map.put(state, :after_boot, true)}
  50. end
  51. defp do_restart(app) do
  52. :ok = Application.ensure_started(app)
  53. :ok = Application.stop(app)
  54. :ok = Application.start(app)
  55. end
  56. end