Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f90e65
commit 071a0bc
Showing
10 changed files
with
150 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
19496 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters