From 76e0e9e62a74d82813e041d4b1c69e4edafb26fa Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Sat, 9 Dec 2017 19:03:43 +0200 Subject: [PATCH] resolves #22: allow keeping N rundirs This replaces LAMINAR_KEEP_RUNDIR (bool) with LAMINAR_KEEP_RUNDIRS (int) --- README.md | 2 +- laminar.conf | 11 +++++++---- src/laminar.cpp | 32 ++++++++++++++++++++++++-------- src/laminar.h | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f892aa8..faf583f 100644 --- a/README.md +++ b/README.md @@ -490,7 +490,7 @@ make - `LAMINAR_BIND_HTTP`: The interface/port or unix socket on which `laminard` should listen for incoming connections to the web frontend. Default `*:8080` - `LAMINAR_BIND_RPC`: The interface/port or unix socket on which `laminard` should listen for incoming commands such as build triggers. Default `unix-abstract:laminar` - `LAMINAR_TITLE`: The page title to show in the web frontend. -- `LAMINAR_KEEP_RUNDIR`: If set, do not delete run directories after completion +- `LAMINAR_KEEP_RUNDIRS`: Set to an integer defining how many rundirs to keep per job. The lowest-numbered ones will be deleted. The default is 0, meaning all run dirs will be immediately deleted. - `LAMINAR_ARCHIVE_URL`: If set, the web frontend served by `laminard` will use this URL to form links to artefacts archived jobs. Must be synchronized with web server configuration. ### Script execution order diff --git a/laminar.conf b/laminar.conf index 1e53a5d..2b479fe 100644 --- a/laminar.conf +++ b/laminar.conf @@ -33,12 +33,15 @@ #LAMINAR_TITLE= ### -### LAMINAR_KEEP_RUNDIR +### LAMINAR_KEEP_RUNDIRS ### -### If set (to anything), the job rundir $LAMINAR_HOME/run/$JOB/$RUN -### will not be deleted after the run has completed +### Setting this prevents the immediate deletion of job rundirs +### $LAMINAR_HOME/run/$JOB/$RUN. Value should be an integer represeting +### the number of rundirs to keep. ### -#LAMINAR_KEEP_RUNDIR=1 +### Default: 0 +### +#LAMINAR_KEEP_RUNDIRS=0 ### ### LAMINAR_ARCHIVE_URL diff --git a/src/laminar.cpp b/src/laminar.cpp index 2ecdc91..cb8f9d9 100644 --- a/src/laminar.cpp +++ b/src/laminar.cpp @@ -73,7 +73,7 @@ Laminar::Laminar() { archiveUrl = ARCHIVE_URL_DEFAULT; if(char* envArchive = getenv("LAMINAR_ARCHIVE_URL")) archiveUrl = envArchive; - eraseRunDir = true; + numKeepRunDirs = 0; homeDir = getenv("LAMINAR_HOME") ?: "/var/lib/laminar"; db = new Database((fs::path(homeDir)/"laminar.sqlite").string().c_str()); @@ -393,8 +393,8 @@ void Laminar::stop() { } bool Laminar::loadConfiguration() { - if(getenv("LAMINAR_KEEP_RUNDIR")) - eraseRunDir = false; + if(const char* ndirs = getenv("LAMINAR_KEEP_RUNDIRS")) + numKeepRunDirs = atoi(ndirs); NodeMap nm; @@ -757,12 +757,28 @@ void Laminar::runFinished(Run * r) { w->complete(r); } - // remove the rundir - if(eraseRunDir) - fs::remove_all(r->runDir); - - // will delete the job + // erase reference to run from activeJobs activeJobs.get<2>().erase(r); + + // remove old run directories + // We cannot count back the number of directories to keep from the currently + // finishing job because there may well be older, still-running instances of + // this job and we don't want to delete their rundirs. So instead, check + // whether there are any more active runs of this job, and if so, count back + // from the oldest among them. If there are none, count back from the latest + // known build number of this job, which may not be that of the run that + // finished here. + auto it = activeJobs.get<4>().equal_range(r->name); + uint oldestActive = (it.first == it.second)? buildNums[r->name] : (*it.first)->build - 1; + for(int i = oldestActive - numKeepRunDirs; i > 0; i--) { + fs::path d = fs::path(homeDir)/"run"/r->name/std::to_string(i); + // Once the directory does not exist, it's probably not worth checking + // any further. 99% of the time this loop should only ever have 1 iteration + // anyway so hence this (admittedly debatable) optimization. + if(!fs::exists(d)) + break; + fs::remove_all(d); + } } bool Laminar::getArtefact(std::string path, std::string& result) { diff --git a/src/laminar.h b/src/laminar.h index 4c4de52..28113ec 100644 --- a/src/laminar.h +++ b/src/laminar.h @@ -85,7 +85,7 @@ private: std::string homeDir; std::set clients; std::set waiters; - bool eraseRunDir; + uint numKeepRunDirs; std::string archiveUrl; };