Skip to content

Commit 1f36d66

Browse files
authored
separate update intervals (#187)
1 parent a9e5893 commit 1f36d66

File tree

5 files changed

+81
-58
lines changed

5 files changed

+81
-58
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ To install or update LODA, please follow the [installation instructions](https:/
22

33
## [Unreleased]
44

5+
### Enhancements
6+
7+
* Separate update intervals for OEIS files (7 days) and programs repository (1 day)
8+
59
# v22.10.13
610

711
### Features

src/include/oeis_manager.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class OeisManager {
7676
Evaluator evaluator;
7777
Finder finder;
7878
bool finder_initialized;
79-
bool update_needed;
79+
bool update_oeis;
80+
bool update_programs;
8081

8182
Optimizer optimizer;
8283
Minimizer minimizer;

src/include/setup.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class Setup {
5353

5454
static int64_t getMaxMemory();
5555

56-
static int64_t getUpdateIntervalInDays();
56+
static int64_t getGitHubUpdateInterval();
57+
58+
static int64_t getOeisUpdateInterval();
5759

5860
static int64_t getMaxLocalProgramAgeInDays();
5961

@@ -72,9 +74,10 @@ class Setup {
7274
static void runWizard();
7375

7476
private:
75-
static constexpr int64_t UNDEFINED_INT = -2; // cannot use -1
76-
static constexpr int64_t DEFAULT_UPDATE_INTERVAL = 1; // 1 day default
77-
static constexpr int64_t DEFAULT_MAX_PROGRAM_AGE = 14; // 2 weeks default
77+
static constexpr int64_t UNDEFINED_INT = -2; // cannot use -1
78+
static constexpr int64_t DEFAULT_GITHUB_UPDATE_INTERVAL = 1; // 1 day default
79+
static constexpr int64_t DEFAULT_OEIS_UPDATE_INTERVAL = 7; // 1 week default
80+
static constexpr int64_t DEFAULT_MAX_PROGRAM_AGE = 14; // 2 weeks default
7881
static constexpr int64_t DEFAULT_MAX_PHYSICAL_MEMORY = 1024; // 1 GB
7982

8083
static std::string LODA_HOME;
@@ -86,7 +89,8 @@ class Setup {
8689
static bool PRINTED_MEMORY_WARNING;
8790
static int64_t MINING_MODE;
8891
static int64_t MAX_MEMORY;
89-
static int64_t UPDATE_INTERVAL;
92+
static int64_t GITHUB_UPDATE_INTERVAL;
93+
static int64_t OEIS_UPDATE_INTERVAL;
9094
static int64_t MAX_PROGRAM_AGE;
9195
static int64_t MAX_INSTANCES;
9296

@@ -104,7 +108,6 @@ class Setup {
104108
static bool checkSubmittedBy();
105109
static bool checkUsageStats();
106110
static bool checkMaxMemory();
107-
static bool checkUpdateInterval();
108111
static bool checkMaxLocalProgramAge();
109112
static bool checkMaxInstances();
110113

src/oeis_manager.cpp

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ OeisManager::OeisManager(const Settings &settings,
3636
evaluator(settings),
3737
finder(settings, evaluator),
3838
finder_initialized(false),
39-
update_needed(false),
39+
update_oeis(false),
40+
update_programs(false),
4041
optimizer(settings),
4142
minimizer(settings),
4243
loaded_count(0),
@@ -305,48 +306,78 @@ bool OeisManager::shouldMatch(const OeisSequence &seq) const {
305306
void OeisManager::update() {
306307
std::vector<std::string> files = {"stripped", "names"};
307308

308-
// check which files need to be updated
309+
// check whether oeis files need to be updated
309310
auto it = files.begin();
310-
int64_t age_in_days = -1;
311+
int64_t oeis_age_in_days = -1;
311312
while (it != files.end()) {
312313
auto path = Setup::getOeisHome() + *it;
313-
age_in_days = getFileAgeInDays(path);
314-
if (age_in_days < 0 || age_in_days >= Setup::getUpdateIntervalInDays()) {
315-
update_needed = true;
314+
oeis_age_in_days = getFileAgeInDays(path);
315+
if (oeis_age_in_days < 0 ||
316+
oeis_age_in_days >= Setup::getOeisUpdateInterval()) {
317+
update_oeis = true;
316318
break;
317319
}
318320
it++;
319321
}
320-
if (update_needed) {
322+
323+
// check whether programs need to be updated
324+
const std::string progs_dir = Setup::getProgramsHome();
325+
const std::string local_dir = progs_dir + "local";
326+
const std::string update_progs_file = local_dir + FILE_SEP + ".update";
327+
int64_t programs_age_in_days = getFileAgeInDays(update_progs_file);
328+
if (programs_age_in_days < 0) {
329+
update_programs = update_oeis; // fall-back if file is not present
330+
} else if (programs_age_in_days >= Setup::getGitHubUpdateInterval()) {
331+
update_programs = true;
332+
}
333+
334+
// perform oeis update
335+
if (update_oeis) {
321336
Setup::checkLatestedVersion();
322-
if (age_in_days == -1) {
337+
if (oeis_age_in_days == -1) {
323338
Log::get().info("Creating OEIS index at \"" + Setup::getOeisHome() +
324339
"\"");
325340
ensureDir(Setup::getOeisHome());
326341
} else {
327342
Log::get().info("Updating OEIS index (last update " +
328-
std::to_string(age_in_days) + " days ago)");
343+
std::to_string(oeis_age_in_days) + " days ago)");
329344
}
330345
std::string cmd, path;
331346
for (auto &file : files) {
332347
path = Setup::getOeisHome() + file;
333348
ApiClient::getDefaultInstance().getOeisFile(file, path);
334349
}
335-
// update programs repository using git pull
350+
}
351+
352+
// perform programs update
353+
if (update_programs) {
336354
auto mode = Setup::getMiningMode();
337-
auto progs_dir = Setup::getProgramsHome();
338355
if (mode != MINING_MODE_SERVER && isDir(progs_dir + ".git")) {
339-
Log::get().info("Updating programs repository");
356+
std::string msg("Updating programs repository");
357+
if (programs_age_in_days >= 0) {
358+
msg += " (last update " + std::to_string(programs_age_in_days) +
359+
" days ago)";
360+
}
361+
Log::get().info(msg);
362+
// update programs repository using git pull
340363
git(progs_dir, "pull origin main -q --ff-only");
364+
// touch marker file to track the age
365+
ensureDir(update_progs_file);
366+
std::ofstream marker(update_progs_file);
367+
if (marker) {
368+
marker << "1" << std::endl;
369+
} else {
370+
Log::get().warn("Cannot write update marker: " + update_progs_file);
371+
}
341372
}
373+
342374
// clean up local programs folder
343375
const int64_t max_age = Setup::getMaxLocalProgramAgeInDays();
344-
const auto local = Setup::getProgramsHome() + "local";
345-
if (max_age >= 0 && isDir(local) &&
376+
if (max_age >= 0 && isDir(local_dir) &&
346377
Setup::getMiningMode() == MiningMode::MINING_MODE_CLIENT) {
347378
Log::get().info("Cleaning up local programs directory");
348379
int64_t num_removed = 0;
349-
for (const auto &it : std::filesystem::directory_iterator(local)) {
380+
for (const auto &it : std::filesystem::directory_iterator(local_dir)) {
350381
const auto stem = it.path().filename().stem().string();
351382
const auto ext = it.path().filename().extension().string();
352383
bool is_program;
@@ -531,9 +562,11 @@ const Stats &OeisManager::getStats() {
531562
stats.reset(new Stats());
532563

533564
// check age of stats
565+
auto update_interval = std::min<int64_t>(Setup::getOeisUpdateInterval(),
566+
Setup::getGitHubUpdateInterval());
534567
auto age_in_days = getFileAgeInDays(stats->getMainStatsFile(stats_home));
535-
if (update_needed || age_in_days < 0 ||
536-
age_in_days >= Setup::getUpdateIntervalInDays()) {
568+
if (update_oeis || update_programs || age_in_days < 0 ||
569+
age_in_days >= update_interval) {
537570
generateStats(age_in_days);
538571

539572
// generate lists in server mode

src/setup.cpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ bool Setup::LOADED_SETUP = false;
2727
bool Setup::PRINTED_MEMORY_WARNING = false;
2828
int64_t Setup::MINING_MODE = UNDEFINED_INT;
2929
int64_t Setup::MAX_MEMORY = UNDEFINED_INT;
30-
int64_t Setup::UPDATE_INTERVAL = UNDEFINED_INT;
30+
int64_t Setup::GITHUB_UPDATE_INTERVAL = UNDEFINED_INT;
31+
int64_t Setup::OEIS_UPDATE_INTERVAL = UNDEFINED_INT;
3132
int64_t Setup::MAX_PROGRAM_AGE = UNDEFINED_INT;
3233
int64_t Setup::MAX_INSTANCES = UNDEFINED_INT;
3334

@@ -98,7 +99,7 @@ std::string Setup::getMinersConfig() {
9899
{
99100
FolderLock lock(getLodaHome());
100101
const auto age_in_days = getFileAgeInDays(default_config);
101-
if (age_in_days < 0 || age_in_days >= getUpdateIntervalInDays()) {
102+
if (age_in_days < 0 || age_in_days >= getGitHubUpdateInterval()) {
102103
const std::string url =
103104
"https://raw.githubusercontent.com/loda-lang/loda-cpp/main/"
104105
"miners.default.json";
@@ -204,12 +205,20 @@ int64_t Setup::getMaxMemory() {
204205
return MAX_MEMORY;
205206
}
206207

207-
int64_t Setup::getUpdateIntervalInDays() {
208-
if (UPDATE_INTERVAL == UNDEFINED_INT) {
209-
UPDATE_INTERVAL =
210-
getSetupInt("LODA_UPDATE_INTERVAL", DEFAULT_UPDATE_INTERVAL);
208+
int64_t Setup::getGitHubUpdateInterval() {
209+
if (GITHUB_UPDATE_INTERVAL == UNDEFINED_INT) {
210+
GITHUB_UPDATE_INTERVAL = getSetupInt("LODA_GITHUB_UPDATE_INTERVAL",
211+
DEFAULT_GITHUB_UPDATE_INTERVAL);
211212
}
212-
return UPDATE_INTERVAL;
213+
return GITHUB_UPDATE_INTERVAL;
214+
}
215+
216+
int64_t Setup::getOeisUpdateInterval() {
217+
if (OEIS_UPDATE_INTERVAL == UNDEFINED_INT) {
218+
OEIS_UPDATE_INTERVAL =
219+
getSetupInt("LODA_OEIS_UPDATE_INTERVAL", DEFAULT_OEIS_UPDATE_INTERVAL);
220+
}
221+
return OEIS_UPDATE_INTERVAL;
213222
}
214223

215224
int64_t Setup::getMaxLocalProgramAgeInDays() {
@@ -337,11 +346,6 @@ void Setup::runWizard() {
337346
if (!checkMaxMemory()) {
338347
return;
339348
}
340-
// since we get the oeis data from the API server, we don't need to
341-
// configure this anymore
342-
// if (!checkUpdateInterval()) {
343-
// return;
344-
// }
345349
#ifdef STD_FILESYSTEM
346350
if (getMiningMode() == MINING_MODE_CLIENT && !checkMaxLocalProgramAge()) {
347351
return;
@@ -699,28 +703,6 @@ bool Setup::checkMaxMemory() {
699703
return true;
700704
}
701705

702-
bool Setup::checkUpdateInterval() {
703-
std::string line;
704-
std::cout << "Enter the update interval for the main OEIS files and the"
705-
<< std::endl
706-
<< "programs repository in days (default " +
707-
std::to_string(DEFAULT_UPDATE_INTERVAL)
708-
<< "):" << std::endl;
709-
int64_t update_interval = getUpdateIntervalInDays();
710-
std::cout << "[" << update_interval << "] ";
711-
std::getline(std::cin, line);
712-
if (!line.empty()) {
713-
update_interval = std::stoll(line);
714-
}
715-
if (update_interval <= 0) {
716-
std::cout << "Invalid value. Please restart the setup." << std::endl;
717-
return false;
718-
}
719-
SETUP["LODA_UPDATE_INTERVAL"] = std::to_string(update_interval);
720-
std::cout << std::endl;
721-
return true;
722-
}
723-
724706
bool Setup::checkMaxLocalProgramAge() {
725707
std::string line;
726708
std::cout << "Enter the maximum age of local programs in days (default: " +

0 commit comments

Comments
 (0)