Allow configuring retention for build actions

This commit is contained in:
Martchus 2023-11-23 18:49:43 +01:00
parent 24e2ec0136
commit edd78667c5
5 changed files with 28 additions and 11 deletions

View File

@ -478,6 +478,7 @@ void BuildServiceCleanup::run()
// get variables from setup
auto setupLock = m_setup.lockToRead();
m_buildActionRetention = m_setup.building.buildActionRetention;
m_paccachePath = findExecutable(m_setup.building.paccachePath);
const auto packageCachePath = m_setup.building.packageCacheDir;
setupLock.unlock();
@ -506,7 +507,7 @@ void BuildServiceCleanup::run()
auto deleted = std::size_t();
constexpr auto stopAt = 150;
m_setup.building.forEachBuildAction(
[this, &count, &deleted, twoWeeksAgo = DateTime::gmtNow() - TimeSpan::fromDays(14)](
[this, &count, &deleted, expirationDate = DateTime::gmtNow() - m_buildActionRetention](
LibPkg::StorageID id, BuildAction &action, ServiceSetup::BuildSetup::VisitorBehavior &visitorBehavior) {
if (count <= stopAt) {
return true; // abort deletion if under 150 build actions anyways
@ -514,8 +515,9 @@ void BuildServiceCleanup::run()
if (m_buildAction->id == id || action.finished.isNull()) {
return false; // avoid deleting cleanup action itself as well as any unfinished actions
}
if (action.result != BuildActionResult::Success || action.finished > twoWeeksAgo) {
return false; // delete only successful actions that are at least two weeks old
if (action.result != BuildActionResult::Success
|| (action.finished > expirationDate && action.type != BuildActionType::BuildServiceCleanup)) {
return false; // delete only successful actions that have expired (unless it is just a cleanup action itself)
}
visitorBehavior = ServiceSetup::BuildSetup::VisitorBehavior::Delete;
++deleted;

View File

@ -695,6 +695,7 @@ private:
std::vector<std::pair<std::string, boost::filesystem::path>> m_concreteCacheDirs;
std::vector<std::pair<std::string, boost::filesystem::path>>::iterator m_concreteCacheDirsIterator;
BuildActionMessages m_messages;
CppUtilities::TimeSpan m_buildActionRetention;
bool m_dryCacheCleanup;
bool m_dbCleanupConcluded, m_cacheCleanupConcluded;
};

View File

@ -1,6 +1,8 @@
#ifndef LIBREPOMGR_HELPER_H
#define LIBREPOMGR_HELPER_H
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/conversion/conversionexception.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/ansiescapecodes.h>
#include <c++utilities/misc/traits.h>
@ -63,7 +65,6 @@ inline void convertValue(const std::multimap<std::string, std::string> &multimap
template <typename TargetType, Traits::EnableIfAny<std::is_integral<TargetType>, Traits::IsSpecializationOf<TargetType, std::atomic>> * = nullptr>
inline void convertValue(const std::multimap<std::string, std::string> &multimap, const std::string &key, TargetType &result)
{
using namespace std;
using namespace CppUtilities;
using namespace CppUtilities::EscapeCodes;
@ -75,8 +76,7 @@ inline void convertValue(const std::multimap<std::string, std::string> &multimap
result = stringToNumber<TargetType>(value);
}
} catch (const ConversionException &) {
cerr << Phrases::ErrorMessage << "Specified number \"" << value << "\" for key \"" << key << "\" is invalid." << Phrases::End;
return;
std::cerr << Phrases::ErrorMessage << "Specified number \"" << value << "\" for key \"" << key << "\" is invalid." << Phrases::End;
}
}
}
@ -90,16 +90,14 @@ template <> inline void convertValue(const std::multimap<std::string, std::strin
template <> inline void convertValue(const std::multimap<std::string, std::string> &multimap, const std::string &key, std::regex &result)
{
using namespace std;
using namespace CppUtilities::EscapeCodes;
if (const char *const value = getLastValue(multimap, key)) {
try {
result = value;
} catch (const regex_error &e) {
cerr << Phrases::ErrorMessage << "Specified regex \"" << value << "\" for key \"" << key << "\" is invalid: " << Phrases::End;
cerr << e.what() << '\n';
return;
} catch (const std::regex_error &e) {
std::cerr << Phrases::ErrorMessage << "Specified regex \"" << value << "\" for key \"" << key << "\" is invalid: " << Phrases::End;
std::cerr << e.what() << '\n';
}
}
}
@ -119,6 +117,20 @@ template <> inline void convertValue(const std::multimap<std::string, std::strin
}
}
template <> inline void convertValue(const std::multimap<std::string, std::string> &multimap, const std::string &key, CppUtilities::TimeSpan &result)
{
using namespace CppUtilities::EscapeCodes;
if (const char *const value = getLastValue(multimap, key)) {
try {
result = CppUtilities::TimeSpan::fromString(value);
} catch (const CppUtilities::ConversionException &e) {
std::cerr << Phrases::ErrorMessage << "Specified duration \"" << value << "\" for key \"" << key << "\" is invalid: " << Phrases::End;
std::cerr << e.what() << '\n';
}
}
}
template <typename VectorType> void mergeSecondVectorIntoFirstVector(VectorType &firstVector, VectorType &secondVector)
{
const auto requiredSize = firstVector.size() + secondVector.size();

View File

@ -181,6 +181,7 @@ void ServiceSetup::BuildSetup::applyConfig(const std::multimap<std::string, std:
convertValue(multimap, "package_cache_dir", packageCacheDir);
convertValue(multimap, "package_download_size_limit", packageDownloadSizeLimit);
convertValue(multimap, "test_files_dir", testFilesDir);
convertValue(multimap, "build_action_retention", buildActionRetention);
convertValue(multimap, "load_files_dbs", loadFilesDbs);
convertValue(multimap, "db_path", dbPath);
}

View File

@ -136,6 +136,7 @@ struct LIBREPOMGR_EXPORT ServiceSetup : public LibPkg::Lockable {
std::uint64_t packageDownloadSizeLimit = 500 * 1024 * 1024;
std::string testFilesDir;
BuildPresets presets;
CppUtilities::TimeSpan buildActionRetention = CppUtilities::TimeSpan::fromDays(14);
bool loadFilesDbs = false;
bool forceLoadingDbs = false;