the Daemon class implemented in PHP. You can implement a queue on a server or a scheduled task that is out of crontab. when used, it inherits from this class and overrides the _dotask method, which is executed through main initialization.
-
- Class Daemon {
- Const DLOG_TO_CONSOLE = 1;
- Const DLOG_NOTICE = 2;
- Const DLOG_WARNING = 4;
- Const DLOG_ERROR = 8;
- Const DLOG_CRITICAL = 16;
- Const Dapc_path = '/tmp/daemon_apc_keys ';
- /**
- * User ID
- *
- * @var int
- */
- Public $userID = 65534; Nobody
- /**
- * Group ID
- *
- * @var Integer
- */
- Public $groupID = 65533; Nobody
- /**
- * Terminate Daemon When set identity failure?
- *
- * @var BOOL
- * @since 1.0.3
- */
- Public $requireSetIdentity = false;
- /**
- * Path to PID file
- *
- * @var String
- * @since 1.0.1
- */
- Public $pidFileLocation = '/tmp/daemon.pid ';
- /**
- * Processlocation
- * Process Information Record directory
- *
- * @var String
- */
- Public $processLocation = ';
- /**
- * Processheartlocation
- * Process Heartbeat Package file
- *
- * @var String
- */
- Public $processHeartLocation = ';
- /**
- * Home Path
- *
- * @var String
- * @since 1.0
- */
- Public $homePath = '/';
- /**
- * Current Process ID
- *
- * @var int
- * @since 1.0
- */
- protected $_pid = 0;
- /**
- * Is this process a children
- *
- * @var Boolean
- * @since 1.0
- */
- protected $_ischildren = false;
- /**
- * Is daemon running
- *
- * @var Boolean
- * @since 1.0
- */
- protected $_isrunning = false;
- /**
- * Constructor
- *
- * @return void
- */
- Public Function __construct () {
- error_reporting (0);
- Set_time_limit (0);
- Ob_implicit_flush ();
- Register_shutdown_function (Array (& $this, ' Releasedaemon '));
- }
- /**
- * START Process
- *
- * @return BOOL
- */
- Public Function main () {
- $this->_logmessage (' starting daemon ');
- if (! $this->_daemonize ()) {
- $this->_logmessage (' Could not start daemon ', self::D log_error);
- return false;
- }
- $this->_logmessage (' Running ... ');
- $this->_isrunning = true;
- while ($this->_isrunning) {
- $this->_dotask ();
- }
- return true;
- }
- /**
- * Stop Process
- *
- * @return void
- */
- Public Function Stop () {
- $this->_logmessage (' stoping daemon ');
- $this->_isrunning = false;
- }
- /**
- * Do task
- *
- * @return void
- */
- protected function _dotask () {
- Override this method
- }
- /**
- * _logmessage
- * Record Log
- *
- * @param string message
- * @param integer level
- * @return void
- */
- protected function _logmessage ($msg, $level = self::D log_notice) {
- Override this method
- }
- /**
- * Daemonize
- *
- * Several rules or characteristics that most daemons possess:
- * 1) Check is daemon already running
- * 2) Fork child process
- * 3) Sets identity
- * 4) Make current process a session Laeder
- * 5) Write process ID to file
- * 6) Change Home path
- * 7) umask (0)
- *
- * @access Private
- * @since 1.0
- * @return void
- */
- Private Function _daemonize () {
- Ob_end_flush ();
- if ($this->_isdaemonrunning ()) {
- Deamon is already running. Exiting
- return false;
- }
- if (! $this->_fork ()) {
- Coudn ' t fork. Exiting.
- return false;
- }
- if (! $this->_setidentity () && $this->requiresetidentity) {
- Required identity set failed. Exiting
- return false;
- }
- if (!posix_setsid ()) {
- $this->_logmessage (' Could not make the current process a session leader ', self::D log_error);
- return false;
- }
- if (! $fp = fopen ($this->pidfilelocation, ' W ')) {
- $this->_logmessage (' Could not write-to-PID file ', self::D log_error);
- return false;
- } else {
- Fputs ($fp, $this->_pid);
- Fclose ($FP);
- }
- Writing to the monitoring log
- $this->writeprocess ();
- ChDir ($this->homepath);
- Umask (0);
- DECLARE (ticks = 1);
- Pcntl_signal (SIGCHLD, Array (& $this, ' Sighandler '));
- Pcntl_signal (SIGTERM, Array (& $this, ' Sighandler '));
- Pcntl_signal (SIGUSR1, Array (& $this, ' Sighandler '));
- Pcntl_signal (SIGUSR2, Array (& $this, ' Sighandler '));
- return true;
- }
- /**
- * Cheks is daemon already running
- *
- * @return BOOL
- */
- Private Function _isdaemonrunning () {
- $oldPid = file_get_contents ($this->pidfilelocation);
- if ($oldPid!== false && Posix_kill (Trim ($oldPid), 0))
- {
- $this->_logmessage (' Daemon already running with PID: '. $oldPid, (self::D log_to_console | self::D log_error));
- return true;
- }
- Else
- {
- return false;
- }
- }
- /**
- * Forks Process
- *
- * @return BOOL
- */
- Private Function _fork () {
- $this->_logmessage (' forking ... ');
- $pid = Pcntl_fork ();
- if ($pid = =-1) {
- Error
- $this->_logmessage (' Could not fork ', self::D log_error);
- return false;
- } elseif ($pid) {
- Parent process
- $this->_logmessage (' Killing parent ');
- Exit ();
- } else {
- Sub-process of fork
- $this->_ischildren = true;
- $this->_pid = Posix_getpid ();
- return true;
- }
- }
- /**
- * Sets identity of a daemon and returns result
- *
- * @return BOOL
- */
- Private Function _setidentity () {
- if (!posix_setgid ($this->groupid) | | |!posix_setuid ($this->userid))
- {
- $this->_logmessage (' Could not set identity ', self::D log_warning);
- return false;
- }
- Else
- {
- return true;
- }
- }
- /**
- * Signals Handler
- *
- * @access Public
- * @since 1.0
- * @return void
- */
- Public Function Sighandler ($sigNo) {
- Switch ($sigNo)
- {
- Case SIGTERM://Shutdown
- $this->_logmessage (' Shutdown signal ');
- Exit ();
- Break
- Case SIGCHLD://Halt
- $this->_logmessage (' Halt signal ');
- while (Pcntl_waitpid ( -1, $status, Wnohang) > 0);
- Break
- Case SIGUSR1://user-defined
- $this->_logmessage (' user-defined signal 1 ');
- $this->_sighandleruser1 ();
- Break
- Case SIGUSR2://user-defined
- $this->_logmessage (' user-defined signal 2 ');
- $this->_sighandleruser2 ();
- Break
- }
- }
- /**
- * Signals HANDLER:USR1
- * Primarily used to periodically clean up DNS parsing records of cached domain names in each process
- *
- * @return void
- */
- protected function _sighandleruser1 () {
- Apc_clear_cache (' user ');
- }
- /**
- * Signals HANDLER:USR2
- * For writing heartbeat package files
- *
- * @return void
- */
- protected function _sighandleruser2 () {
- $this->_initprocesslocation ();
- File_put_contents ($this->processheartlocation, Time ());
- return true;
- }
- /**
- * Releases daemon pid file
- * This method was called on exit (destructor like)
- *
- * @return void
- */
- Public Function Releasedaemon () {
- if ($this->_ischildren && is_file ($this->pidfilelocation)) {
- $this->_logmessage (' releasing Daemon ');
- Unlink ($this->pidfilelocation);
- }
- }
- /**
- * Writeprocess
- * Write the current process information to the monitoring log, another script will scan the monitoring log data send signal, if not responding, restart the process
- *
- * @return void
- */
- Public Function writeprocess () {
- Initialize proc
- $this->_initprocesslocation ();
- $command = Trim (implode (' ', $_server[' argv '));
- Specify the directory for the process
- $processDir = $this->processlocation. '/' . $this->_pid;
- $processCmdFile = $processDir. '/cmd ';
- $processPwdFile = $processDir. '/pwd ';
- directory where all processes are located
- if (!is_dir ($this->processlocation)) {
- mkdir ($this->processlocation, 0777);
- chmod ($processDir, 0777);
- }
- Querying for Duplicate process records
- $pDirObject = Dir ($this->processlocation);
- while ($pDirObject && ($pid = $pDirObject->read ())!== false) {
- if ($pid = = '. ' | | $pid = = ' ... ' | | intval ($PID)! = $pid) {
- Continue
- }
- $pDir = $this->processlocation. '/' . $pid;
- $pCmdFile = $pDir. '/cmd ';
- $pPwdFile = $pDir. '/pwd ';
- $pHeartFile = $pDir. '/heart ';
- Process that initiates the same parameters according to the cmd check
- if (Is_file ($pCmdFile) && trim (file_get_contents ($pCmdFile)) = = $command) {
- Unlink ($pCmdFile);
- Unlink ($pPwdFile);
- Unlink ($pHeartFile);
- Delete Directory has cache
- Usleep (1000);
- RmDir ($pDir);
- }
- }
- New Process Directory
- if (!is_dir ($processDir)) {
- mkdir ($processDir, 0777);
- chmod ($processDir, 0777);
- }
- Write Command Arguments
- File_put_contents ($processCmdFile, $command);
- File_put_contents ($processPwdFile, $_server[' PWD ');
- Write file has cache
- Usleep (1000);
- return true;
- }
- /**
- * _initprocesslocation
- * Initialization
- *
- * @return void
- */
- protected function _initprocesslocation () {
- $this->processlocation = Root_path. '/app/data/proc ';
- $this->processheartlocation = $this->processlocation. '/' . $this->_pid. '/heart ';
- }
- }
Copy Code |