Skip to content

Commit

Permalink
add: list of watch files will be checked and process restarted if any…
Browse files Browse the repository at this point in the history
… change
  • Loading branch information
blacknell committed Nov 12, 2020
1 parent 55671f6 commit f7b522a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
9 changes: 5 additions & 4 deletions example/wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
$wrapper = new Blacknell\Watchdog\Watchdog($log);

$wrapper->watch(
'/bin/ls', // replace this with the command that starts your long lived process
"mygrepstring", // replace this with a script that will be grep'd to see if it is still running
'/tmp/mywatchdog.watchdog', // your script should touch this file regularly
15); // and this is how regularly (worst case) in seconds
'/bin/ls', // replace this with the command that starts your long lived process
"mygrepstring", // replace this with a script that will be grep'd to see if it is still running
'/tmp/mywatchdog.watchdog', // your script should touch this file regularly
15, // and this is how regularly (worst case) in seconds
['/home/pi/myscript.php']); // changing any of these files will force a restart
50 changes: 39 additions & 11 deletions src/Watchdog.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,68 @@ function __destruct()

/**
* @param $watchScript full command to re-start script
* @param $watchScriptGrep grep'able string for the script we're watching
* @param $watchScriptGrep grep'able string for the script we're watching
* @param $watchdogFile the file that the script keeps touching
* @param $watchdogMaxAge the interval in seconds at which it should always be touched
* @param $watchfiles list of filenames to be checked for changes since process started
*
* @throws \Moment\MomentException
*/
public function watch($watchScript, $watchScriptGrep, $watchdogFile, $watchdogMaxAge)
public function watch($watchScript, $watchScriptGrep, $watchdogFile, $watchdogMaxAge, $watchfiles = [])
{
@assert(is_string($watchScript));
@assert(is_string($watchScriptGrep));
@assert(is_string($watchdogFile));
@assert(is_int($watchdogMaxAge));
@assert($watchdogMaxAge > 0);
@assert(is_array($watchfiles));

$watchdogDead = false;

$hostName = gethostname();
$ipAddress = gethostbyname($hostName);
$hostName = gethostname();
$ipAddress = gethostbyname($hostName);

if (!@filemtime($watchdogFile)) {
$watchdogDead = true;
$this->logger->notice(sprintf("Watchdog file %s does not exist" . PHP_EOL, $watchdogFile), [$hostName, $ipAddress]);
$this->logger->notice(sprintf("Watchdog file %s does not exist" . PHP_EOL, $watchdogFile), [$hostName, $ipAddress]);
} else {
$modifiedTime = new Moment();
$modifiedTime->setTimestamp(filemtime($watchdogFile));
$fileModificationTime = new Moment();
$fileModificationTime->setTimestamp(filemtime($watchdogFile));
$now = new Moment();
$this->logger->debug(sprintf("Watchdog file last touched %s, %s", $modifiedTime->format(Watchdog::LOG_DATE_FORMAT), $modifiedTime->fromNow()->getRelative()));
$this->logger->debug(sprintf("Watchdog file last touched %s, %s", $fileModificationTime->format(Watchdog::LOG_DATE_FORMAT), $fileModificationTime->fromNow()->getRelative()));

if (($now->getTimestamp() - $modifiedTime->getTimestamp()) > $watchdogMaxAge) {
$this->logger->notice(sprintf("Watchdog file %s is more than %u seconds old. Last touched %s, %s", $watchdogFile, $watchdogMaxAge, $modifiedTime->format(Watchdog::LOG_DATE_FORMAT), $modifiedTime->fromNow()->getRelative()), [$hostName, $ipAddress]);
if (($now->getTimestamp() - $fileModificationTime->getTimestamp()) > $watchdogMaxAge) {
$this->logger->notice(sprintf("Watchdog file %s is more than %u seconds old. Last touched %s, %s", $watchdogFile, $watchdogMaxAge, $fileModificationTime->format(Watchdog::LOG_DATE_FORMAT), $fileModificationTime->fromNow()->getRelative()), [$hostName, $ipAddress]);
$watchdogDead = true;
}
}

if ($watchdogDead) {
$watchFilesHaveChanged = false;
if (count($watchfiles) > 0) {
$this->logger->debug("Checking for changes to files since process was started", $watchfiles);
$processes = array();
exec(sprintf('ps -eo lstart,cmd|grep %s|grep -v grep', $watchScriptGrep), $processes);
// for each matching process find it's start time
// and compare to the files we've been asked to check against
foreach ($processes as $process) {
$processStartTime = new Moment(substr($process, 0, 24));
foreach ($watchfiles as $watchfile) {
$fileModificationTime = new Moment();
$fileModificationTime->setTimestamp(filemtime($watchfile));
if (@filemtime($watchfile)) {
$fileModificationTime = new Moment();
$fileModificationTime->setTimestamp(filemtime($watchfile));
if ($fileModificationTime->isAfter($processStartTime)) {
$this->logger->notice(sprintf("Watch file %s changed %s since process started %s", $watchfile, $fileModificationTime->format(Watchdog::LOG_DATE_FORMAT), $processStartTime->format(Watchdog::LOG_DATE_FORMAT)), [$hostName, $ipAddress]);
$watchFilesHaveChanged = true;
break 2;
}
}
}
}
}

if ($watchdogDead || $watchFilesHaveChanged) {

// first find processes and ask nicely
$processes = array();
Expand Down

0 comments on commit f7b522a

Please sign in to comment.