cleanup sync

This commit is contained in:
Botanic 2013-10-23 17:08:27 -07:00
parent 51678eb618
commit 74aeedc681

View file

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