logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

pleroma.ex (2883B)


  1. defmodule Restarter.Pleroma do
  2. use GenServer
  3. require Logger
  4. @init_state %{need_reboot: false, rebooted: false, after_boot: false}
  5. def start_link(_) do
  6. GenServer.start_link(__MODULE__, [], name: __MODULE__)
  7. end
  8. def init(_), do: {:ok, @init_state}
  9. def rebooted? do
  10. GenServer.call(__MODULE__, :rebooted?)
  11. end
  12. def rebooted do
  13. GenServer.cast(__MODULE__, :rebooted)
  14. end
  15. def need_reboot? do
  16. GenServer.call(__MODULE__, :need_reboot?)
  17. end
  18. def need_reboot do
  19. GenServer.cast(__MODULE__, :need_reboot)
  20. end
  21. def refresh do
  22. GenServer.cast(__MODULE__, :refresh)
  23. end
  24. def restart(env, delay) do
  25. GenServer.cast(__MODULE__, {:restart, env, delay})
  26. end
  27. def restart_after_boot(env) do
  28. GenServer.cast(__MODULE__, {:after_boot, env})
  29. end
  30. def handle_call(:rebooted?, _from, state) do
  31. {:reply, state[:rebooted], state}
  32. end
  33. def handle_call(:need_reboot?, _from, state) do
  34. {:reply, state[:need_reboot], state}
  35. end
  36. def handle_cast(:rebooted, state) do
  37. {:noreply, Map.put(state, :rebooted, true)}
  38. end
  39. def handle_cast(:need_reboot, %{need_reboot: true} = state), do: {:noreply, state}
  40. def handle_cast(:need_reboot, state) do
  41. {:noreply, Map.put(state, :need_reboot, true)}
  42. end
  43. def handle_cast(:refresh, _state) do
  44. {:noreply, @init_state}
  45. end
  46. # Don't actually restart during tests.
  47. # We just check if the correct call has been done.
  48. # If we actually restart, we get errors during the tests like
  49. # (RuntimeError) could not lookup Ecto repo Pleroma.Repo because it was not started or
  50. # it does not exist
  51. # See tests in Pleroma.Config.TransferTaskTest
  52. def handle_cast({:restart, :test, _}, state) do
  53. Logger.debug("pleroma manually restarted")
  54. {:noreply, Map.put(state, :need_reboot, false)}
  55. end
  56. def handle_cast({:restart, _, delay}, state) do
  57. Process.sleep(delay)
  58. do_restart(:pleroma)
  59. {:noreply, Map.put(state, :need_reboot, false)}
  60. end
  61. def handle_cast({:after_boot, _}, %{after_boot: true} = state), do: {:noreply, state}
  62. # Don't actually restart during tests.
  63. # We just check if the correct call has been done.
  64. # If we actually restart, we get errors during the tests like
  65. # (RuntimeError) could not lookup Ecto repo Pleroma.Repo because it was not started or
  66. # it does not exist
  67. # See tests in Pleroma.Config.TransferTaskTest
  68. def handle_cast({:after_boot, :test}, state) do
  69. Logger.debug("pleroma restarted after boot")
  70. state = %{state | after_boot: true, rebooted: true}
  71. {:noreply, state}
  72. end
  73. def handle_cast({:after_boot, _}, state) do
  74. do_restart(:pleroma)
  75. state = %{state | after_boot: true, rebooted: true}
  76. {:noreply, state}
  77. end
  78. defp do_restart(app) do
  79. :ok = Application.ensure_started(app)
  80. :ok = Application.stop(app)
  81. :ok = Application.start(app)
  82. end
  83. end