po/src/photo.worker.php

173 lines
4.0 KiB
PHP

<?php
// Copyright (C) 2002-2006 Balint Kis (balint@k-i-s.net)
// Copyright (C) 2005-2013 Solomon Peachy (pizza@shaftnet.org)
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
chdir(__DIR__); // Ensures all includes work
include_once "include/config.php";
include_once "include/site.php";
include_once "include/import.php";
include_once "include/print.printer.php";
$database = FALSE;
$workers = array();
$printers = array();
$memcache = FALSE;
if (!$external_workers) {
print "\$external_workers is FALSE. set to TRUE to use me.\n";
exit(1);
}
function child_fn_worker() {
global $database;
global $memcache;
global $po_memcache;
global $po_worker;
global $site_url;
print "Starting Import Worker\n";
/* Connect to the memcache */
if (count($po_memcache)) {
$memcache = new Memcached($site_url);
$memcache->addServers($po_memcache);
}
while (TRUE) {
$database = po_dbconnect();
photo_import_worker($database);
sleep(10);
site_epilog($database);
}
}
function child_fn_printer() {
global $database;
global $memcache;
global $po_memcache;
global $po_worker;
global $site_url;
print "Starting Printer Worker\n";
/* Connect to the memcache */
if (count($po_memcache)) {
$memcache = new Memcached($site_url);
$memcache->addServers($po_memcache);
}
while (TRUE) {
$database = po_dbconnect();
photo_print_worker($database);
sleep(10);
site_epilog($database);
}
}
function dofork_worker() {
global $workers;
$pid = pcntl_fork();
/* Child */
if ($pid == -1) {
/* Gadzooks, something went wrong! */
} else if ($pid == 0) {
child_fn_worker();
exit();
} else {
/* Parent */
$workers["$pid"] = $pid;
po_log("Photo import worker started (total ".sizeof($workers) .") PID $pid ");
}
return $pid;
}
function dofork_printer() {
global $printers;
$pid = pcntl_fork();
/* Child */
if ($pid == -1) {
/* Gadzooks, something went wrong! */
} else if ($pid == 0) {
child_fn_printer();
exit();
} else {
/* Parent */
$printers["$pid"] = $pid;
po_log("Photo printer worker started (total ".sizeof($printers) .") PID $pid ");
}
return $pid;
}
if (!function_exists("pcntl_fork")) {
print "PHP's PCNTL extension is not installed or enabled.\n";
exit(1);
}
/* Disable execution limits */
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);
# look up info on configured user/group
$uinfo = posix_getpwnam($external_username);
$ginfo = posix_getgrnam($external_groupname);
if (($uinfo === FALSE) || ($ginfo == FALSE)) {
print "Invalid username or groupname specified.\n";
exit(1);
}
if ((posix_getuid() != 0) &&
(posix_getuid() != $uinfo['uid'])) {
print "Needs to be run as root! (Will drop privilges ASAP)\n";
exit(1);
}
# drop privlidges!
posix_setgid($ginfo['gid']);
posix_setuid($uinfo['uid']);
/* Start up new workers. */
for ($i = 0 ; $i < $num_of_workers ; $i++) {
$pid = dofork_worker();
}
$pid = dofork_printer();
/* Clean up after the workers and restart them as necessary */
while(TRUE) {
$pid = pcntl_wait($status, WUNTRACED);
if ($pid > 0) {
if ($workers["$pid"]) {
unset($workers["$pid"]);
$pid = dofork_worker();
} else if ($printers["$pid"]) {
unset($printers["$pid"]);
$pid = dofork_worker();
}
}
}
exit();