/**
  * Function used to update the DB to show the current run status of the cron
  * Used for the CRON Manager UI for updating status fields
  * @param $cron array The cron for which the runstate needs to be set
  * @param $state int If 0, then cron is inactive, if 1 it is active and running
  * @param $dbh object The database object
  */
 private static function setRunState($cron, $state = 0, $dbh)
 {
     $return = false;
     $q = 'UPDATE admin_crons SET run_state = ' . $state . ' WHERE cron_id = ' . $cron . ';';
     try {
         // Set / Unset a lock
         switch ($state) {
             case 0:
                 $dbh->runQuery("SELECT RELEASE_LOCK('cron_" . $cron . "');");
                 $result = $dbh->runQuery($q);
                 $return = true;
                 break;
             case 1:
                 // First check if the lock is set
                 $lock = $dbh->getResults("SELECT IS_USED_LOCK('cron_" . $cron . "') as locked;");
                 if (GenConfig::DEBUG) {
                     OutputHandler::displayOutput("[%white%DEBUG%lightgray%] : Does lock cron_{$cron} exist? \n" . print_r($lock, true) . "\n");
                 }
                 // The lock exists
                 if (!empty($lock[0]["locked"]) && !is_null($lock[0]["locked"])) {
                     $result = 0;
                     $return = false;
                 } else {
                     $dbh->runQuery("SELECT GET_LOCK('cron_" . $cron . "', 10);");
                     $result = $dbh->runQuery($q);
                     $return = true;
                 }
                 break;
         }
     } catch (PDOException $e) {
         $result = 1;
         $return = false;
     }
     # Failed
     if ($result == 1) {
         if (GenConfig::DEBUG) {
             OutputHandler::displayOutput("%lightred%Could not update the run state of cronjob %red%{$cron_id}%lightred%! :(%lightgray%\n--------------------\n");
         }
         SR_Agent::Log(GenConfig::API, SystemReporter::MSG_ERROR, "Could not update the run state of cronjob {$cron_id}! :(");
     }
     return $return;
 }
 /**
  * Private Fucntion completeRun
  * @description
  * Function is called after all the databases have been backed up, in order to create a final MD5 checksum of the databases and then compress it all as a single file
  */
 private function completeRun()
 {
     // Check if it is the LIVE environment
     if (LIVE === true) {
         $backupDir = "/ts_backups/mysql_backups/";
     } else {
         $backupDir = "/srv_admin/srv_backups/mysql/";
     }
     // Check if it was a full or incremental backup run
     if (date("D") == "Fri") {
         $backupDir .= "full_backups/";
     } else {
         $backupDir .= "incremental_backups/";
     }
     // Setup paths and filenames
     $folder = $backupDir;
     $backupDir .= date("d_m_Y") . "/";
     $compressed_file = $folder . date("d_m_Y") . ".tar.gz";
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec("md5sum {$backupDir}/*.tar.gz > {$backupDir}/databases.md5");
         if (DEBUG === true) {
             OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] Generated MD5 Hashes of DB Backups%lightgray%", $this->color);
         }
     }
     $command = "tar -zcvf {$compressed_file} {$backupDir}";
     if (DEBUG === true) {
         OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] Command : %lightblue%{$command}%lightgray%", $this->color);
     }
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec($command, $command_output);
         // Generate MD5 checksum of the compressed file (usefull for integrity checks)
         exec("md5sum {$compressed_file} > {$compressed_file}.md5");
     } else {
         $command_output = array("Folder compressed");
     }
     // Check if debugging is enabled
     if (DEBUG === true) {
         // Display debugging information
         foreach ($command_output as $cmd_output) {
             OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] [%lightblue%{$backupDir}%lightgray%]\t" . $cmd_output, $this->color);
         }
     }
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec("rm -rf {$backupDir}");
     }
     SR_Agent::Log(APPID, SystemReporter::MSG_SUCCESS, "Backup complete for " . date("d/m/Y"));
 }