Files
resatennis_tyroltenn/IDrive/.scripts/Job_Termination_Script.php

209 lines
6.9 KiB
PHP
Executable File

<?php
include('fetchPath.php');
define('LOCALSEARCHPATH',"\/Backup\/\.\+searchList");
define('REMOTESEARCHPATH',"\/Restore\/\.\+searchList");
define('TRASHSEARCHPATH',"\/Restore\/\.\+trashList");
define("CHILDPIDS", "childpids.txt");
$operationType = 0;
$arg = "";
$arg2 = "";
if(isset($argv[1]))
$arg = $argv[1];
if(isset($argv[2]))
$arg2 = $argv[2];
$jobName = `pwd`;
$jobName = trim($jobName);
getOperationType($jobName);
$pidPath = $jobName.PATHSEPARATOR.PIDFILE;
$childpidfile = $jobName . PATHSEPARATOR . CHILDPIDS;
$jobTermPath = $jobName.PATHSEPARATOR.UTFFILE;
$processKilled = 0;
if(!file_exists($pidPath)) {
traceLog("$jobName is not running");
return;
}
$jobStatusCmd = constructJobTerminateCommand();
if($jobStatusCmd == ""){
@unlink($pidPath);
@unlink($jobTermPath);
}
else{
if(LOCALSEARCH == $operationType && file_exists($childpidfile)) {
$cpids = file($childpidfile);
foreach($cpids as $cpid) {
$cpid = trim($cpid);
if($cpid != '' && (int)$cpid > 0) {
`kill $cpid`;
}
}
@unlink($childpidfile);
}
$psResult = `$jobStatusCmd`;
$arr = preg_split("/\n/", $psResult);
$len = count($arr);
createUserCancel();
@unlink($pidPath);
@unlink($jobTermPath);
for($i = 0; $i < $len; $i++) {
$arr[$i] = trim($arr[$i]);
if(preg_match("/grep/",$arr[$i]))
continue;
if($operationType == LOCALSEARCH && $machine == QNAP ){
$separator = " ";
$pidIdx = 0;
}
$tmpArr = explode($separator, $arr[$i]);
for($j = $pidIdx; $j < count($tmpArr) ; $j++){
if($tmpArr[$j] != "" && preg_match('/^\d+$/', $tmpArr[$j])){
`kill -9 $tmpArr[$j]`;
$processKilled = 1;
break;
}
}
}
# kill all ls pid's by scanning the remaining ls in "ls pid file"
$lsPIDTasks = glob($jobName . PATHSEPARATOR . 'lsPID_*');
if(($operationType == BACKUP || $operationType == LOCALBACKUP) && $lsPIDTasks) {
foreach($lsPIDTasks as $lsPidTask) {
$lsPID = trim(file_get_contents($lsPidTask));
`kill -9 $lsPID`;
@unlink($lsPidTask);
}
}
if($processKilled)
traceLog("$jobName has been killed");
else
traceLog("Not able to kill $jobName.");
}
removeCutoffCronEntry();
#****************************************************************************************************
# Subroutine Name : getOperationType.
# Objective : Get operation type based on job path.
# Added By : Avinash Kumar
#*****************************************************************************************************/
function getOperationType($jobPath)
{
GLOBAL $localSearch, $remoteSearch, $trashSearch, $backup, $restore, $delete, $archive, $operationType;
if(preg_match("/".LOCALSEARCHPATH."/", $jobPath))
$operationType = LOCALSEARCH;
elseif(preg_match("/".REMOTESEARCHPATH."/", $jobPath))
$operationType = REMOTESEARCH;
elseif(preg_match("/".TRASHSEARCHPATH."/", $jobPath))
$operationType = TRASHSEARCH;
elseif(preg_match("/".BACKUP."/", $jobPath))
$operationType = BACKUP;
elseif(preg_match("/".RESTORE."/", $jobPath))
$operationType = RESTORE;
elseif(preg_match("/".DELETE."/", $jobPath))
$operationType = DELETE;
elseif(preg_match("/".ARCHIVE."/", $jobPath))
$operationType = ARCHIVE;
elseif(preg_match("/".LOCALBACKUP."/", $jobPath))
$operationType = LOCALBACKUP;
}
#****************************************************************************************************
# Subroutine Name : createUserCancel.
# Objective : Generate cancel.txt for backup and restore operations
# Added By : Avinash Kumar
#*****************************************************************************************************/
function createUserCancel()
{
GLOBAL $operationType, $arg, $arg2;
if(BACKUP != $operationType and RESTORE != $operationType)
return;
if(!($CFH = fopen(CANCEL_PATH, "w"))){
traceLog("Could not open file ".CANCEL_PATH.", Reason:$!");
return;
}
if("" != $arg){
if($arg2 != '')
fwrite($CFH, 'Operation could not be completed. Reason : '.$arg2);
else
fwrite($CFH, 'Operation could not be completed. Reason : Operation Cancelled due to Cut off.');
}
else
fwrite($CFH, 'Operation could not be completed, Reason: Operation Cancelled by User.');
fclose($CFH);
@chmod(CANCEL_PATH,PERMISSIONVAL);
}
#****************************************************************************************************
# Subroutine Name : removeCutoffCronEntry.
# Objective : remove cut off entry from crontab
# Added By : Pooja H
#*****************************************************************************************************/
function removeCutoffCronEntry()
{
GLOBAL $arg, $webDir, $userDir, $phpPath, $jobName;
#In case of Job_termination for Start immediately option, cron entry for cut off will be removed
if($arg != "" and $arg == 0){
$temp_file = $userDir.PATHSEPARATOR.TEMP_OPERATION_FILE;
if($TEMP_FILE = fopen($temp_file, "w")){
fwrite($TEMP_FILE, "removeCrontabEntry\n");
fwrite($TEMP_FILE, "\"$jobName\"");
fclose($TEMP_FILE);
$operationsFile = $webDir.PATHSEPARATOR.APPDIR.PATHSEPARATOR.SCRIPTS.PATHSEPARATOR.OPERATIONSFILE." removeCrontabEntry";
exec("$phpPath $operationsFile");
traceLog("Could not execute ".OPERATIONSFILE);
}
else
traceLog("Could not open file $temp_file.Reason : ".customErrorMsg());
}
}
#****************************************************************************************************
# Subroutine Name : constructJobTerminateCommand.
# Objective : To get the job termination command depending on the job type.
# Added By : Pooja H
# Modified By : Sabin Cheruvattil
#*****************************************************************************************************/
function constructJobTerminateCommand()
{
GLOBAL $operationType, $jobTermPath, $jobName, $pidPath, $psPath, $machine;
$jobtype = "";
$jobStatusCmd = "";
if(BACKUP == $operationType or RESTORE == $operationType or REMOTESEARCH == $operationType or ARCHIVE == $operationType or TRASHSEARCH == $operationType){
$jobtype = "BACKUP_RESTORE_REMOTESEARCH_CLEANUP";
$jobTermPath = str_replace("'","'\\''",$jobTermPath);
$jobTermPath = "'".$jobTermPath."'";
$jobStatusCmd = getJobTerminationCommand($jobTermPath, $jobtype);
}
elseif(LOCALSEARCH == $operationType){
$jobtype = "LOCALSEARCH";
// $parentPid = '';
// $parentPid = trim(file_get_contents($pidPath));
// if($parentPid !='' && $machine == SYNOLOGY){
if($machine == SYNOLOGY) {
$find = end(explode('/', $jobName));
// $jobStatusCmd = "$psPath | grep $parentPid| grep -v \"\-sh\" | grep -v grep | grep \"\-name '*$find\"";
$jobStatusCmd = "$psPath | grep -v \"\-sh\" | grep -v grep | grep \"\-iname '*$find\"";
} else {
$jobName = str_replace("'","'\\''",$jobName);
$jobName = "'".$jobName."'";
$jobStatusCmd = getJobTerminationCommand($jobName, $jobtype);
}
}
elseif(DELETE == $operationType){
$jobtype = "DELETE";
$jobStatusCmd = getJobTerminationCommand($jobName, $jobtype);
}
return $jobStatusCmd;
}
?>