Add BackupWorker

This commit is contained in:
Egor Kislitsyn 2020-09-02 21:45:22 +04:00
commit a0ad9bd734
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
5 changed files with 48 additions and 7 deletions

View file

@ -30,7 +30,7 @@ defmodule Pleroma.Backup do
def create(user) do
with :ok <- validate_limit(user),
{:ok, backup} <- user |> new() |> Repo.insert() do
{:ok, backup}
Pleroma.Workers.BackupWorker.enqueue("process", %{"backup_id" => backup.id})
end
end
@ -71,6 +71,15 @@ defmodule Pleroma.Backup do
|> Repo.one()
end
def remove_outdated(%__MODULE__{id: latest_id, user_id: user_id}) do
__MODULE__
|> where(user_id: ^user_id)
|> where([b], b.id != ^latest_id)
|> Repo.delete_all()
end
def get(id), do: Repo.get(__MODULE__, id)
def process(%__MODULE__{} = backup) do
with {:ok, zip_file} <- zip(backup),
{:ok, %{size: size}} <- File.stat(zip_file),

View file

@ -0,0 +1,17 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.BackupWorker do
alias Pleroma.Backup
use Pleroma.Workers.WorkerHelper, queue: "backup"
@impl Oban.Worker
def perform(%Job{args: %{"op" => "process", "backup_id" => backup_id}}) do
with {:ok, %Backup{} = backup} <-
backup_id |> Backup.get() |> Backup.process() do
{:ok, backup}
end
end
end