ported fill_search_index to lib2; refactored cronjob process synchronization

This commit is contained in:
following
2013-07-12 15:59:00 +02:00
parent 63409347bc
commit 3a5bdab4ec
6 changed files with 154 additions and 234 deletions

View File

@ -407,7 +407,6 @@
/* cronjob
*/
$opt['cron']['pidfile'] = $opt['rootpath'] . 'cache2/runcron.pid';
$opt['cron']['username'] = 'apache'; // system username for cronjobs
/* phpbb news integration (index.php)

View File

@ -0,0 +1,109 @@
<?php
/****************************************************************************
For license information see doc/license.txt
Unicode Reminder メモ
synchronization of processes which must not run concurrently
****************************************************************************/
class ProcessSync
{
var $pidfile_path;
function __construct($name)
{
global $opt;
$this->pidfile_path = $opt['rootpath'] . "cache2/$name.pid";
}
// Enter code section which must not run concurrently
function Enter()
{
if (!$this->CheckDaemon())
return false;
if (file_exists($this->pidfile_path))
{
echo "Error: Pidfile (".$this->pidfile_path.") already present\n";
return false;
}
else
{
if ($pidfile = @fopen($this->pidfile_path, "w"))
{
fputs($pidfile, posix_getpid());
fclose($pidfile);
return true;
}
else
{
echo "can't create Pidfile ".$this->pidfile_path."\n";
return false;
}
}
}
// checks if other instance of process is running
private function CheckDaemon()
{
if ($pidfile = @fopen($this->pidfile_path, "r"))
{
$pid_daemon = fgets($pidfile, 20);
fclose($pidfile);
$pid_daemon = (int)$pid_daemon;
// process running?
if (posix_kill($pid_daemon, 0))
{
// yes, good bye
echo "Error: process for ".$this->pidfile_path. " is already running with pid=$pid_daemon\n";
false;
}
else
{
// no, remove pid_file
echo "process not running, removing old pid_file (".$this->pidfile_path.")\n";
unlink($this->pidfile_path);
return true;
}
}
else
{
return true;
}
}
// Leave code section which must not run concurrently
function Leave($message = false)
{
if ($pidfile = @fopen($this->pidfile_path, "r"))
{
$pid = fgets($pidfile, 20);
fclose($pidfile);
if ($pid == posix_getpid())
unlink($this->pidfile_path);
}
else
{
echo "Error: can't delete own pidfile (".$this->pidfile_path.")\n";
}
if ($message)
{
echo $message . "\n";
}
}
}
?>

View File

@ -1,4 +0,0 @@
<FilesMatch "*">
Order Deny,Allow
Deny from All
</FilesMatch>

View File

@ -1,125 +0,0 @@
#!/usr/local/bin/php -q
<?php
/****************************************************************************
For license information see doc/license.txt
Unicode Reminder メモ
refresh the search-index of all modified descriptions
****************************************************************************/
//prepare the templates and include all neccessary
// needs absolute rootpath because called as cronjob
$rootpath = dirname(__FILE__) . '/../../';
$pidfile = $rootpath . 'cache/search.pid';
// chdir to proper directory (needed for cronjobs)
chdir(substr(realpath($_SERVER['PHP_SELF']), 0, strrpos(realpath($_SERVER['PHP_SELF']), '/')));
require_once($rootpath . 'lib/clicompatbase.inc.php');
require_once($rootpath . 'lib/ftsearch.inc.php');
// use posix pid-files to lock process
if (!CreatePidFile($pidfile))
{
CleanupAndExit($pidfile, "Another instance is running!");
exit;
}
db_connect();
ftsearch_refresh();
CleanupAndExit($pidfile);
//
// checks if other instance is running, creates pid-file for locking
//
function CreatePidFile($PidFile)
{
if(!CheckDaemon($PidFile))
{
return false;
}
if(file_exists($PidFile))
{
echo "Error: Pidfile (".$PidFile.") already present at ".__FILE__.":".__LINE__."!\n";
return false;
}
else
{
if($pidfile = @fopen($PidFile, "w"))
{
fputs($pidfile, posix_getpid());
fclose($pidfile);
return true;
}
else
{
echo "can't create Pidfile $PidFile at ".__FILE__.":".__LINE__."!\n";
return false;
}
}
}
//
// checks if other instance of process is running..
//
function CheckDaemon($PidFile)
{
if($pidfile = @fopen($PidFile, "r"))
{
$pid_daemon = fgets($pidfile, 20);
fclose($pidfile);
$pid_daemon = (int)$pid_daemon;
// process running?
if(posix_kill($pid_daemon, 0))
{
// yes, good bye
echo "Error: process already running with pid=$pid_daemon!\n";
false;
}
else
{
// no, remove pid_file
echo "process not running, removing old pid_file (".$PidFile.")\n";
unlink($PidFile);
return true;
}
}
else
{
return true;
}
}
//
// deletes pid-file
//
function CleanupAndExit($PidFile, $message = false)
{
if($pidfile = @fopen($PidFile, "r"))
{
$pid = fgets($pidfile, 20);
fclose($pidfile);
if($pid == posix_getpid())
unlink($PidFile);
}
else
{
echo "Error: can't delete own pidfile (".$PidFile.") at ".__FILE__.":".__LINE__."!\n";
}
if($message)
{
echo $message . "\n";
}
}
?>

View File

@ -0,0 +1,28 @@
#!/usr/local/bin/php -q
<?php
/****************************************************************************
For license information see doc/license.txt
Unicode Reminder メモ
refresh the search-index of all modified descriptions
This is run as separate cronjob because it takes much time, may not
fit into the runcron frequency and should run at some time in the night.
****************************************************************************/
// needs absolute rootpath because called as cronjob
$opt['rootpath'] = dirname(__FILE__) . '/../../';
require $opt['rootpath'] . 'lib2/cli.inc.php';
require $opt['rootpath'] . 'lib2/search/ftsearch.inc.php';
$process_sync = new ProcessSync('fill_searchindex');
if ($process_sync->Enter())
{
ftsearch_refresh();
$process_sync->Leave();
}
?>

View File

@ -20,27 +20,26 @@
die("ERROR: runcron must be run by '" . $opt['cron']['username'] . "' but was called by '" . $processUser['name'] . "'\n".
"Try something like 'sudo -u ".$opt['cron']['username']." php runcron.php'.\n");
// use posix pid-files to lock process
if (!CreatePidFile($opt['cron']['pidfile']))
// ensure that we do not run concurrently
$process_sync = new ProcessSync('runcron');
if ($process_sync->Enter())
{
CleanupAndExit($opt['cron']['pidfile'], "Another instance is running!");
exit;
// Run as system user, if possible.
// This is relevant e.g. for publishing and for auto-archiving caches.
if ($opt['logic']['systemuser']['user'] != '')
if (!$login->system_login($opt['logic']['systemuser']['user']))
die("ERROR: runcron system user login failed");
$modules_dir = $opt['rootpath'] . 'util2/cron/modules/';
$hDir = opendir($modules_dir);
while (false !== ($file = readdir($hDir)))
if (substr($file, -10) == '.class.php')
require($modules_dir . $file);
$process_sync->Leave();
}
// Run as system user, if possible.
// This is relevant e.g. for publishing and for auto-archiving caches.
if ($opt['logic']['systemuser']['user'] != '')
if (!$login->system_login($opt['logic']['systemuser']['user']))
die("ERROR: runcron system user login failed");
$modules_dir = $opt['rootpath'] . 'util2/cron/modules/';
$hDir = opendir($modules_dir);
while (false !== ($file = readdir($hDir)))
if (substr($file, -10) == '.class.php')
require($modules_dir . $file);
CleanupAndExit($opt['cron']['pidfile']);
function checkJob(&$job)
{
@ -53,90 +52,4 @@ function checkJob(&$job)
}
}
//
// checks if other instance is running, creates pid-file for locking
//
function CreatePidFile($PidFile)
{
if(!CheckDaemon($PidFile))
{
return false;
}
if(file_exists($PidFile))
{
echo "Error: Pidfile (".$PidFile.") already present at ".__FILE__.":".__LINE__."!\n";
return false;
}
else
{
if($pidfile = @fopen($PidFile, "w"))
{
fputs($pidfile, posix_getpid());
fclose($pidfile);
return true;
}
else
{
echo "can't create Pidfile $PidFile at ".__FILE__.":".__LINE__."!\n";
return false;
}
}
}
//
// checks if other instance of process is running..
//
function CheckDaemon($PidFile)
{
if($pidfile = @fopen($PidFile, "r"))
{
$pid_daemon = fgets($pidfile, 20);
fclose($pidfile);
$pid_daemon = (int)$pid_daemon;
// process running?
if(posix_kill($pid_daemon, 0))
{
// yes, good bye
echo "Error: process already running with pid=$pid_daemon!\n";
false;
}
else
{
// no, remove pid_file
echo "process not running, removing old pid_file (".$PidFile.")\n";
unlink($PidFile);
return true;
}
}
else
{
return true;
}
}
//
// deletes pid-file
//
function CleanupAndExit($PidFile, $message = false)
{
if($pidfile = @fopen($PidFile, "r"))
{
$pid = fgets($pidfile, 20);
fclose($pidfile);
if($pid == posix_getpid())
unlink($PidFile);
}
else
{
echo "Error: can't delete own pidfile (".$PidFile.") at ".__FILE__.":".__LINE__."!\n";
}
if($message)
{
echo $message . "\n";
}
}
?>