Process Rendering is applicable to queen processes (for continues processes), worker processes (one-time processes), and kannel handlers. Process will be rendered using config file called manifest and its process_type class file. Process should be install in its appropriate entity (queen, worker or kannel). Config file is rendering control that define what should be used and how to render the process. process_type class file is used to perform the defined process.
Below is as an example:
configuration file:
<configuration id="ant_monitor">
<section name="core">
<!-- Process Configuration-->
<config name="process">
<config name="type">ant_monitor</config>
<config name="name">AntMonitor</config>
<config name="start">#TIMESTAMP FIRST TIME FOR EXECUTION#</config>
<config name="end">#TIMESTAMP LAST TIME FOR EXECUTION#</config>
</config>
<!--Worker Location -->
<config name="location">#LOCATION OF WORKERS#</config>
<config name="ants">#ANTS#</config>
<config name="exceptionants">#EXCEPTION ANTS#</config>
</section>
</configuration>
processor file:
class ant_monitor extends workerProcess {
/**
* An array containing all of our acceptable file extensions
*
* @var array
*/
private $nestClient = null;
/**
* index postion pointer
*
* @var integer
*/
private $index = null;
/**
* config object
*
* @var object
*/
private $configobj = null;
/**
* The construct function for our object.
*
* @param object $worker
*
* @return void
*/
public function __construct(&$worker){
parent::__construct($worker);
if (!class_exists('NestClient'))
include(CIISONLINE_System::GetSystemDir().'Nest'.DIRECTORY_SEPARATOR.'client.php');
$this->nestClient = new NestClient();
$this->configobj = new CIISONLINE_Config_XML();
}
/**
* Configure processWorker (e.g.: Turns our config options into stored array data).
*
* @return void
*/
public function configure(){
$this->data = null;
$DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR;
$exceptionants = array();
if (isset($this->config->exceptionants) && $this->config->exceptionants!='') {
$exceptionants = explode(',', $this->config->exceptionants);
}
$exceptionants[] = $this->worker->path;
if (isset($this->config->location) && $this->config->location!='') {
$dir = explode(',', $this->config->location);
foreach ($dir as $directory) {
$directory = realpath($directory);
if (is_dir($directory)) {
if (substr($directory,-strlen($DIRECTORY_SEPARATOR))!=$DIRECTORY_SEPARATOR) $directory .= $DIRECTORY_SEPARATOR;
foreach (scandir($directory) as $ant) {
if (in_array($ant, array('.','..','.svn'))) continue;
if (!is_dir($directory.$ant) || in_array($directory.$ant.$DIRECTORY_SEPARATOR,$exceptionants)) continue;
$this->data[] = $directory.$ant.$DIRECTORY_SEPARATOR;
}
}
}
}
if (isset($this->config->ants) && $this->config->ants!='') {
$ants = explode(',', $this->config->ants);
foreach($ants as $ant) {
$ant = realpath($ant);
if (is_dir($ant)) {
if (substr($ant,-strlen($DIRECTORY_SEPARATOR))!=$DIRECTORY_SEPARATOR) $ant .= $DIRECTORY_SEPARATOR;
if (in_array($ant,$exceptionants)) continue;
$this->data[] = $ant;
}
}
}
if (count($this->data)>0) $this->index=0; else $this->index=-1;
}
/**
* If you overload workerProcess's onInit function, be sure to call
* onInit through workerProcess with the line:
* parent::onInit($config, $filename);
*
* If you neglect to do this, the workerProcess will load improperly and therefore
* not work correctly within a worker.
*
* @param object $config The configseed configuration handler
* @param string $filename The config file which generated this
* instance
*
* @return void
*/
public function onInit($config, $filename) {
parent::onInit($config, $filename);
$this->metrics['process.status'] = 'OK';
$this->configure();
}
/**
* If you overload workerProcess's onReconfigure function, be sure to call
* onReconfigure through workerProcess with the line:
* parent::onReconfigure($config, $filename);
*
* If you neglect to do this, the workerProcess will reload improperly and
* therefore stop working correctly within a worker.
*
* @param object $config The configseed configuration handler
* @param string $filename The config file which generated this
* instance
*
* @return void
*/
public function onReconfigure($config, $filename) {
parent::onReconfigure($config, $filename);
$this->metrics['process.status'] = 'OK';
$this->configure();
}
/**
* A required static breadcrumb which tells configseed what congiruation
* options we should have.
*
* This is where you should mark options from your configuration file as
* either required (Necessary for your object to work) or optional (not
* necessary to functionality, but rather more fine-tune configuration).
*
* Be sure to call both workerProcess::docConfig() with
* their proper options to ensure that you build an accurate map of options.
*
* @param object $config The configuration object
*
* @return void
*/
static public function docConfig(&$config) {
workerProcess::docConfig($config);
$config->addOption('required', 'location','An absolute paths to workers');
}
/**
* evaluate if data has been set.
*
* @return true/false
*/
public function isEmptyData(){
return ((isset($this->data) && $this->index>=0)?false:true);
}
/**
* evaluate if cursor data is in the end position
*
* @return true/false
*/
public function isEndData(){
if (isset($this->data)) return (($this->index==count($this->data))?true:false);
}
/**
* evaluate if cursor data is in the first position.
*
* @return true/false
*/
public function isFirstData(){
if (isset($this->data)) return (($this->index==0)?true:false);
}
/**
* process the current data as expectced
*
* @return true/false
*/
public function processCurrentData(){
if (isset($this->data) && $this->index>=0 && !$this->isEndData()) {
$DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR;
$path = $this->data[$this->index];
$ant = 'worker';
$antconfig = $path.'configuration'.$DIRECTORY_SEPARATOR.'worker.conf';
if (!file_exists($antconfig)) {
$ant = 'queen';
$antconfig = $path.'configuration'.$DIRECTORY_SEPARATOR.'queen.conf';
}
if (!file_exists($antconfig)) return;
$config = $this->configobj;
$config->load($antconfig,'core');
$host = isset($config->connections->nest->host)?$config->connections->nest->host:'';
$port = isset($config->connections->nest->port)?$config->connections->nest->port:'';
$id = isset($config->connections->nest->id)?$config->connections->nest->id:'';
$password = isset($config->connections->nest->password)?$config->connections->nest->password:'';
if ($host=='' || $port=='' || $id=='' || $password=='') return;
$this->nestClient->Open($host,$port);
$this->nestClient->Authenticate($id,$password,$ant);
// state 2: authenticated. if authenticated then the worker not running
if (($state=$this->nestClient->getState())==2) {
// offline status
if (isset($config->runtime) && (string)$config->runtime!='') {
$runtime = $config->runtime;
} else {
$runtime = $path.'runtime'.$DIRECTORY_SEPARATOR;
}
if (substr($runtime,-strlen($DIRECTORY_SEPARATOR))!=$DIRECTORY_SEPARATOR) $runtime .= $DIRECTORY_SEPARATOR;
if (is_dir($runtime)) {
$runtimedirs = array('command','response');
foreach ($runtimedirs as $runtimedir) {
if (is_dir($runtime.$runtimedir)) {
foreach (scandir($runtime.$runtimedir) as $runtimefile) {
if (in_array($runtimefile,array('.','..'))) continue;
if (is_dir($runtime.$runtimedir.$DIRECTORY_SEPARATOR.$runtimefile))
CIISONLINE_Environment::RemoveDir($runtime.$runtimedir.$DIRECTORY_SEPARATOR.$runtimefile.$DIRECTORY_SEPARATOR);
else @unlink($runtime.$runtimedir.$DIRECTORY_SEPARATOR.$runtimefile);
}
}
}
}
}
$this->nestClient->Close();
$this->worker->controller->service();
CIISONLINE_Environment::Log()->info('', null, null, (__CLASS__), LOGGER_APPLICATION_CATEGORY, "Scan $antconfig $ant.");
$this->metrics['transactions.count']++;
}
}
/**
* moving to the next data
*
* @return true/false
*/
public function moveNextData(){
if (isset($this->data) && $this->index>=0 && !$this->isEndData()) {
++$this->index;
return true;
}
return false;
}
/**
* moving to the previous data
*
* @return true/false
*/
public function movePrevData(){
if (isset($this->data) && $this->index>=0) {
if ($this->index>0) --$this->index;
return true;
}
return false;
}
/**
* moving to the first data
*
* @return true/false
*/
public function moveFirstData(){
if (isset($this->data) && $this->index>=0) {
$this->index=0;
return true;
}
return false;
}
/**
* moving to the last data
*
* @return true/false
*/
public function moveLastData(){
if (isset($this->data) && $this->index>=0) {
$this->index=count($this->data)-1;
return true;
}
return false;
}
/**
* moving to some position of data
*
* @return void
*/
public function moveToData($offset){
if (isset($this->data) && $this->index>=0 && $offset>=0 && $offset<count($this->data)) {
$this->index=$offset;
return true;
}
return false;
}
/**
* The construct function for our object.
*
* @return void
*/
public function __destruct(){
parent::__destruct();
unset($this->nestClient);
unset($this->configobj);
}
/**
* Rutine to execute at the end processing end data.
*
* @return void
*/
public function onEndData(){
$this->metrics['process.status'] = 'OK';
$this->configure();
}
}
No comments:
Post a Comment