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.

139 line
3.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Pleroma do
  5. @apps [
  6. :restarter,
  7. :ecto,
  8. :ecto_sql,
  9. :postgrex,
  10. :db_connection,
  11. :cachex,
  12. :flake_id,
  13. :swoosh,
  14. :timex,
  15. :fast_html
  16. ]
  17. @cachex_children ["object", "user", "scrubber", "web_resp"]
  18. @doc "Common functions to be reused in mix tasks"
  19. def start_pleroma do
  20. Pleroma.Config.Holder.save_default()
  21. Pleroma.Config.Oban.warn()
  22. Pleroma.Application.limiters_setup()
  23. Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
  24. unless System.get_env("DEBUG") do
  25. Logger.remove_backend(:console)
  26. end
  27. adapter = Application.get_env(:tesla, :adapter)
  28. apps =
  29. if adapter == Tesla.Adapter.Gun do
  30. [:gun | @apps]
  31. else
  32. [:hackney | @apps]
  33. end
  34. Enum.each(apps, &Application.ensure_all_started/1)
  35. oban_config = [
  36. crontab: [],
  37. repo: Pleroma.Repo,
  38. log: false,
  39. queues: [],
  40. plugins: []
  41. ]
  42. children =
  43. [
  44. Pleroma.Repo,
  45. Pleroma.Emoji,
  46. {Pleroma.Config.TransferTask, false},
  47. Pleroma.Web.Endpoint,
  48. {Oban, oban_config},
  49. {Majic.Pool,
  50. [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
  51. ] ++
  52. http_children(adapter)
  53. cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
  54. Supervisor.start_link(children ++ cachex_children,
  55. strategy: :one_for_one,
  56. name: Pleroma.Supervisor
  57. )
  58. if Pleroma.Config.get(:env) not in [:test, :benchmark] do
  59. pleroma_rebooted?()
  60. end
  61. end
  62. defp pleroma_rebooted? do
  63. if Restarter.Pleroma.rebooted?() do
  64. :ok
  65. else
  66. Process.sleep(10)
  67. pleroma_rebooted?()
  68. end
  69. end
  70. def load_pleroma do
  71. Application.load(:pleroma)
  72. end
  73. def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
  74. Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
  75. end
  76. def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
  77. prompt_message = "#{prompt} [#{defname || defval}] "
  78. input =
  79. if mix_shell?(),
  80. do: Mix.shell().prompt(prompt_message),
  81. else: :io.get_line(prompt_message)
  82. case input do
  83. "\n" ->
  84. case defval do
  85. nil ->
  86. shell_prompt(prompt, defval, defname)
  87. defval ->
  88. defval
  89. end
  90. input ->
  91. String.trim(input)
  92. end
  93. end
  94. def shell_info(message) do
  95. if mix_shell?(),
  96. do: Mix.shell().info(message),
  97. else: IO.puts(message)
  98. end
  99. def shell_error(message) do
  100. if mix_shell?(),
  101. do: Mix.shell().error(message),
  102. else: IO.puts(:stderr, message)
  103. end
  104. @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
  105. def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
  106. def escape_sh_path(path) do
  107. ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  108. end
  109. defp http_children(Tesla.Adapter.Gun) do
  110. Pleroma.Gun.ConnectionPool.children() ++
  111. [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
  112. end
  113. defp http_children(_), do: []
  114. end