快速业务通道

用PHP构建一个简易监视引擎

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-25
定义该引擎的ServiceCheckRunner类:

class ServiceCheckRunner {
 private $num_children;
 private $services = array();
 private $children = array();
 public function _ _construct($conf, $num_children)
 {
  $loggers = array();
  $this->num_children = $num_children;
  $conf = simplexml_load_file($conf);
  foreach($conf->loggers->logger as $logger) {
   $class = new Reflection_Class("$logger->class");
   if($class->isInstantiable()) {
    $loggers["$logger->id"] = $class->newInstance();
   }
   else {
    fputs(STDERR, "{$logger->class} cannot be instantiated.\n");
    exit;
   }
  }
  foreach($conf->services->service as $service) {
   $class = new Reflection_Class("$service->class");
   if($class->isInstantiable()) {
    $item = $class->newInstance($service->params);
    foreach($service->loggers->logger as $logger) {
     $item->register_logger($loggers["$logger"]);
    }
    $this->services[] = $item;
   }
   else {
    fputs(STDERR, "{$service->class} is not instantiable.\n");
    exit;
   }
  }
 }
 private function next_attempt_sort($a, $b){
  if($a->next_attempt() == $b->next_attempt()) {
   return 0;
  }
  return ($a->next_attempt() < $b->next_attempt())? -1 : 1;
 }
 private function next(){
  usort($this->services,array($this,''next_attempt_sort''));
  return $this->services[0];
 }
 public function loop(){
  declare(ticks=1);
  pcntl_signal(SIGCHLD, array($this, "sig_child"));
  pcntl_signal(SIGUSR1, array($this, "sig_usr1"));
  while(1) {
   $now = time();
   if(count($this->children)< $this->num_children) {
    $service = $this->next();
    if($now < $service->next_attempt()) {
     sleep(1);
     continue;
    }
    $service->set_next_attempt();
    if($pid = pcntl_fork()) {
     $this->children[$pid] = $service;
    }
    else {
     pcntl_alarm($service->timeout());
     exit($service->run());
    }
   }
  }
 }
 public function log_current_status(){
  foreach($this->services as $service) {
   $service->log_current_status();
  }
 }
 private function sig_child($signal){
  $status = ServiceCheck::FAILURE;
  pcntl_signal(SIGCHLD, array($this, "sig_child"));
  while(($pid = pcntl_wait($status, WNOHANG)) > 0){
   $service = $this->children[$pid];
   unset($this->children[$pid]);
   if(pcntl_wifexited($status) && pcntl_wexitstatus($status) ==ServiceCheck::SUCCESS)
   {
    $status = ServiceCheck::SUCCESS;
   }
   $service->post_run($status);
  }
 }
 private function sig_usr1($signal){
  pcntl_signal(SIGUSR1, array($this, "sig_usr1"));
  $this->log_current_status();
 }
}

  这是一个很复杂的类。其构造器读取并分析一个XML文件,创建所有的将被监视的服务,并创建记录它们的日志程序。

  loop()方法是该类中的主要方法。它设置请求的信号处理器并检查是否能够创建一个新的子进程。现在,如果下一个事件(以next_attempt时间CHUO排序)运行良好,那么一个新的进

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号