Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix web server kill when the SIGKILL constant isn't available #150

Merged
merged 1 commit into from
Oct 21, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions tests/bootstrap-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@
$php_version = phpversion();
$php_major = floatval(substr($php_version, 0, 3));

// Define SIGKILL if pcntl is not found
if (!function_exists('pcntl_signal')) {
define('SIGKILL', 9);
}

if ($php_major < 5.4) {
define('WITHOUT_SERVER', true);
define('WITHOUT_SERVER', true);
} else {
// Command that starts the built-in web server
$command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);

// Execute the command and store the process ID
$output = array();
exec($command, $output, $exit_code);

// sleep for a second to let server come up
sleep(1);
$pid = (int) $output[0];

// check server.log to see if it failed to start
$server_logs = file_get_contents("./server.log");
if (strpos($server_logs, "Fail") !== false) {
// server failed to start for some reason
print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
print_r($server_logs);
exit(1);
}

echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;

register_shutdown_function(function() {
// cleanup after ourselves -- remove log file, shut down server
global $pid;
unlink("./server.log");
posix_kill($pid, SIGKILL);
});
// Command that starts the built-in web server
$command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);

// Execute the command and store the process ID
$output = array();
exec($command, $output, $exit_code);

// sleep for a second to let server come up
sleep(1);
$pid = (int) $output[0];

// check server.log to see if it failed to start
$server_logs = file_get_contents("./server.log");
if (strpos($server_logs, "Fail") !== false) {
// server failed to start for some reason
print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
print_r($server_logs);
exit(1);
}

echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;

register_shutdown_function(function() {
// cleanup after ourselves -- remove log file, shut down server
global $pid;
unlink("./server.log");
posix_kill($pid, SIGKILL);
});
}