Skip to content

Commit

Permalink
Added a simple lock for cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
albancrommer committed Jul 5, 2011
1 parent 0f90e65 commit 071a0bc
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 31 deletions.
4 changes: 3 additions & 1 deletion actions/indexAction.php
Expand Up @@ -26,6 +26,8 @@ public function run()
while ($entry = $results->fetch_assoc()) {
$entries[] = $entry;
}
return $data['entries'] = $entries;
$data['title'] = "Accueil";
$data['entries'] = $entries;
return $data;
}
}
5 changes: 3 additions & 2 deletions actions/readAction.php
Expand Up @@ -46,8 +46,9 @@ public function run()
$results = DBO::getAdapter()->query($sql);
if( $results->num_rows > 0 )
while ($r = $results->fetch_assoc()) {
$linksFrom[] = $r;}
$data['linksFrom'] = $linksFrom;
$linksFrom[] = $r;}
$data['linksFrom'] = $linksFrom;
$data['title'] = "Infos sur ".$entry['url'];

return $data;
}
Expand Down
1 change: 1 addition & 0 deletions actions/searchAction.php
Expand Up @@ -34,6 +34,7 @@ public function run()
$data['entries'][] = $r;
}
}
$data['title'] = "Recherche sur ".$searchStr;
return $data;
}
}
3 changes: 1 addition & 2 deletions bot.php
Expand Up @@ -7,7 +7,6 @@ class curlException extends exception
{
}


$bot = new crawlBot(array(
"seomoz-user" => $config['seomoz-user'],
"seomoz-key" => $config['seomoz-key'],
Expand Down Expand Up @@ -35,7 +34,7 @@ class curlException extends exception
} catch (curlException $e) {
LOG::getSingleton()->alert("EXCEPTION : ".$e->getMessage()."\n");
LOG::getSingleton()->alert("KILL PROCESS\n");
exec('./cronjob.sh >> /dev/null 2>&1 &');
exec($config['php'].' cronjob.php >> /dev/null 2>&1 &');
echo("\n############ END ##########\n");
die('end.');
} catch (exception $e) {
Expand Down
43 changes: 43 additions & 0 deletions cronjob.php
@@ -0,0 +1,43 @@
<?php

if( 'cli' != PHP_SAPI ) die('Not running from CLI');
include('bootstrap.php');
include("library/botManager.php");
try {

$botManager = new botManager();

$botManager->setLockFile($config['lockfile']);

$nInstances = $botManager->getInstancesCount();
if( $botManager->getInstancesCount() >= $config["instances"]){
echo("Enough instances already\n");
die();
}

exec($config["php"]." bot.php >> /dev/null 2>&1 & echo $!", $output, $return_var );

$pid = $output[0];

if( 0 != $return_var){

throw new Exception("Process returned an error", 1);

}elseif( $pid > 0 ){

$botManager->add( array(
"pid" => $pid
));

}

} catch (Exception $e) {

$message .= "###########\n";
$message .= $e->getMessage()." ";
$message .= $e->getTraceAsString();
$message .= "\n###########\n";

print $message;
LOG::getSingleton()->alert($message);
}
19 changes: 0 additions & 19 deletions cronjob.sh

This file was deleted.

94 changes: 94 additions & 0 deletions library/botManager.php
@@ -0,0 +1,94 @@
<?php
/**
*
*/
class botManager
{

function __construct( $options = null )
{
if( null != $options['lockfile'] ){
$this->setLockFile( $options['lockfile']);
}
}

public function add( $options = null)
{
$handle = fopen($this->_lockfile,"a");
fwrite($handle,implode(',',$options)."\n");
fclose($handle);

}
public function dumpFile()
{
$arFile = file($this->_lockfile);
echo ("lockfile : $this->_lockfile\n");
var_dump( $arFile );
}

public function kill( $pid = null )
{
$cmd = "kill $pid";
exec($cmd,$output,$result);
if( $result == 0)
return true;
return false;

}

public function getInstancesCount()
{
$arFile = file($this->_lockfile);
if(count($arFile) < 1 ) return 0;
$nInstances = 0;
$n = count($arFile);
foreach ($arFile as $bot) {
$botStatus=explode(',',$bot);
if( $this->running( $botStatus[0] )){
$nInstances++;
$current[]=$bot;
}
}
$this->save($current);
return $nInstances;

}
public function running( $pid = null )
{
$cmd ="ps $pid";
exec($cmd,$output,$result);
if( count($output)>1)
return true;
return false;
}

public function save( $bots = null )
{
if( null == $bots) $c="";
elseif( count($bots) < 1 ) $c="";
else foreach ($bots as $key => $value) {
$c .= $value;
}
file_put_contents($this->_lockfile,$c);

}

public function setLockFile($lockfile = null)
{
if( null == $lockfile ){
throw( new Exception("botManager:setLockFile missing parameter : lockfile."));}

if(!touch($lockfile))
throw new Exception("botManager::setLockFile NOT A VALID FILE ", 1);

if( !is_file($lockfile)){
throw new Exception("botManager::setLockFile NOT A VALID FILE ", 1);
}
if( !is_writable($lockfile)){
throw new Exception("botManager::setLockFile NOT WRITABLE ", 1);
}
$this->_lockfile = $lockfile;
}

}

3 changes: 1 addition & 2 deletions templates/template.phtml
Expand Up @@ -2,8 +2,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<title><?php echo $data['title']; ?> </title>
<link href="<?php echo $root ?>/css/screen.css" media="screen" rel="stylesheet" type="text/css" />

</head>
Expand Down
1 change: 1 addition & 0 deletions tmp/trustrankbot.lock
@@ -0,0 +1 @@
19496
8 changes: 3 additions & 5 deletions views/index.phtml
@@ -1,13 +1,11 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$entries = $data;
$entries = $data['entries'];
?>
<div class="page index">
<table class="results-list">
<?php $cycle = 1 ?>
<?php if( count($data) > 0) :?>
<?php printTable($data )?>
<?php if( count($entries) > 0) :?>
<?php printTable($entries )?>
<?php else:?>
<p> None found </p>
<?php endif;?>
Expand Down

0 comments on commit 071a0bc

Please sign in to comment.