PHP構建監視服務的方法是什么

    時間:2024-09-25 07:03:56 PHP 我要投稿
    • 相關推薦

    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。

    【PHP構建監視服務的方法是什么】相關文章:

    如何使用PHP構建一個高性能的彈幕后端服務01-22

    PHP錯誤類型及屏蔽方法08-22

    PHP處理密碼的幾種方法10-17

    十天學會php的方法09-23

    MongoDB的PHP驅動方法與技巧大全09-08

    了解PHP環境搭建與配置的方法08-03

    PHP開發環境安裝的方法有哪些10-27

    php在新浪云中使用imagick的方法07-14

    構建生產結構優化系統的方法07-31

    構建企業內訓體系的方法12-30

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      日韩亚洲一区中文字幕 | 最新日韩精品中文字幕 | 久久国产伦三级理电影 | 日本羞羞的视频在线播放 | 亚洲中文色欧另类欧美动图 | 亚洲乱码精品一区二区不卡 |