1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
CloudSyncHousekeeper.cpp
Dmitry Vedenko
**********************************************************************/
#include <algorithm>
#include <atomic>
#include <chrono>
#include <future>
#include <wx/file.h>
#include "AppEvents.h"
#include "CodeConversions.h"
#include "CloudLibrarySettings.h"
#include "CloudProjectsDatabase.h"
namespace audacity::cloud::audiocom::sync
{
namespace
{
class Housekeeper final
{
public:
Housekeeper()
{
AppEvents::OnAppInitialized([this] { OnAppInitialized(); });
AppEvents::OnAppClosing([this] { OnAppClosing(); });
}
private:
void OnAppInitialized()
{
mHousekeepingOperation = std::async([this] { PerformHousekeeping(); });
}
void PerformHousekeeping()
{
const auto timeToKeep = std::chrono::hours(24 * DaysToKeepFiles.Read());
const auto now = std::chrono::system_clock::now();
auto& cloudProjectsDatabase = CloudProjectsDatabase::Get();
auto projects = cloudProjectsDatabase.GetCloudProjects();
for (const auto& project : projects)
{
if (mHousekeepingCancelled.load())
return;
const auto path = ToWXString(project.LocalPath);
if (!wxFileExists(path))
{
cloudProjectsDatabase.DeleteProject(project.ProjectId);
continue;
}
const auto lastAccess =
std::max(project.LastModified, project.LastRead);
const auto discardTreshold =
std::chrono::system_clock::from_time_t(lastAccess) + timeToKeep;
if (discardTreshold > now)
continue;
if (wxRemoveFile(path))
cloudProjectsDatabase.DeleteProject(project.ProjectId);
}
// Do we need to remove the files that are not in the database?
}
void OnAppClosing()
{
mHousekeepingCancelled.store(true);
if (mHousekeepingOperation.valid())
mHousekeepingOperation.wait();
auto& cloudProjectsDatabase = CloudProjectsDatabase::Get();
auto connectionLock = cloudProjectsDatabase.GetConnection();
if (!connectionLock)
return;
auto vacuumStatement = connectionLock->CreateStatement("VACUUM");
if (!vacuumStatement)
return;
vacuumStatement->Prepare().Run();
}
std::future<void> mHousekeepingOperation;
std::atomic<bool> mHousekeepingCancelled { false };
}; // class Housekeeper
static Housekeeper housekeeper;
} // namespace
} // namespace audacity::cloud::audiocom::sync
|