Diffs
Holo_Daemon/trunk/Holo/Daemon/Daemon.php
@@ -1,122 +0,0 @@
-<?php
-
-
-// example:
-// Holo_Daemon();
-class Holo_Daemon
-{
- const PERFORM = 'perform';
- private $thread_num = 1;
- private $handlers = array();
- private $pid_file = '/var/run/';
-
- static public function daemonize()
- {
- declare(ticks = 1);
- ini_set("max_execution_time", "0");
- ini_set("max_input_time", "0");
- set_time_limit(0);
-
- $pid = pcntl_fork();
- if($pid) {
- exit();
- }
-
- posix_setsid();
-
- $pid = pcntl_fork();
- if($pid) {
- exit();
- }
-
- chdir('/');
- umask(0);
- fclose(STDOUT);
- fclose(STDIN);
- fclose(STDERROR);
- }
-
- public function run()
- {
- self::daemonize();
- pcntl_signal(SIGTERM, array($this, 'signal_handler'));
- pcntl_signal(SIGHUP, array($this, 'signal_handler'));
- while (true) {
- $this->handler(self::PERFORM);
- }
- }
-
-
- private function __constractor($params)
- {
- $this->thread_num = $this->ifisset($params['thread_num'], 1);
- $this->pid_file = $tiss->ifisset($params['pid_file'], false);
- $this->log_file = $tiss->ifisset($params['log_file'], false);
- }
-
- private ifisset(&$value, $default)
- {
- return isset($value) ? $value : $default;
- }
-
-
- //todo removeHandler or clearHanler
- public function addHandler($key, $function)
- {
- if (isset($this->handlers[$key]) === false) {
- $this->handlers[$key] = array();
- }
- $this->handlers[$key][] = $function;
- }
-
- private function signal_handler($signo)
- {
- $this->handler($signo);
- if ($signo === SIGTERM) {
- exit;
- }
-
- }
-
- private function handler($handler)
- {
- if (isset($this->handlers[$handler]) === true ) {
- foreach ($this->handlers[$handler] as $function) {
- $function($this);
- }
- }
- }
-
-
- private function create_pidfile() {
- if (! $this->pid_file) {
- return false;
- }
-
- if (! @file_put_contents($thia->pid_file, posix_getsid())) {
- return false;
- }
- return true;
- }
-
-
- private function remove_pidfile()
- {
- if (! $this->pid_file) {
- return false;
- }
-
- if (file_exists($this->pid_file)) {
- return false;
- }
-
- unlink($this->pid_file);
- return true;
- }
-
- public loger($type, $message)
- {
-
-
- }
-}
Holo_Daemon/trunk/Holo/Daemon.php
@@ -0,0 +1,117 @@
+<?php
+
+
+// example:
+// Holo_Daemon();
+class Holo_Daemon
+{
+ const PERFORM = 'perform';
+
+ private $handlers = array();
+ private $pid_file = '/var/run/holo_daemon.pid';
+
+ static public function daemonize()
+ {
+ declare(ticks = 1);
+ ini_set("max_execution_time", "0");
+ ini_set("max_input_time", "0");
+ set_time_limit(0);
+
+ $pid = pcntl_fork();
+ if($pid) {
+ exit();
+ }
+
+ posix_setsid();
+
+ $pid = pcntl_fork();
+ if($pid) {
+ exit();
+ }
+
+ chdir('/');
+ umask(0);
+ //fclose(STDOUT);
+ //fclose(STDIN);
+ //fclose(STDERR);
+ }
+
+ public function start()
+ {
+ self::daemonize();
+ $this->create_pidfile();
+ pcntl_signal(SIGTERM, array($this, 'signal_handler'));
+ pcntl_signal(SIGHUP, array($this, 'signal_handler'));
+ $this->handler(self::PERFORM);
+ $this->remove_pidfile();
+ }
+
+ public function stop()
+ {
+ $pid = $this->read_pidfile();
+ if ($pid !== false) {
+ posix_kill($pid, SIGTERM);
+ }
+ }
+
+ public function __construct($params)
+ {
+ $this->pid_file = $this->ifisset($params['pid_file'], false);
+ }
+
+ private function ifisset(&$value, $default)
+ {
+ return isset($value) ? $value : $default;
+ }
+
+ public function setHandler($key, $function)
+ {
+ $this->handlers[$key] = $function;
+ }
+
+ protected function signal_handler($signo)
+ {
+ var_dump($signo);
+ $this->handler($signo);
+ if ($signo === SIGTERM) {
+ $this->remove_pidfile();
+ exit;
+ }
+ }
+
+ private function handler($handler)
+ {
+ if (isset($this->handlers[$handler]) === true ) {
+ call_user_func($this->handlers[$handler]);
+ }
+ }
+
+
+ private function create_pidfile() {
+ if (! @file_put_contents($this->pid_file, posix_getpid())) {
+ return false;
+ }
+ return true;
+ }
+
+
+ private function remove_pidfile()
+ {
+ if (! file_exists($this->pid_file)) {
+ return false;
+ }
+
+ unlink($this->pid_file);
+ return true;
+ }
+
+
+ private function read_pidfile()
+ {
+ if (! file_exists($this->pid_file)) {
+ return false;
+ }
+
+ return file_get_contents($this->pid_file);
+ }
+}
Holo_Daemon/trunk/sample/simple.php
@@ -0,0 +1,22 @@
+<?php
+require dirname(__FILE__) . '/../Holo/Daemon.php';
+$pid_file = dirname(__FILE__) . '/sample.pid';
+
+$daemon = new Holo_Daemon(array('pid_file' => $pid_file));
+$daemon->setHandler(Holo_Daemon::PERFORM, 'sample');
+function sample() {
+ $log_file = dirname(__FILE__) . '/log.data';
+ for ($i = 0; $i < 100; $i++) {
+ error_log(sprintf("count:%s\n", $i), 3, $log_file);
+ sleep(1);
+ }
+}
+$cmd = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
+switch ($cmd) {
+ case 'stop':
+ $daemon->stop();
+ break;
+ case 'start':
+ $daemon->start();
+ break;
+}
属性に変更があったパス: Holo_Daemon/trunk/sample/simple.php
___________________________________________________________________
名前: svn:executable
+ *