1
0
Fork 0
oldhaven/php/bin/parser/handler.php

89 lines
2.2 KiB
PHP
Raw Normal View History

2011-04-02 14:57:17 +00:00
#!/opt/local/bin/php
<?php
2011-04-04 17:40:52 +00:00
require_once '/www/server/php/common.php';
2011-04-02 14:57:17 +00:00
2011-04-02 20:15:22 +00:00
$tmp = array_slice(scandir(Config::get('app:Parser:bot_stats_dir')), 2);
$bots = array();
foreach ($tmp as $bfile) {
$bots[str_replace('.json', '', $bfile)] = json_decode(file_get_contents(Config::get('app:Parser:bot_stats_dir').$bfile));
}
2011-04-04 20:59:50 +00:00
2011-04-02 20:15:22 +00:00
foreach ($bots as $name => $bot) {
$tmp = exec("ps ".$bot->pid);
$bot->active = (int) (strpos($tmp, strval($bot->pid)) !== false);
}
if (!isset($argv[1])) {
echo "Bad action\n";
die;
2011-04-02 14:57:17 +00:00
}
switch ($argv[1]) {
case 'add':
2011-04-02 20:15:22 +00:00
$bot_name = (isset($argv[2]) ? ucfirst($argv[2]) : false);
if ($bot_name && isset($bots[$bot_name])) {
$bot = $bots[$bot_name];
if($bot->active) {
echo $bot_name ." is working already\n";
} else {
echo "Launching ". $bot_name ."\n";
2011-04-04 17:43:05 +00:00
shell_exec(ROOT_DIR ."/bin/parser/worker_html_grabber.php ". $bot_name ." > /www/parser_data/log/". $bot_name .".log &");
2011-04-02 20:15:22 +00:00
}
} else {
$bots = custom_shuffle($bots);
foreach ($bots as $name => $bot) {
if (!$bot->active) {
echo "Launching ". $name ."\n";
2011-04-04 17:43:05 +00:00
shell_exec(ROOT_DIR ."/bin/parser/worker_html_grabber.php ". $name ." > /www/parser_data/log/". $name .".log &");
2011-04-04 20:59:50 +00:00
if ($bot_name != 'All') {
die;
}
2011-04-02 20:15:22 +00:00
}
}
2011-04-04 20:59:50 +00:00
echo "All bots are working.\n";
2011-04-02 20:15:22 +00:00
}
2011-04-02 14:57:17 +00:00
break;
2011-04-02 20:15:22 +00:00
case 'retire':
$bot_name = (isset($argv[2]) ? ucfirst($argv[2]) : false);
2011-04-04 20:59:50 +00:00
if ($bot_name && isset($bots[$bot_name])) {
2011-04-02 20:15:22 +00:00
$bot = $bots[$bot_name];
if(!$bot->active) {
echo $bot_name ." is not working now\n";
} else {
echo "Stopping ". $bot_name ."\n";
shell_exec("kill ". $bot->pid);
}
} else {
$bots = custom_shuffle($bots);
foreach ($bots as $name => $bot) {
if ($bot->active) {
echo "Stopping ". $name ."\n";
shell_exec("kill ". $bot->pid);
2011-04-04 20:59:50 +00:00
if ($bot_name != 'All') {
die;
}
2011-04-02 20:15:22 +00:00
}
}
2011-04-04 20:59:50 +00:00
echo "All bots are stopped.\n";
2011-04-02 14:57:17 +00:00
}
2011-04-02 20:15:22 +00:00
break;
2011-04-02 14:57:17 +00:00
case 'list':
2011-04-02 20:15:22 +00:00
foreach($bots as $name => $bot) {
echo $name .': '. ($bot->active ? 'working' : 'down') ."\n";
2011-04-02 14:57:17 +00:00
}
break;
default:
2011-04-02 20:15:22 +00:00
echo "Bad action\n";
2011-04-02 14:57:17 +00:00
break;
2011-04-02 20:15:22 +00:00
}
function custom_shuffle($my_array = array()) {
$copy = array();
while (count($my_array)) {
$element = array_rand($my_array);
$copy[$element] = $my_array[$element];
unset($my_array[$element]);
}
return $copy;
2011-04-02 14:57:17 +00:00
}