From d1ab9814a35c75b91c93f619c6801e36c8a15b3e Mon Sep 17 00:00:00 2001 From: hahn <axel.hahn@iml.unibe.ch> Date: Fri, 22 Dec 2017 15:56:21 +0100 Subject: [PATCH] add locking for cronjob that fetches git revisions --- shellscripts/cron_cache_gitbranches.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/shellscripts/cron_cache_gitbranches.php b/shellscripts/cron_cache_gitbranches.php index 9e2c37e8..2b1a0257 100644 --- a/shellscripts/cron_cache_gitbranches.php +++ b/shellscripts/cron_cache_gitbranches.php @@ -8,6 +8,7 @@ // http://iml:deployment@ci.iml.unibe.ch/deployment/?prj=ci&action=deploy&par3=preview&confirm=1 +$iStart=date('U'); $sDocroot = (dirname(__dir__)) . "/public_html"; $processUser = posix_getpwuid(posix_geteuid()); @@ -15,6 +16,17 @@ if ($processUser['name']=="root"){ die("Do not start the script as user root\n"); }; +echo "\n<h1>===== IMLDEPLOYMENT - cache git branches :: ".(date("Y-m-d H:i:s"))." =====</h1>\n\n"; +$sLockfile="/tmp/cache-gitbranches.lock"; +if (file_exists($sLockfile)){ + $iAge=date('U')-filemtime($sLockfile); + $iMagAge=60*60 * 3; + if ($iAge < 60*60 * 3){ + die("ABORT: lock file exists: $sLockfile (age: $iAge sec ... needs to wait up to $iMagAge sec or the running script is finished)\n"); + } + echo "INFO: ignoring existing lock file $sLockfile (age: $iAge sec is older $iMagAge sec)\n"; +} + require_once("$sDocroot/deployment/classes/logger.class.php"); global $oCLog; $oCLog = new logger(); @@ -25,7 +37,9 @@ require_once("$sDocroot/deployment/classes/project.class.php"); $oPrj1 = new project(); -echo "<h1>===== IMLDEPLOYMENT - cache git branches =====</h1>\n"; +echo "creating lock file $sLockfile\n"; +file_put_contents($sLockfile, "started ".date("Y-m-d H:i:s")); + foreach ($oPrj1->getProjects() as $sPrj) { $oPrj = new project($sPrj); @@ -35,4 +49,9 @@ foreach ($oPrj1->getProjects() as $sPrj) { } echo "<br>\n"; } -echo "done\n"; +echo "\n\n---------- done\n"; +echo "removing lock file $sLockfile\n"; +unlink($sLockfile); +echo "Script needed ".(date('U')-$iStart) . " s\n\n"; + +// echo $oCLog->renderCli(); \ No newline at end of file -- GitLab