summaryrefslogtreecommitdiff
path: root/includes/daemonize/daemonize_lib.php
blob: 6880c43def58b7fd3d793e436109b70a33731915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

function daemonize_init( $pPidfile ) {
	global $gDaemonTouchTimer;
	// Log our PID as the first thing, so other processes don't try to kill us.
	echo "DAEMON ".getmypid()." : writing pidfile $pPidfile\n";
	if( !$fp = fopen($pPidfile, "w") ) {
		die( "could not open pid file: $pPidfile\n\n" );
	}
	fwrite($fp, posix_getpid()."\n".time()."\n");
	fclose($fp);
	$gDaemonTouchTimer = 0; // Timer so we only touch the pidfile sometimes. 
}

function daemonize_refresh( $pPidfile ) {
	global $gDaemonTouchTimer;
	static $ticks = 0;
	if ($gDaemonTouchTimer + 15 < time()) { // So the revivifier can tell we've hung.
		if( ($ticks % 600) == 0 ) {
			// keep the noise down and only log a message once per minute
			if( !defined( 'IS_LIVE' ) ) {
				echo date( 'd/M/Y:H:i:s O' )." - DAEMON ".getmypid()." : Touching the file $pPidfile to ".time()."\n";
			}
		}
		if( $fp = fopen($pPidfile, "w") ) {
			fwrite($fp, posix_getpid()."\n".time()."\n");
			fclose($fp);
			$gDaemonTouchTimer = time();
		} else {
			die( "could not touch pid file: $pPidfile\n\n" );
		}
		$ticks++;
	} 
}