analytics/lib/workers/local_import_analytics_cleaner.ex
Adrian Gruntkowski c1c03b729c
Reapply "Local CSV exports/imports and S3/UI updates (#3989)" (#3995) (#3996)
* Reapply "Local CSV exports/imports and S3/UI updates (#3989)" (#3995)

This reverts commit aee69e44c8.

* remove unused functions

* eh, that one was actually used

* ugh, they were both used

---------

Co-authored-by: ruslandoga <67764432+ruslandoga@users.noreply.github.com>
2024-04-11 09:15:01 +02:00

33 lines
911 B
Elixir

defmodule Plausible.Workers.LocalImportAnalyticsCleaner do
@moduledoc """
Worker for cleaning local files left after analytics import jobs.
"""
use Oban.Worker, queue: :analytics_imports, unique: [period: 3600]
@impl Oban.Worker
def perform(%Oban.Job{args: args}) do
%{"import_id" => import_id, "paths" => paths} = args
if import_in_progress?(import_id) do
{:snooze, _one_hour = 3600}
else
Enum.each(paths, fn path ->
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
if File.exists?(path), do: File.rm!(path)
end)
end
end
defp import_in_progress?(import_id) do
import Ecto.Query
require Plausible.Imported.SiteImport
alias Plausible.Imported.SiteImport
SiteImport
|> where(id: ^import_id)
|> where([i], i.status in ^[SiteImport.pending(), SiteImport.importing()])
|> Plausible.Repo.exists?()
end
end