/**
  * \brief Given a folder_pk, add a job.
  * \param $uploadpk - the upload(upload_id) you want to delete
  * \param $Depends - Depends is not used for now
  *
  * \return NULL on success, string on failure.
  */
 function Delete($uploadpk, $Depends = NULL)
 {
     global $SysConf;
     /* Prepare the job: job "Delete" */
     $user_pk = Auth::getUserId();
     $group_pk = Auth::getGroupId();
     $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $uploadpk);
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE UPLOAD {$uploadpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         $error_msg = _("Is the scheduler running? Your jobs have been added to job queue.");
         $URL = Traceback_uri() . "?mod=showjobs&upload={$uploadpk} ";
         $LinkText = _("View Jobs");
         $msg = "{$error_msg} <a href={$URL}>{$LinkText}</a>";
         return $msg;
     }
     return NULL;
 }
示例#2
0
 /**
  * \brief queue the job
  *
  * \param
  * \returns status string
  **/
 function QueueJob()
 {
     global $SysConf;
     /* Find all the maintagent options specified by the user.
      * They look like _REQUEST["a"] = "a", _REQUEST["b"]="b", ...
      */
     $options = "-";
     foreach ($_REQUEST as $key => $value) {
         if ($key == $value) {
             $options .= $value;
         }
     }
     /* Create the maintenance job */
     $user_pk = $SysConf['auth']['UserId'];
     $upload_pk = 0;
     // dummy
     $job_pk = JobAddJob($user_pk, "Maintenance", $upload_pk);
     if (empty($job_pk) || $job_pk < 0) {
         return _("Failed to insert job record");
     }
     $jq_pk = JobQueueAdd($job_pk, "maintagent", NULL, NULL, NULL, NULL, $options);
     if (empty($jq_pk)) {
         return _("Failed to insert task 'Maintenance' into job queue");
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return _("The maintenance job has been queued");
 }
示例#3
0
 /**
  * \brief Delete
  * Creates a job to detele the folder
  *
  * \param $folderpk - the folder_pk to remove
  * \return NULL on success, string on failure.
  */
 function Delete($folderpk, $userId)
 {
     /* Can't remove top folder */
     if ($folderpk == FolderGetTop()) {
         $text = _("Can Not Delete Root Folder");
         return $text;
     }
     /* Get the folder's name */
     $FolderName = FolderGetName($folderpk);
     /* Prepare the job: job "Delete" */
     $groupId = Auth::getGroupId();
     $jobpk = JobAddJob($userId, $groupId, "Delete Folder: {$FolderName}");
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE FOLDER {$folderpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return NULL;
 }
 /**
  * \brief Given a folder_pk, add a job.
  * \param $uploadpk - the upload(upload_id) you want to delete
  * \param $Depends - Depends is not used for now
  *
  * \return NULL on success, string on failure.
  */
 function Delete($uploadpk, $Depends = NULL)
 {
     global $SysConf;
     /* Prepare the job: job "Delete" */
     $user_pk = $SysConf['auth']['UserId'];
     $jobpk = JobAddJob($user_pk, "Delete", $uploadpk);
     if (empty($jobpk) || $jobpk < 0) {
         $text = _("Failed to create job record");
         return $text;
     }
     /* Add job: job "Delete" has jobqueue item "delagent" */
     $jqargs = "DELETE UPLOAD {$uploadpk}";
     $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
     if (empty($jobqueuepk)) {
         $text = _("Failed to place delete in job queue");
         return $text;
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         return $error_msg . "\n" . $output;
     }
     return NULL;
 }
示例#5
0
 function Output()
 {
     if ($this->State != PLUGIN_STATE_READY) {
         return 0;
     }
     $V = "";
     $Page = "";
     $UploadPk = GetParm('upload', PARM_INTEGER);
     if (empty($UploadPk)) {
         $UploadPk = -1;
     } else {
         $UploadPerm = GetUploadPerm($UploadPk);
         if ($UploadPerm < PERM_WRITE) {
             $text = _("Permission Denied");
             echo "<h2>{$text}<h2>";
             return;
         }
     }
     switch ($this->OutputType) {
         case "XML":
             break;
         case "HTML":
             // micro menus
             $V .= menu_to_1html(menu_find($this->Name, $MenuDepth), 0);
             /* Process any actions */
             if (@$_SESSION['UserLevel'] >= PLUGIN_DB_WRITE) {
                 $jq_pk = GetParm("jobid", PARM_INTEGER);
                 $Action = GetParm("action", PARM_STRING);
                 $UploadPk = GetParm("upload", PARM_INTEGER);
                 if (!empty($UploadPk)) {
                     $UploadPerm = GetUploadPerm($UploadPk);
                     if ($UploadPerm < PERM_WRITE) {
                         $text = _("Permission Denied");
                         echo "<h2>{$text}<h2>";
                         return;
                     }
                 }
                 $Page = GetParm('page', PARM_INTEGER);
                 if (empty($Page)) {
                     $Page = 0;
                 }
                 $jqtype = GetParm("jqtype", PARM_STRING);
                 $ThisURL = Traceback_uri() . "?mod=" . $this->Name . "&upload={$UploadPk}";
                 $Job = GetParm('job', PARM_INTEGER);
                 switch ($Action) {
                     case 'pause':
                         if (empty($jq_pk)) {
                             break;
                         }
                         $Command = "pause {$jq_pk}";
                         $rv = fo_communicate_with_scheduler($Command, $response_from_scheduler, $error_info);
                         if ($rv == false) {
                             $V .= _("Unable to pause job.") . " " . $response_from_scheduler . $error_info;
                         }
                         echo "<script type=\"text/javascript\"> window.location.replace(\"{$ThisURL}\"); </script>";
                         break;
                     case 'restart':
                         if (empty($jq_pk)) {
                             break;
                         }
                         $Command = "restart {$jq_pk}";
                         $rv = fo_communicate_with_scheduler($Command, $response_from_scheduler, $error_info);
                         if ($rv == false) {
                             $V .= _("Unable to restart job.") . " " . $response_from_scheduler . $error_info;
                         }
                         echo "<script type=\"text/javascript\"> window.location.replace(\"{$ThisURL}\"); </script>";
                         break;
                     case 'cancel':
                         if (empty($jq_pk)) {
                             break;
                         }
                         $Msg = "\"" . _("Killed by") . " " . @$_SESSION['User'] . "\"";
                         $Command = "kill {$jq_pk} {$Msg}";
                         $rv = fo_communicate_with_scheduler($Command, $response_from_scheduler, $error_info);
                         if ($rv == false) {
                             $V .= _("Unable to cancel job.") . $response_from_scheduler . $error_info;
                         }
                         echo "<script type=\"text/javascript\"> window.location.replace(\"{$ThisURL}\"); </script>";
                         break;
                     default:
                         break;
                 }
             }
             if (!empty($Job)) {
                 $V .= $this->ShowJobDB($Job);
             } else {
                 if ($UploadPk) {
                     $upload_pks = array($UploadPk);
                     $Jobs = $this->Uploads2Jobs($upload_pks, $Page);
                 } else {
                     $Jobs = $this->MyJobs($this->nhours);
                 }
                 $JobsInfo = $this->GetJobInfo($Jobs, $Page);
                 /* Sort jobs by job_pk (so most recent comes out first) */
                 usort($JobsInfo, "CompareJobsInfo");
                 $V .= $this->Show($JobsInfo, $Page);
             }
             break;
         case "Text":
             break;
         default:
             break;
     }
     if (!$this->OutputToStdout) {
         return $V;
     }
     print "{$V}";
     return;
 }
 /**
  * \brief submit the specified operation
  * \param $operation operation name, e.g. 'status'
  * \param $job_id selected job id
  * \param $priority_id selected priority id
  * \param $level_id selected level id
  * \return return response from the scheduler
  **/
 function OperationSubmit($operation, $job_id, $priority_id, $level_id)
 {
     if ("start" === $operation) {
         $commu_status = fo_communicate_with_scheduler('status', $response_from_scheduler, $this->error_info);
         if ($commu_status) {
             $response_from_scheduler = "Warning, the scheduler is running";
         } else {
             $this->error_info = null;
             $this->StartScheduler();
             return $this->error_info;
         }
     } else {
         if ("restarts" === $operation) {
             $this->StartScheduler('restarts');
             return $this->error_info;
         }
     }
     $commands = $operation;
     if (!empty($job_id) && 'scheduler' != $job_id) {
         $commands .= " {$job_id}";
     }
     if (isset($priority_id)) {
         $commands .= " {$priority_id}";
     }
     if (!empty($level_id)) {
         $commands .= " {$level_id}";
     }
     $commands = trim($commands);
     $commu_status = fo_communicate_with_scheduler($commands, $response_from_scheduler, $this->error_info);
     return $response_from_scheduler . $this->error_info;
 }
示例#7
0
/**
 * \brief Queue an agent.  This is a simple version of AgentAdd() that can be
 *  used by multiple plugins that only use upload_pk as jqargs.
 *  Before queuing, check if agent needs to be queued.  It doesn't need to be queued if:
 *  - It is already queued
 *  - It has already been run by the latest agent version
 *
 * \param $plugin caller plugin object
 * \param $job_pk
 * \param $upload_pk
 * \param $ErrorMsg - error message on failure
 * \param $Dependencies - array of named dependencies. Each array element is the plugin name.
 *         For example,  array(agent_adj2nest, agent_pkgagent).  
 *         Typically, this will just be array(agent_adj2nest).
 * \param $jqargs (optional) jobqueue.jq_args
 *
 * \returns
 * - jq_pk Successfully queued
 * -   0   Not queued, latest version of agent has previously run successfully
 * -  -1   Not queued, error, error string in $ErrorMsg
 **/
function CommonAgentAdd($plugin, $job_pk, $upload_pk, &$ErrorMsg, $Dependencies, $jqargs = "", $jq_cmd_args = NULL)
{
    global $Plugins;
    $Deps = array();
    $DependsEmpty = array();
    /* check if the latest agent has already been run */
    if ($plugin->AgentHasResults($upload_pk) == 1) {
        return 0;
    }
    /* if it is already scheduled, then return success */
    if (($jq_pk = IsAlreadyScheduled($job_pk, $plugin->AgentName, $upload_pk)) != 0) {
        return $jq_pk;
    }
    /* queue up dependencies */
    foreach ($Dependencies as $Dependency) {
        if (is_array($Dependency)) {
            $PluginName = $Dependency['name'];
            $DepArgs = $Dependency['args'];
        } else {
            $PluginName = $Dependency;
            $DepArgs = null;
        }
        $DepPlugin = plugin_find($PluginName);
        if ($DepPlugin === null) {
            $ErrorMsg = "Invalid plugin name: {$PluginName}, (CommonAgentAdd())";
            return -1;
        }
        if (($Deps[] = $DepPlugin->AgentAdd($job_pk, $upload_pk, $ErrorMsg, $DependsEmpty, $DepArgs)) == -1) {
            return -1;
        }
    }
    /* schedule AgentName */
    if (empty($jqargs)) {
        $jqargs = $upload_pk;
    }
    $jq_pk = JobQueueAdd($job_pk, $plugin->AgentName, $jqargs, "", $Deps, NULL, $jq_cmd_args);
    if (empty($jq_pk)) {
        $ErrorMsg = _("Failed to insert agent {$plugin->AgentName} into job queue. jqargs: {$jqargs}");
        return -1;
    }
    /* Tell the scheduler to check the queue. */
    $success = fo_communicate_with_scheduler("database", $output, $error_msg);
    if (!$success) {
        $ErrorMsg = $error_msg . "\n" . $output;
    }
    return $jq_pk;
}
/**
* \brief  Get runnable job list, the process is below:
          1. send command 'status'to scheduler
          2. the scheduler return status of all scheduled jobs
          3. retrieve the job list
          4. return the job list 

* \return an array, the runnable job list
          the array is like: Array(1, 2, 3, .., i), sorted, if no jobs, return nothing
*/
function GetRunnableJobList()
{
    /* get the raw job list from scheduler 
         send command 'status' to the scheduler, get the all status of runnable jobs and scheduler 
         like:
          scheduler:[#] daemon:[#] jobs:[#] log:[str] port:[#] verbose:[#]
          job:[#] status:[str] type:[str] priority:[#] running:[#] finished[#] failed:[#]
          job:[#] status:[str] type:[str] priority:[#] running:[#] finished[#] failed:[#]
       */
    $command = "status";
    $command_status = fo_communicate_with_scheduler($command, $status_info, $error_msg);
    /* can not get status info from the scheduler, so can not get runnable jobs, probably the scheduler is not running */
    if (false === $command_status) {
        return;
    }
    $pattern = '/job:(\\d+) /';
    preg_match_all($pattern, $status_info, $matches);
    /* the $matches[1] is like: Array(1, 2, 3, .., i)  */
    $job_array = $matches[1];
    sort($job_array, SORT_NUMERIC);
    return $job_array;
}
示例#9
0
 public function Output()
 {
     $page = "";
     $uploadPk = GetParm('upload', PARM_INTEGER);
     if (empty($uploadPk)) {
         $uploadPk = -1;
     } elseif ($uploadPk > 0) {
         if (!$this->uploadDao->isEditable($uploadPk, Auth::getGroupId())) {
             $this->vars['message'] = _("Permission Denied");
             return;
         }
     }
     $this->vars['uploadId'] = $uploadPk;
     /* Process any actions */
     $action = GetParm("action", PARM_STRING);
     $page = GetParm('page', PARM_INTEGER) ?: 0;
     if ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_WRITE && !empty($action)) {
         $jq_pk = GetParm("jobid", PARM_INTEGER);
         $uploadPk = GetParm("upload", PARM_INTEGER);
         if (!($uploadPk === -1 && ($_SESSION[Auth::USER_LEVEL] >= PLUGIN_DB_ADMIN || $this->jobDao->hasActionPermissionsOnJob($jq_pk, Auth::getUserId(), Auth::getGroupId()))) && !$this->uploadDao->isEditable($uploadPk, Auth::getGroupId())) {
             $this->vars['message'] = _("Permission Denied to perform action");
         } else {
             $thisURL = Traceback_uri() . "?mod=" . $this->Name . "&upload={$uploadPk}";
             switch ($action) {
                 case 'pause':
                     if (empty($jq_pk)) {
                         break;
                     }
                     $command = "pause {$jq_pk}";
                     $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
                     if ($rv == false) {
                         $this->vars['errorInfo'] = _("Unable to pause job.") . " " . $response_from_scheduler . $error_info;
                     }
                     echo "<script type=\"text/javascript\"> window.location.replace(\"{$thisURL}\"); </script>";
                     break;
                 case 'restart':
                     if (empty($jq_pk)) {
                         break;
                     }
                     $command = "restart {$jq_pk}";
                     $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
                     if ($rv == false) {
                         $this->vars['errorInfo'] = _("Unable to restart job.") . " " . $response_from_scheduler . $error_info;
                     }
                     echo "<script type=\"text/javascript\"> window.location.replace(\"{$thisURL}\"); </script>";
                     break;
                 case 'cancel':
                     if (empty($jq_pk)) {
                         break;
                     }
                     $Msg = "\"" . _("Killed by") . " " . $_SESSION[Auth::USER_NAME] . "\"";
                     $command = "kill {$jq_pk} {$Msg}";
                     $rv = fo_communicate_with_scheduler($command, $response_from_scheduler, $error_info);
                     if ($rv == false) {
                         $this->vars['errorInfo'] = _("Unable to cancel job.") . $response_from_scheduler . $error_info;
                     }
                     echo "<script type=\"text/javascript\"> window.location.replace(\"{$thisURL}\"); </script>";
                     break;
             }
         }
     }
     $job = GetParm('job', PARM_INTEGER);
     if (!empty($job)) {
         $this->vars['jobId'] = $job;
         $this->vars['uploadName'] = $this->getUploadNameForGeekyScan($job);
     } else {
         $allusersval = GetParm("allusers", PARM_INTEGER);
         if (!$allusersval) {
             $allusersval = 0;
         }
         $this->vars['allusersval'] = $allusersval;
         if (!$page) {
             $page = 0;
         }
         $this->vars['page'] = $page;
         $this->vars['clockTime'] = $this->getTimeToRefresh();
         $this->vars['allusersdiv'] = menu_to_1html(menu_find($this->Name, $MenuDepth), 0);
         $this->vars['injectedFoot'] = GetParm("injectedFoot", PARM_TEXT);
         $this->vars['message'] = GetParm("injectedMessage", PARM_TEXT);
     }
 }
示例#10
0
 /**
  * \brief Given an uploadpk, add a job.
  * \param $Depends - specifying other dependencies.
  * $Depends can be a jq_pk, or an array of jq_pks, or NULL.
  *
  * \return NULL on success, string on failure.
  */
 function AgentAdd($uploadpk, $Depends = NULL, $priority = 0)
 {
     global $PG_CONN;
     $Job_name = str_replace("'", "''", "reunpack");
     //get userpk from uploadpk
     $UploadRec = GetSingleRec("upload", "where upload_pk='{$uploadpk}'");
     //if upload record didn't have user pk, use current user
     $user_fk = $UploadRec['user_fk'];
     if (empty($user_fk)) {
         $user_fk = $_SESSION[UserId];
     }
     //updated ununpack_ars table to let reunpack run
     $SQLARS = "UPDATE ununpack_ars SET ars_success = FALSE WHERE upload_fk = '{$uploadpk}';";
     $result = pg_query($PG_CONN, $SQLARS);
     DBCheckResult($result, $SQLARS, __FILE__, __LINE__);
     pg_free_result($result);
     if (empty($uploadpk)) {
         $SQLInsert = "INSERT INTO job\n        (job_queued,job_priority,job_name,job_user_fk) VALUES\n        (now(),'{$priority}','{$Job_name}','{$user_fk}');";
     } else {
         $SQLInsert = "INSERT INTO job\n        (job_queued,job_priority,job_name,job_upload_fk,job_user_fk) VALUES\n        (now(),'{$priority}','{$Job_name}','{$uploadpk}','{$user_fk}');";
     }
     $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk = '{$user_fk}' ORDER BY job_pk DESC LIMIT 1;";
     $result = pg_query($PG_CONN, $SQLcheck);
     DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
     $row = pg_fetch_assoc($result);
     pg_free_result($result);
     if (!empty($row)) {
         $jobpk = $row['job_pk'];
     } else {
         $result = pg_query($PG_CONN, $SQLInsert);
         DBCheckResult($result, $SQLInsert, __FILE__, __LINE__);
         $row = pg_fetch_assoc($result);
         pg_free_result($result);
         $SQLcheck = "SELECT job_pk FROM job WHERE job_upload_fk = '{$uploadpk}' AND job_name = '{$Job_name}' AND job_user_fk = '{$user_fk}';";
         $result = pg_query($PG_CONN, $SQLcheck);
         DBCheckResult($result, $SQLcheck, __FILE__, __LINE__);
         $row = pg_fetch_assoc($result);
         pg_free_result($result);
         $jobpk = $row['job_pk'];
     }
     if (empty($jobpk) || $jobpk < 0) {
         return "Failed to insert job record! {$SQLInsert}";
     }
     if (!empty($Depends) && !is_array($Depends)) {
         $Depends = array($Depends);
     }
     /* job "unpack" has jobqueue item "unpack" */
     $jqargs = "SELECT pfile.pfile_sha1 || '.' || pfile.pfile_md5 || '.' || pfile.pfile_size AS pfile,\n      upload_pk, pfile_fk\n        FROM upload\n        INNER JOIN pfile ON upload.pfile_fk = pfile.pfile_pk\n        WHERE upload.upload_pk = '{$uploadpk}';";
     echo "JobQueueAdd used to do a reschedule here<br>";
     $jobqueuepk = JobQueueAdd($jobpk, "ununpack", $uploadpk, NULL, $Depends);
     if (empty($jobqueuepk)) {
         return "Failed to insert item into job queue";
     }
     /* Tell the scheduler to check the queue. */
     $success = fo_communicate_with_scheduler("database", $output, $error_msg);
     if (!$success) {
         $ErrorMsg = $error_msg . "\n" . $output;
     }
     return NULL;
 }
示例#11
0
 /**
  * @param int $jobId
  * @param int $uploadId
  * @param &string $errorMsg - error message on failure
  * @param array $dependencies
  * @param string|null $jqargs (optional) jobqueue.jq_args
  * @return
  * * jqId  Successfully queued
  * *   0   Not queued, latest version of agent has previously run successfully
  * *  -1   Not queued, error, error string in $ErrorMsg
  **/
 protected function doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs = "", $jq_cmd_args = null)
 {
     $deps = array();
     foreach ($dependencies as $dependency) {
         $dep = $this->implicitAgentAdd($jobId, $uploadId, $errorMsg, $dependency);
         if ($dep == -1) {
             return -1;
         }
         $deps[] = $dep;
     }
     if (empty($jqargs)) {
         $jqargs = $uploadId;
     }
     $jobQueueId = \JobQueueAdd($jobId, $this->AgentName, $jqargs, "", $deps, NULL, $jq_cmd_args);
     if (empty($jobQueueId)) {
         $errorMsg = "Failed to insert agent {$this->AgentName} into job queue. jqargs: {$jqargs}";
         return -1;
     }
     $success = \fo_communicate_with_scheduler("database", $output, $errorMsg);
     if (!$success) {
         $errorMsg .= "\n" . $output;
     }
     return $jobQueueId;
 }