12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace KIF\Core;
- use KIF\Cli\SingleProcess;
- abstract class AbstractDaemon extends Controller {
-
- public function run() {
- $action = $this->action;
- $this->$action();
- }
-
-
- abstract protected function doSomething();
-
-
- protected $aliveTime = 7200;
-
-
-
- private $objSingleProcess;
-
- public function __construct() {
- $processId = realpath(__FILE__) . '-' . get_class($this);
-
- $timeout = 2 * $this->aliveTime;
- $this->objSingleProcess = new SingleProcess($processId, $timeout, true);
-
- }
-
-
- protected function isAlive() {
- static $time_init = null;
- if (is_null($time_init)) {
- $time_init = time();
- }
-
- $time_now = time();
-
- if ($time_now - $time_init >= $this->aliveTime) {
- return false;
- }
-
- return true;
- }
-
-
- protected function doDefault() {
- if ($this->objSingleProcess->isRun()) {
- echo "已经有进程在跑了\r\n";
- exit();
- }
- $this->objSingleProcess->run();
-
- do {
- if (!$this->isAlive()) {
- break;
- }
-
- $this->doSomething();
-
-
-
- usleep(1000000);
- echo "休息...\r\n";
- } while ( true );
-
- $this->objSingleProcess->complete();
- echo "done\r\n";
- exit ();
- }
-
- }
|