$data = mysqli_fetch_array(mysqli_query($mysql, $checkUser)); $lastStart = intval(trim($data['time'])); $checkReboots = "SELECT time FROM swift_logs WHERE username='******' AND action LIKE '%{$server}' AND time>{$lastStart} ORDER BY id DESC LIMIT 5"; $getTimes = mysqli_query($mysql, $checkReboots); $i = 1; $lastTime = time(); while ($times = mysqli_fetch_array($getTimes)) { $time = intval(trim($times['time'])); if ($lastTime - $time <= 75) { $i++; $lastTime = $time; } } if ($i == 5) { echo "\nServer {$server} stopped due to 5 reboots in a row."; stopServer($hostIp, $sshport, $account, $accpass); $closeServer = "UPDATE swift_servers SET active=0 WHERE id={$srvid}"; mysqli_query($mysql, $closeServer); $time = time(); $setAlert = "INSERT INTO swift_logs (username, ip, action, time) VALUES('CRON Task', '{$hostIp}', 'Server {$server} did not start correctly and has now been stopped.', '{$time}')"; mysqli_query($mysql, $setAlert); continue; } $time = time(); echo "\nRebooting server {$server} due to screen being down"; $query33 = "SELECT active FROM swift_servers WHERE id={$srvid}"; $result2 = mysqli_fetch_array(mysqli_query($mysql, $query33)); if (intval($result2['active']) == 1) { $query2 = "INSERT INTO swift_logs (username, ip, action, time) VALUES ('CRON Task', '{$hostIp}', 'Restarted server {$server}', '{$time}')"; restartServer($hostIp, $sshport, $account, $accpass, $startcmd); mysqli_query($mysql, $query2);
function runTakeoverTest() { global $test_run_id; $pid = posix_getpid(); $takeoverFile = '/tmp/takeover.' . $test_run_id; $serverProc = $serverPort = $adminPort = null; $debugPort = false; $serverHome = __DIR__ . '/..'; $serverRoot = __DIR__ . '/../server_root'; $customArgs = " -vServer.TakeoverFilename={$takeoverFile}"; try { $serverProc = startServer($serverPort, $adminPort, $debugPort, $serverHome, $serverRoot, $customArgs); if ($serverProc === null) { echo 'failed to start first server'; return; } $takeoverid = 'new' . $test_run_id; $customArgs = ''; $newServerProc = takeoverOldServer($serverPort, $adminPort, $serverHome, $serverRoot, $takeoverFile, $serverProc, $customArgs, $takeoverid); if ($newServerProc === null) { echo 'failed to start another server to takeover'; return; } // Check and make sure that the server is always online, and old // server exits after a finite amount of time. It is OK if both // servers are working at the same time. $testids = array($test_run_id, $takeoverid); for ($i = 1;; $i++) { if (!checkServerId($serverPort, $testids)) { return; } // Old server should die after a while $status = proc_get_status($serverProc); if ($status === false) { error_log('error retrieving old server status'); proc_close($serverProc); return; } if (!$status['running']) { break; } // old server is gone if ($i > 300) { // Either the new process is initializing too slowly, or the // old server is stuck after takeover. 5 minutes should be // enough even for a debug build on a slow machine. error_log('old server still running after 300 seconds?'); return; } sleep(1); } if (!checkServerId($serverPort, $takeoverid)) { return; } stopServer($adminPort, $newServerProc); } catch (Exception $e) { error_log("Caught exception, test failed, pid={$pid}, exn=" . $e->getMessage()); killChildren($pid); if ($serverProc) { proc_close($serverProc); } if ($newServerProc) { proc_close($newServerProc); } error_log('test failed'); return; } echo 'takeover successful'; }
function runTest($testName, $testController) { try { $serverPort = get_random_port(); $adminPort = get_random_port(); while ($adminPort === $serverPort) { $adminPort = get_random_port(); } $debugPort = get_random_port(); while ($debugPort === $serverPort || $debugPort === $adminPort) { $debugPort = get_random_port(); } $pid = posix_getpid(); $serverProc = null; $clientProcessId = 0; $serverProc = startServer($serverPort, $adminPort, $debugPort); waitForServerToGetGoing($serverPort); startDebuggerClient($debugPort, "/debugger/{$testName}.in", $pipes); $clientProcessId = getClientProcessId($pipes[1]); if (!$clientProcessId || ($clientProcessId = intval($clientProcessId)) <= 0) { tlog('Failed to communicate with the debugger client process'); dumpLogFilesToStdoutAndDie(); } tlog("Debugger client process id = {$clientProcessId}"); $testController($pipes[1], $clientProcessId, $serverPort); // Echo stderr, just in case. // (It was redirected to stdout, so this should be empty). echo stream_get_contents($pipes[2]); stopServer($adminPort); } catch (Exception $e) { error_log("Caught exception, test failed, pid={$pid}"); killChildren(posix_getpid()); error_log('test failed'); } }
function restartServer($hostIp, $sshport, $account, $accpass, $startcmd) { stopServer($hostIp, $sshport, $account, $accpass); startServer($hostIp, $sshport, $account, $accpass, $startcmd); }