added fork on linux as well as implimented pid file to check for other running crons

This commit is contained in:
Botanic 2013-10-23 15:26:36 -07:00
parent 4cb47986bb
commit 5ab5b58649

View file

@ -14,58 +14,75 @@ class Sync{
* These changes are: createPermissions, createUser, change_pass, change_mail * These changes are: createPermissions, createUser, change_pass, change_mail
*/ */
static public function syncdata ($display = true) { static public function syncdata ($display = true) {
try { if (function_exists('pcntl_fork')) {
$dbl = new DBLayer("lib"); $pid = pcntl_fork();
$statement = $dbl->executeWithoutParams("SELECT * FROM ams_querycache"); }
$rows = $statement->fetchAll(); $pidfile = '/tmp/ams_cron_pid';
foreach ($rows as $record) { if(isset($pid)) {
// We're the main process.
} else {
if(!file_exists($pidfile)) {
$pid = getmypid();
$file = fopen($pidfile, 'w');
$db = new DBLayer($record['db']); fwrite($file, $pid);
switch($record['type']) { fclose($file);
case 'createPermissions':
$decode = json_decode($record['query']); try {
$values = array('username' => $decode[0]); $dbl = new DBLayer("lib");
//make connection with and put into shard db & delete from the lib $statement = $dbl->executeWithoutParams("SELECT * FROM ams_querycache");
$sth = $db->execute("SELECT UId FROM user WHERE Login= :username;", $values); $rows = $statement->fetchAll();
$result = $sth->fetchAll(); foreach ($rows as $record) {
foreach ($result as $UId) {
$ins_values = array('id' => $UId['UId']); $db = new DBLayer($record['db']);
$db->execute("INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES (:id, 'r2', 'OPEN');", $ins_values); switch($record['type']) {
$db->execute("INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES (:id , 'ryzom_open', 'OPEN');", $ins_values); case 'createPermissions':
} $decode = json_decode($record['query']);
break; $values = array('username' => $decode[0]);
case 'change_pass': //make connection with and put into shard db & delete from the lib
$decode = json_decode($record['query']); $sth = $db->execute("SELECT UId FROM user WHERE Login= :username;", $values);
$values = array('user' => $decode[0], 'pass' => $decode[1]); $result = $sth->fetchAll();
//make connection with and put into shard db & delete from the lib foreach ($result as $UId) {
$db->execute("UPDATE user SET Password = :pass WHERE Login = :user",$values); $ins_values = array('id' => $UId['UId']);
break; $db->execute("INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES (:id, 'r2', 'OPEN');", $ins_values);
case 'change_mail': $db->execute("INSERT INTO permission (UId, ClientApplication, AccessPrivilege) VALUES (:id , 'ryzom_open', 'OPEN');", $ins_values);
$decode = json_decode($record['query']); }
$values = array('user' => $decode[0], 'mail' => $decode[1]); break;
//make connection with and put into shard db & delete from the lib case 'change_pass':
$db->execute("UPDATE user SET Email = :mail WHERE Login = :user",$values); $decode = json_decode($record['query']);
break; $values = array('user' => $decode[0], 'pass' => $decode[1]);
case 'createUser': //make connection with and put into shard db & delete from the lib
$decode = json_decode($record['query']); $db->execute("UPDATE user SET Password = :pass WHERE Login = :user",$values);
$values = array('login' => $decode[0], 'pass' => $decode[1], 'mail' => $decode[2] ); break;
//make connection with and put into shard db & delete from the lib case 'change_mail':
$db->execute("INSERT INTO user (Login, Password, Email) VALUES (:login, :pass, :mail)",$values); $decode = json_decode($record['query']);
break; $values = array('user' => $decode[0], 'mail' => $decode[1]);
//make connection with and put into shard db & delete from the lib
$db->execute("UPDATE user SET Email = :mail WHERE Login = :user",$values);
break;
case 'createUser':
$decode = json_decode($record['query']);
$values = array('login' => $decode[0], 'pass' => $decode[1], 'mail' => $decode[2] );
//make connection with and put into shard db & delete from the lib
$db->execute("INSERT INTO user (Login, Password, Email) VALUES (:login, :pass, :mail)",$values);
break;
}
$dbl->execute("DELETE FROM ams_querycache WHERE SID=:SID",array('SID' => $record['SID']));
}
if ($display == true) {
print('Syncing completed');
} }
$dbl->execute("DELETE FROM ams_querycache WHERE SID=:SID",array('SID' => $record['SID']));
} }
if ($display == true) { catch (PDOException $e) {
print('Syncing completed'); if ($display == true) {
} print('Something went wrong! The shard is probably still offline!');
} print_r($e);
catch (PDOException $e) { }
if ($display == true) {
print('Something went wrong! The shard is probably still offline!');
print_r($e);
} }
unlink($pidfile);
} }
} }
}
} }