<?php
/*=======================================================================
 *
 * autoranking_keepalive.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");


// $path = getParameterValue("0", "PATH_DOCROOT", "0");
$dirName = dirname(__FILE__);
$dirName = str_replace("\\", "/", $dirName);
$lastSlashPos = strrpos($dirName,"/");
$path = substr($dirName, 0, $lastSlashPos);

$isCronJob = FALSE;                           // Switch for cron-job
$loopFlag = TRUE;                             // Flag for endless flag
$logFile = $path . "/log/autoranking.log";    // Logfile


function call_autoranking_main() {
  global $logFile, $path;

  $result = "";    
  // $cmd = $_SERVER['DOCUMENT_ROOT'] . " /phoenix/include/autoranking_main.php >" . $_SERVER['DOCUMENT_ROOT'] . "/phoenix/log/autoranking.stdout+err 2>&1 &";
  $cmd = "php " . $path . "/include/autoranking_main.php >" . $path . "/log/autoranking.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart autoranking process [Time: " . $currentTime . "]");
}

function call_autoranking() {
  global $logFile, $path;

  $result = "";    
  // $cmd = $_SERVER['DOCUMENT_ROOT'] . " /phoenix/include/autoranking.php >" . $_SERVER['DOCUMENT_ROOT'] . "/phoenix/log/autoranking.stdout+err 2>&1 &";
  $cmd = "php " . $path . "/include/autoranking.php >" . $path . "/log/autoranking.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart autoranking process [Time: " . $currentTime . "]");
}

function call_standing_orders() {
  global $logFile, $path;

  $result = "";    
  $cmd = "php " . $path . "/jobs/check_jb_permanent.php >>" . $path . "/log/check_jb_permanent.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart check_jb_permanent process [Time: " . $currentTime . "]");
}

function call_automailer() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer_main.php >>" . $path . "/log/automailer.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart automailer process [Time: " . $currentTime . "]");
}

function call_automailer7() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer7_main.php >>" . $path . "/log/automailer7.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart automailer7 process [Time: " . $currentTime . "]");
}

function call_DPF() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/ckeck_DPF_main.php >>" . $path . "/log/dpf.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart check_DPF_main process [Time: " . $currentTime . "]");
}

function call_vhtDispoDays() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/cron_vht_dispo_days.php >>" . $path . "/log/vhtDispoDays.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Executed vht_dispo_days process [Time: " . $currentTime . "]");
}

function call_serviceAcceptanceProtocol() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/cron_service_acceptance_protocol.php >>" . $path . "/log/acceptance_protocol.stdout+err 2>&1 &";

  $ausgabe = system($cmd, $result);

  // Log restart
  $currentTime = getDateTime("0");
  writeToFile($logFile, "WATCHDOG: Restart service_acceptance_protocol process [Time: " . $currentTime . "]");
}

// Execution-Time for script
if (!$isCronJob) :
   set_time_limit(0);
endif;


// Endless loop if no cron-job
while ($loopFlag):

  // Set loop flag
  if ($isCronJob) : $loopFlag = FALSE; endif;

  // Get last execution time of the autoranking script
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","autoranking","ka_lastexecutiontime");

  // Get timestamps
  // ("checkTime" = NOW() minus 2 minutes)
  $currentTime = getDateTime("0");
  $checkTime = getDateTime("datetime_plus_offset", array(0,-1,0,0,0,0), $formatStr = "Y-m-d H:i:s");

  // Watchdog execution
  writeToFile($logFile, "Watchdog executed: " . $currentTime);

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     // call_autoranking();
     call_autoranking_main();
  endif;

  // *********************************************************************************************************************

  // Check check_jb_permanent being alive
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","standing_orders","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 30 minutes)
  $checkTime = getDateTime("datetime_plus_offset", array(0,-30,0,0,0,0), $formatStr = "Y-m-d H:i:s");
  if ($checkTime > $lastExecutionTime):     
     call_standing_orders();
  endif;

  // *********************************************************************************************************************

  // Get last execution time of the automailer script
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","automailer","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 2 minutes)
  $checkTime = getDateTime("datetime_plus_offset", array(0,-2,0,0,0,0), $formatStr = "Y-m-d H:i:s");

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     call_automailer();
  endif;

  // *********************************************************************************************************************

  // Get last execution time of the automailer script
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","automailer7","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 2 minutes)
  $checkTime = getDateTime("datetime_plus_offset", array(0,-2,0,0,0,0), $formatStr = "Y-m-d H:i:s");

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     call_automailer7();
  endif;

  // *********************************************************************************************************************

  // Get last execution time of the dpf script
  /*
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","dpf","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 30 minutes)
  $checkTime = getDateTime("datetime_plus_offset", array(0,-30,0,0,0,0), $formatStr = "Y-m-d H:i:s");

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     call_DPF();
  endif;
  */

  // *********************************************************************************************************************

  // Get last execution time of the vhtDispoDays script
  /*
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","vhtDispoDays","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 7 days)
  $checkTime = getDateTime("datetime_plus_offset", array(0,0,0,0,-1,0), $formatStr = "Y-m-d H:i:s");

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     call_vhtDispoDays();
  endif;
  */

  // *********************************************************************************************************************

  // Get last execution time of the service acceptance protocol script
  /*
  $lastExecutionTime = getFieldValueFromId("keepalive","ka_process","serviceAcceptanceProtocol","ka_lastexecutiontime");

  // ("checkTime" = NOW() minus 30 minutes)
  $checkTime = getDateTime("datetime_plus_offset", array(0,-1,0,0,0,0), $formatStr = "Y-m-d H:i:s");

  // Check autoranking being alive
  if ($checkTime > $lastExecutionTime) :
     call_serviceAcceptanceProtocol();
  endif;
  */


  if (!$isCronJob) :
     sleep(60);
  endif;

endwhile;  // Endless loop
?>
