PHP构建监视服务的方法是什么
【提要】本篇《PHP构建监视服务的方法》特别为需要方法编程学习的朋友收集整理的,仅供参考。内容如下:
PHP监视服务记录程序应该能够支持任意的服务检查(例如,HTTP和FTP服务)并且能够以任意方式(通过电子邮件,输出到一个日志文件,等等)记录事件。你当然想让它以一个守护程序方式运行;所以,你应该请求它输出其完整的当前状态。以下是小编为大家搜索整理的PHP构建监视服务的方法,希望能给大家带来帮助!
一个服务需要实现下列抽象类:
abstract class ServiceCheck {
const FAILURE = 0;
const SUCCESS = 1;
protected $timeout = 30;
protected $next_attempt;
protected $current_status = ServiceCheck::SUCCESS;
protected $previous_status = ServiceCheck::SUCCESS;
protected $frequency = 30;
protected $description;
protected $consecutive_failures = 0;
protected $status_time;
protected $failure_time;
protected $loggers = array();
abstract public function __construct($params);
public function __call($name, $args)
{
if(isset($this->$name)) {
return $this->$name;
}
}
public function set_next_attempt()
{
$this->next_attempt = time() + $this->frequency;
}
public abstract function run();
public function post_run($status)
{
if($status !== $this->current_status) {
$this->previous_status = $this->current_status;
}
if($status === self::FAILURE) {
if( $this->current_status === self::FAILURE ) {
$this->consecutive_failures++;
}
else {
$this->failure_time = time();
}
}
else {
$this->consecutive_failures = 0;
}
$this->status_time = time();
$this->current_status = $status;
$this->log_service_event();
}
public function log_current_status()
{
foreach($this->loggers as $logger) {
$logger->log_current_status($this);
}
}
private function log_service_event()
{
foreach($this->loggers as $logger) {
$logger->log_service_event($this);
}
}
public function register_logger(ServiceLogger $logger)
{
$this->loggers[] = $logger;
}
}
上面的__call()重载方法提供对一个ServiceCheck对象的参数的只读存取操作:
· timeout-在引擎终止检查之前,这一检查能够挂起多长时间。
· next_attempt-下次尝试连接到服务器的时间。
· current_status-服务的当前状态:SUCCESS或FAILURE。
· previous_status-当前状态之前的状态。
· frequency-每隔多长时间检查一次服务。
· description-服务描述。
· consecutive_failures-自从上次成功以来,服务检查连续失
败的`次数。
· status_time-服务被检查的最后时间。
· failure_time-如果状态为FAILED,则它代表发生失败的时间。
这个类还实现了观察者模式,允许ServiceLogger类型的对象注册自身,然后当调用log_current_status()或log_service_event()时调用它。
这里实现的关键函数是run(),它负责定义应该怎样执行检查。如果检查成功,它应该返回SUCCESS;否则返回FAILURE。
-
如何理解Javascript的caller,callee,call,apply区别
在提到上述的概念之前,首先想说说javascript中函数的隐含参数:argumentsarguments该对象代表正在执行的函数和调用它的函数的参数。[function.]arguments[n]参数function:选项。当前正在执行的Function对象的名字。n:选项。要传递给Function对象的从0开始的参数值...
-
javascript 日期类型学习笔记
1.创建日期对象复制代码代码如下:varnow=newDate();//获得当前系统日期和时间varsomeDate=newDate(e("May25,2012"));varsomeDate=newDate("May25,2012");//与上相同varsomeDate=newDate((2010,0));//GMT时间2010年1月1日凌晨0时varsomeDate=newDate(2010,0);/...
-
JavaScript的课堂讲解
本文主要内容:1.分析函数的四种调用形式2.弄清楚函数中this的意义3.明确构造函对象的过程4.学会使用上下文调用函数一、函数调用形式函数调用形式是最常见的形式,也是最好理解的形式。所谓函数形式就是一般声明函数后直接调用即是。例如:复制代码代码如下://声明...
-
css经典教程
css经典教程1有些习惯显得尤为重要,今天与您分享六个CSS习惯。因为习惯其实是会变的,所以只能写“最近”的。这些习惯都跟技术无关,如果不遵守,也不会出错。但是我觉得良好的习惯会体现一个人的素质。我们在中向大家介绍过很多CSS经验与技巧,这些东西都发布在/css/...