Skip to content

Instantly share code, notes, and snippets.

@jbuehring
Created May 31, 2012 16:10
Show Gist options
  • Select an option

  • Save jbuehring/2844462 to your computer and use it in GitHub Desktop.

Select an option

Save jbuehring/2844462 to your computer and use it in GitHub Desktop.
<?php
error_reporting(-1);
ini_set('memory_limit', '1536M');
$item_count = 500;
$outdial_queue_timeframe = 115;
$outdial_queue_max_cps = 5;
$outdial_queue_min_cps = 0.12;
foreach ($argv as $arg)
{ $parts = explode('=', $arg);
switch ($parts[0])
{
case 'count':
$item_count = (int)$parts[1] > 0 ? (int)$parts[1] : 500;
break;
case 'timeframe':
$outdial_queue_timeframe = (float)$parts[1] > 0 ? (float)$parts[1] : 115;
break;
case 'maxcps':
$outdial_queue_max_cps = (float)$parts[1] > 0 ? (float)$parts[1] : 5;
break;
case 'mincps':
$outdial_queue_min_cps = (float)$parts[1] > 0 ? (float)$parts[1] : 0.12;
break;
case '?':
case 'help':
output_help();
break;
}
}
/*****/
$parent_pid = getmypid();
log_it("Parent ID $parent_pid: Starting outdial queue processor");
$children = array();
log_it("Parent ID $parent_pid: Preparing to process $item_count over $outdial_queue_timeframe seconds");
/*****/
$sleep_factor = round($item_count / $outdial_queue_timeframe, 1);
if ($sleep_factor > $outdial_queue_max_cps)
{
$sleep_factor = $outdial_queue_max_cps;
}
elseif ($sleep_factor < $outdial_queue_min_cps)
{
$sleep_factor = $outdial_queue_min_cps;
}
$sleep_factor = 1000000 / $sleep_factor;
/*****/
log_it("Parent ID $parent_pid: Child process pace: $sleep_factor");
while ($item_count > 0)
{
$pid = pcntl_fork();
if ($pid == -1)
{
log_it("Parent ID $parent_pid: MAJOR ERROR forking child process, terminating processor.");
break;
}
elseif ($pid == 0)
{
log_it("Child ID ".getmypid().": Forked");
exit( child_process() );
}
else
{
log_it("Parent ID $parent_pid: Forked Child ID $pid");
$children[] = $pid;
usleep($sleep_factor);
}
$item_count--;
}
$start = time();
while (count($children) > 0)
{
$remove = array();
foreach($children as $pid)
{
if (pcntl_waitpid($pid, $status, WNOHANG) > 0)
{
$remove[] = $pid;
log_it("Parent ID $parent_pid: Child pid $pid finished");
}
}
$children = array_diff($children, $remove);
if (count($children) > 0 && (time() - $start) >= 120)
{
foreach ($children as $pid)
{
if (function_exists('posix_kill'))
{
posix_kill($pid, SIGINT);
}
else
{
@exec("kill -9 $pid");
}
log_it("Parent ID $parent_pid: Child pid $pid killed");
}
break;
}
sleep(1);
}
log_it("Parent ID $parent_pid: Terminating");
/***** MAIN PROCESS ENDS HERE *****/
function child_process()
{
$pid = getmypid();
$sleep = mt_rand(20, 45);
log_it("Child ID $pid: Sleeping for $sleep seconds...");
sleep($sleep);
exit(0);
}
function log_it($info)
{
$pretty_date = date("Ymd H:i:s");
$text = sprintf("%-60s", $info);
list($usec, $sec) = explode(" ", microtime());
echo "$pretty_date $usec : $text\n";
}
function output_help()
{
echo "Faux Queue Processor\n";
echo "Item Count: count=500 (optional)\n";
echo "Timeframe: timeframe=115\n";
echo "Max CPS: maxcps=5\n";
echo "Min CPS: mincps=0.12\n";
echo "Help/Usage: help (optional)\n\n";
exit(-1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment