summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEudyptula <eitan@mosenkis.net>2009-06-25 17:28:59 -0400
committerEudyptula <eitan@mosenkis.net>2009-06-25 17:28:59 -0400
commitb164ea1e21bf01817ae690703640b0ac7fef615e (patch)
tree5a2c1f38d43059b6b01917be599729328a3086c8 /backend
parentAdded logout and user self-registration with email confirmation; Updates to s... (diff)
downloadingenue-b164ea1e21bf01817ae690703640b0ac7fef615e.tar.gz
ingenue-b164ea1e21bf01817ae690703640b0ac7fef615e.tar.bz2
ingenue-b164ea1e21bf01817ae690703640b0ac7fef615e.zip
Added signal handling and logging; wrote init script; added other nice daemon-like behavior to backend
Diffstat (limited to 'backend')
-rwxr-xr-xbackend/backend.php58
-rw-r--r--backend/functions/build.php2
-rw-r--r--backend/functions/log.php4
-rw-r--r--backend/functions/signals.php23
-rw-r--r--backend/include/signals.php4
5 files changed, 74 insertions, 17 deletions
diff --git a/backend/backend.php b/backend/backend.php
index 7f8d5f0..4d53b6d 100755
--- a/backend/backend.php
+++ b/backend/backend.php
@@ -1,47 +1,77 @@
#!/usr/bin/php
<?php
-require_once(dirname(__FILE__).'/../shared/include/includes.php'); // USE __DIR__ once 5.3.0 is out
-require_once(SHARED.'/include/dbinit.php');
-$pdo=&$S['pdo'];
-$opts=getopt('f');
+require_once(dirname(__FILE__).'/../shared/include/includes.php'); // USE __DIR__ once 5.3.0 is out (and 2 lines down)
+require_once(BACKEND.'/include/signals.php');
+declare(ticks=1);
+$pidfile='/var/run/ingenue.pid'; // Doesn't work when not run as root
+$opts=getopt('fk');
if (isset($opts['f'])) {
$f=pcntl_fork();
switch($f) {
case -1:
die("Failed to fork");
case 0:
- echo "Forked. Commiting suicide.\n";
- exit;
+ $conf['debug']=false;
+ break;
default:
- echo "I am the child!\n";
+ die();
+ }
+}
+if (isset($opts['k'])) {
+ if (is_file($pidfile)) {
+ $pid=trim(file_get_contents($pidfile));
+ if (posix_kill($pid, 0)) {
+ debug("Sending SIGTERM to $pid");
+ if (!posix_kill($pid, SIGTERM)) {
+ debug("Failed to send SIGTERM to $pid");
+ die(1);
+ }
+ } else {
+ debug("Couldn't send signal 0 to $pid");
+ die(1);
+ }
+ } else {
+ debug('No PID file found');
}
+ die();
+}
+if (is_file($pidfile)) {
+ $pid=trim(file_get_contents($pidfile));
+ if (posix_kill($pid, 0))
+ die("Found already running backend PID=$pid.\n");
}
+if (posix_geteuid() !== 0)
+ debug(STDERR, "Not running as root... this is not going to accomplish much.");
+if (file_put_contents($pidfile, posix_getpid()))
+ $unlinkpidfile=true;
+require_once(SHARED.'/include/dbinit.php');
while (true) {
// TODO check first for builds that need to be resumed
- $r=$pdo->query('SELECT * FROM `builds` WHERE `status`="build/ready" ORDER BY `ctime` ASC LIMIT 1');
+ $r=$S['pdo']->query('SELECT * FROM `builds` WHERE `status`="build/ready" ORDER BY `ctime` ASC LIMIT 1');
if ($r->rowCount()) {
$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
$build->start=time();
$build->status='build/running';
$build->write();
- echo 'Starting build id='.$build->id."\n";
+ log_msg('Starting build id='.$build->id);
$image=null;
try {
$image=build($build);
} catch (Exception $e) {
- echo 'Caught exception: '.$e->getMessage()."\n";
+ log_msg('Caught exception: '.$e->getMessage());
$build->status='finished/failed: '.$e->getMessage();
}
$build->finish=time();
- echo 'Finished with build id='.$build->id."\n";
+ log_msg('Finished with build id='.$build->id);
if (isset($image)) {
- echo "Completed image at $image\n";
+ log_msg("Completed image at $image");
$build->status='finished/success';
}
$build->write();
+ unset($build);
}
- echo 'Sleeping...';
+ log_msg('Sleeping...', false);
sleep(5);
- echo "done\n";
+ log_msg("done");
}
?>
diff --git a/backend/functions/build.php b/backend/functions/build.php
index 815f45c..df07fdc 100644
--- a/backend/functions/build.php
+++ b/backend/functions/build.php
@@ -1,7 +1,7 @@
<?php
// This is the main function that carries out a build from start to finish
function build(&$build) {
- global $pdo, $conf, $profile;
+ global $conf, $profile;
$opts=$build->get_buildopts();
$profile=new sql_profile($opts['profile']);
$headers=$profile->get_headers();
diff --git a/backend/functions/log.php b/backend/functions/log.php
index 16476a6..251e526 100644
--- a/backend/functions/log.php
+++ b/backend/functions/log.php
@@ -1,7 +1,7 @@
<?php
// In the future, this will log to the database and require a build ID, but for now it just prints
-function log_msg($msg, $nl=true) {
- echo $msg.($nl?"\n":'');
+function log_msg($msg, $nl=true) { // This is a quick hack, we need to do something smarter
+ debug($msg.($nl?"\n":''));
}
function log_status($msg, $result) {
log_msg($msg."... ".($result?color('[success]', 'green'):color('[failure]', 'red')));
diff --git a/backend/functions/signals.php b/backend/functions/signals.php
new file mode 100644
index 0000000..bb3d44d
--- /dev/null
+++ b/backend/functions/signals.php
@@ -0,0 +1,23 @@
+<?php
+function handle_signal($sig=null) {
+ global $pidfile, $unlinkpidfile, $build, $task;
+ if (isset($pidfile, $unlinkpidfile) && $unlinkpidfile)
+ unlink($pidfile);
+ if (isset($build)) {
+ if (isset($task)) {
+ $task->finish=time();
+ $task->exit=-$sig;
+ $task->write();
+ debug('task '.$task->id.' given exit status '.-$sig);
+ } else {
+ debug('$task not set');
+ }
+ $build->finish=time();
+ $build->status='finished/failed: got signal '.$sig;
+ $build->write();
+ debug('build '.$build->id.' given status '.$build->status);
+ }
+ debug("\nGot signal $sig - exiting");
+ exit;
+}
+?>
diff --git a/backend/include/signals.php b/backend/include/signals.php
new file mode 100644
index 0000000..a2a0403
--- /dev/null
+++ b/backend/include/signals.php
@@ -0,0 +1,4 @@
+<?php
+pcntl_signal(SIGINT, 'handle_signal');
+pcntl_signal(SIGTERM, 'handle_signal');
+?>