Lock import/export job while running, to prevent multiple instances.

This commit is contained in:
Nathan Gray 2012-02-14 20:29:33 +00:00
parent 68798a2200
commit 1109aea63a

View File

@ -363,16 +363,37 @@
/** /**
* Execute a scheduled import or export * Execute a scheduled import or export
*/ */
public static function exec($data) { public static function exec($data)
{
ob_start(); ob_start();
$data['record_count'] = 0; $data['record_count'] = 0;
unset($data['errors']); unset($data['errors']);
unset($data['warnings']); unset($data['warnings']);
unset($data['result']); unset($data['result']);
if($data['lock'])
{
// Lock expires
if($data['lock'] < time())
{
unset($data['lock']);
$data['warnings'][][] = lang('Lock expired on previous run');
}
else
{
// Still running
ob_end_flush();
return;
}
}
$data['last_run'] = time(); $data['last_run'] = time();
// Lock job for an hour to prevent multiples overlapping
$data['lock'] = time() + 3600;
self::update_job($data, true);
// check file // check file
$file_check = self::check_target($data); $file_check = self::check_target($data);
if($file_check !== true) { if($file_check !== true) {
@ -380,6 +401,7 @@
// Update job with results // Update job with results
self::update_job($data); self::update_job($data);
ob_end_flush();
fwrite(STDERR,'importexport_schedule: ' . date('c') . ": $file_check \n"); fwrite(STDERR,'importexport_schedule: ' . date('c') . ": $file_check \n");
return; return;
} }
@ -438,6 +460,10 @@
foreach($targets as $target) foreach($targets as $target)
{ {
// Update lock timeout
$data['lock'] = time() + 3600;
self::update_job($data, true);
$resource = null; $resource = null;
try { try {
if($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' )) { if($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' )) {
@ -506,7 +532,10 @@
} }
// Run time in minutes // Run time in minutes
$data['run_time'] = (time() - $data['last_run']) / 60; $data['run_time'] = round((time() - $data['last_run']) / 60,1);
// Clear lock
$data['lock'] = 0;
// Update job with results // Update job with results
self::update_job($data); self::update_job($data);
@ -526,7 +555,7 @@
* Update the async job with current status, and send a notification * Update the async job with current status, and send a notification
* to user if there were any errors. * to user if there were any errors.
*/ */
private static function update_job($data) { private static function update_job($data, $no_notification = false) {
$id = self::generate_id($data); $id = self::generate_id($data);
$async = ExecMethod('phpgwapi.asyncservice.read', $id); $async = ExecMethod('phpgwapi.asyncservice.read', $id);
$async = $async[$id]; $async = $async[$id];
@ -539,6 +568,7 @@
$data $data
); );
} }
if($no_notification) return $result;
// Send notification to user // Send notification to user
if($data['warnings'] || $data['errors']) if($data['warnings'] || $data['errors'])