Add BackupWorker

This commit is contained in:
Egor Kislitsyn 2020-09-02 21:45:22 +04:00
parent 4f3a633745
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

@ -551,6 +551,7 @@ config :pleroma, Oban,
queues: [ queues: [
activity_expiration: 10, activity_expiration: 10,
token_expiration: 5, token_expiration: 5,
backup: 1,
federator_incoming: 50, federator_incoming: 50,
federator_outgoing: 50, federator_outgoing: 50,
ingestion_queue: 50, ingestion_queue: 50,

View File

@ -2288,6 +2288,12 @@ config :pleroma, :config_description, [
description: "Activity expiration queue", description: "Activity expiration queue",
suggestions: [10] suggestions: [10]
}, },
%{
key: :backup,
type: :integer,
description: "Backup queue",
suggestions: [1]
},
%{ %{
key: :attachments_cleanup, key: :attachments_cleanup,
type: :integer, type: :integer,

View File

@ -30,7 +30,7 @@ defmodule Pleroma.Backup do
def create(user) do def create(user) do
with :ok <- validate_limit(user), with :ok <- validate_limit(user),
{:ok, backup} <- user |> new() |> Repo.insert() do {:ok, backup} <- user |> new() |> Repo.insert() do
{:ok, backup} Pleroma.Workers.BackupWorker.enqueue("process", %{"backup_id" => backup.id})
end end
end end
@ -71,6 +71,15 @@ defmodule Pleroma.Backup do
|> Repo.one() |> Repo.one()
end 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 def process(%__MODULE__{} = backup) do
with {:ok, zip_file} <- zip(backup), with {:ok, zip_file} <- zip(backup),
{:ok, %{size: size}} <- File.stat(zip_file), {: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

View File

@ -3,35 +3,43 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.BackupTest do defmodule Pleroma.BackupTest do
use Oban.Testing, repo: Pleroma.Repo
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
import Mock import Mock
alias Pleroma.Backup alias Pleroma.Backup
alias Pleroma.Bookmark alias Pleroma.Bookmark
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Workers.BackupWorker
test "it creates a backup record" do setup do: clear_config([Pleroma.Upload, :uploader])
test "it creates a backup record and an Oban job" do
%{id: user_id} = user = insert(:user) %{id: user_id} = user = insert(:user)
assert {:ok, backup} = Backup.create(user) assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
assert_enqueued(worker: BackupWorker, args: args)
backup = Backup.get(args["backup_id"])
assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
end end
test "it return an error if the export limit is over" do test "it return an error if the export limit is over" do
%{id: user_id} = user = insert(:user) %{id: user_id} = user = insert(:user)
limit_days = 7 limit_days = 7
assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
assert {:ok, backup} = Backup.create(user) backup = Backup.get(args["backup_id"])
assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"} assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"}
end end
test "it process a backup record" do test "it process a backup record" do
Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
%{id: user_id} = user = insert(:user) %{id: user_id} = user = insert(:user)
assert {:ok, %{id: backup_id} = backup} = Backup.create(user) assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id}} = job} = Backup.create(user)
assert {:ok, %Backup{} = backup} = Backup.process(backup) assert {:ok, backup} = BackupWorker.perform(job)
assert backup.file_size > 0 assert backup.file_size > 0
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
end end