<commit_before><?php

// ----- only run from command line -----
if ($argc < 1)
    die;

include_once("../../config.php");
include "../../common/functions.php";        // load base functions file
include "../common/functions.php";           // load capture function file

$dbh = pdo_connect();
create_error_logs($dbh);
//$dbh = null;                                 // https://bugs.php.net/bug.php?id=62065

$roles = unserialize(CAPTUREROLES);

foreach ($roles as $role) {

     if (defined('IDLETIME')) {
          $idletime = IDLETIME;
     } else {
	     $idletime = 600;
     }

	$pid = 0;
	$running = false;
	if (file_exists(BASE_FILE . "proc/$role.procinfo")) {
	    $procfile = file_get_contents(BASE_FILE . "proc/$role.procinfo");

	    $tmp = explode("|", $procfile);
	    $pid = $tmp[0];
	    $last = $tmp[1];

	    // check if script with pid is still runnning
	    exec("ps -p " . $pid . " | wc -l", $out);
	    $out = trim($out[0]);
	    if ($out == 2)
          $running = true;

	    // check whether the process has been idle for too long
	    logit("controller.log", "script $role called - pid:" . $pid . "  idle:" . (time() - $last));
         if ($last < (time() - $idletime)) {

               // record confirmed gap
               gap_record($role, $last, time());

               if ($running) {
          
                   exec("kill -s SIGTERM " . $pid);

                   logit("controller.log", "script $role was idle for more than " . $idletime . " seconds - killing and starting");
                   mail($mail_to,"DMI-TCAT controller killed a process","script $role was idle for more than " . $idletime . " seconds - killing and starting");

                   sleep(2);

                   passthru("php " . BASE_FILE . "capture/stream/$role.php > /dev/null 2>&1 &");
                            
               }
         }
	}
	if (!$running) {

	    logit("controller.log", "script $role was not running - starting");

	    passthru("php " . BASE_FILE . "capture/stream/$role.php > /dev/null 2>&1 &");
	}

}

function logit($file, $message) {

    $file = BASE_FILE . "logs/" . $file;
    $message = date("Y-m-d H:i:s") . " " . $message . "\n";
    file_put_contents($file, $message, FILE_APPEND);
}

?>
<commit_msg>* Restart process using posix_kill, increase sleep.<commit_after><?php

// ----- only run from command line -----
if ($argc < 1)
    die;

include_once("../../config.php");
include "../../common/functions.php";        // load base functions file
include "../common/functions.php";           // load capture function file

$dbh = pdo_connect();
create_error_logs($dbh);
//$dbh = null;                                 // https://bugs.php.net/bug.php?id=62065

$roles = unserialize(CAPTUREROLES);

foreach ($roles as $role) {

     if (defined('IDLETIME')) {
          $idletime = IDLETIME;
     } else {
	     $idletime = 600;
     }

	$pid = 0;
	$running = false;
	if (file_exists(BASE_FILE . "proc/$role.procinfo")) {
	    $procfile = file_get_contents(BASE_FILE . "proc/$role.procinfo");

	    $tmp = explode("|", $procfile);
	    $pid = $tmp[0];
	    $last = $tmp[1];

	    // check if script with pid is still runnning
	    exec("ps -p " . $pid . " | wc -l", $out);
	    $out = trim($out[0]);
	    if ($out == 2)
          $running = true;

	    // check whether the process has been idle for too long
	    logit("controller.log", "script $role called - pid:" . $pid . "  idle:" . (time() - $last));

         if ($last < (time() - $idletime)) {

               // record confirmed gap
               gap_record($role, $last, time());

               if ($running) {
          
                   posix_kill(SIGTERM, $pid);

                   logit("controller.log", "script $role was idle for more than " . $idletime . " seconds - killing and starting");
                   mail($mail_to,"DMI-TCAT controller killed a process","script $role was idle for more than " . $idletime . " seconds - killing and starting");

                   sleep(6);       // we need some time to allow graceful exit

                   passthru("php " . BASE_FILE . "capture/stream/$role.php > /dev/null 2>&1 &");
                            
               }
         }
	}
	if (!$running) {

	    logit("controller.log", "script $role was not running - starting");

	    passthru("php " . BASE_FILE . "capture/stream/$role.php > /dev/null 2>&1 &");
	}

}

function logit($file, $message) {

    $file = BASE_FILE . "logs/" . $file;
    $message = date("Y-m-d H:i:s") . " " . $message . "\n";
    file_put_contents($file, $message, FILE_APPEND);
}

?>

{'fraction_non_alphanumeric': 0.17164016554127642, 'fraction_numerical': 0.008277063820518406, 'mean_word_length': 1.9137055837563453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335448', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1454, 'n_words': 370}