1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Workers.BackupWorker do
6 use Oban.Worker, queue: :backup, max_attempts: 1
9 alias Pleroma.User.Backup
11 def process(backup, admin_user_id \\ nil) do
12 %{"op" => "process", "backup_id" => backup.id, "admin_user_id" => admin_user_id}
17 def schedule_deletion(backup) do
18 days = Pleroma.Config.get([Backup, :purge_after_days])
19 time = 60 * 60 * 24 * days
20 scheduled_at = Calendar.NaiveDateTime.add!(backup.inserted_at, time)
22 %{"op" => "delete", "backup_id" => backup.id}
23 |> new(scheduled_at: scheduled_at)
28 %{"op" => "delete", "backup_id" => backup.id}
34 args: %{"op" => "process", "backup_id" => backup_id, "admin_user_id" => admin_user_id}
36 with {:ok, %Backup{} = backup} <-
37 backup_id |> Backup.get() |> Backup.process(),
38 {:ok, _job} <- schedule_deletion(backup),
39 :ok <- Backup.remove_outdated(backup),
42 |> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
43 |> Pleroma.Emails.Mailer.deliver() do
48 def perform(%Job{args: %{"op" => "delete", "backup_id" => backup_id}}) do
49 case Backup.get(backup_id) do
50 %Backup{} = backup -> Backup.delete(backup)