1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Migrators.Support.BaseMigrator do
7 Base background migrator functionality.
10 @callback perform() :: any()
11 @callback retry_failed() :: any()
12 @callback feature_config_path() :: list(atom())
13 @callback query() :: Ecto.Query.t()
14 @callback fault_rate_allowance() :: integer() | float()
16 defmacro __using__(_opts) do
24 alias __MODULE__.State
28 @behaviour Pleroma.Migrators.Support.BaseMigrator
30 defdelegate data_migration(), to: State
31 defdelegate data_migration_id(), to: State
32 defdelegate state(), to: State
33 defdelegate persist_state(), to: State, as: :persist_to_db
34 defdelegate get_stat(key, value \\ nil), to: State, as: :get_data_key
35 defdelegate put_stat(key, value), to: State, as: :put_data_key
36 defdelegate increment_stat(key, increment), to: State, as: :increment_data_key
38 @reg_name {:global, __MODULE__}
40 def whereis, do: GenServer.whereis(@reg_name)
45 GenServer.start_link(__MODULE__, nil, name: @reg_name)
54 {:ok, nil, {:continue, :init_state}}
58 def handle_continue(:init_state, _state) do
59 {:ok, _} = State.start_link(nil)
61 data_migration = data_migration()
62 manual_migrations = Config.get([:instance, :manual_data_migrations], [])
65 Config.get(:env) == :test ->
68 is_nil(data_migration) ->
69 message = "Data migration does not exist."
70 update_status(:failed, message)
71 Logger.error("#{__MODULE__}: #{message}")
73 data_migration.state == :manual or data_migration.name in manual_migrations ->
74 message = "Data migration is in manual execution or manual fix mode."
75 update_status(:manual, message)
76 Logger.warning("#{__MODULE__}: #{message}")
78 data_migration.state == :complete ->
79 on_complete(data_migration)
82 send(self(), :perform)
89 def handle_info(:perform, state) do
92 update_status(:running)
93 put_stat(:iteration_processed_count, 0)
94 put_stat(:started_at, NaiveDateTime.utc_now())
98 fault_rate = fault_rate()
99 put_stat(:fault_rate, fault_rate)
100 fault_rate_allowance = fault_rate_allowance()
106 is_float(fault_rate) and fault_rate <= fault_rate_allowance ->
108 Done with fault rate of #{fault_rate} which doesn't exceed #{fault_rate_allowance}.
109 Putting data migration to manual fix mode. Try running `#{__MODULE__}.retry_failed/0`.
112 Logger.warning("#{__MODULE__}: #{message}")
113 update_status(:manual, message)
114 on_complete(data_migration())
117 message = "Too many failures. Try running `#{__MODULE__}.retry_failed/0`."
118 Logger.error("#{__MODULE__}: #{message}")
119 update_status(:failed, message)
126 defp on_complete(data_migration) do
127 if data_migration.feature_lock || feature_state() == :disabled do
129 "#{__MODULE__}: migration complete but feature is locked; consider enabling."
134 Config.put(feature_config_path(), :enabled)
139 @doc "Approximate count for current iteration (including processed records count)"
140 def count(force \\ false, timeout \\ :infinity) do
141 stored_count = get_stat(:count)
143 if stored_count && !force do
146 processed_count = get_stat(:processed_count, 0)
147 max_processed_id = get_stat(:max_processed_id, 0)
148 query = where(query(), [entity], entity.id > ^max_processed_id)
150 count = Repo.aggregate(query, :count, :id, timeout: timeout) + processed_count
151 put_stat(:count, count)
158 def failures_count do
159 with {:ok, %{rows: [[count]]}} <-
161 "SELECT COUNT(record_id) FROM data_migration_failed_ids WHERE data_migration_id = $1;",
162 [data_migration_id()]
168 def feature_state, do: Config.get(feature_config_path())
170 def force_continue do
171 send(whereis(), :perform)
180 update_status(:complete)
182 on_complete(data_migration())
185 defp update_status(status, message \\ nil) do
186 put_stat(:state, status)
187 put_stat(:message, message)
191 with failures_count when is_integer(failures_count) <- failures_count(),
192 true <- failures_count > 0 do
193 failures_count / Enum.max([get_stat(:affected_count, 0), 1])
199 defp records_per_second do
200 get_stat(:iteration_processed_count, 0) / Enum.max([running_time(), 1])
205 NaiveDateTime.utc_now(),
206 get_stat(:started_at, NaiveDateTime.utc_now())