123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- class TripalJob {
-
- protected $job_id = NULL;
-
- protected $job = NULL;
-
- public function __construct() {
- }
-
- public function load($job_id) {
-
- if (!$job_id or !is_numeric($job_id)) {
-
-
- if (is_object($job_id) AND is_a($job_id, 'TripalJob')) {
- $job_id = $job_id->job->job_id;
- }
-
-
- else {
- throw new Exception("You must provide the job_id to load the job.");
- }
- }
- $sql = 'SELECT j.* FROM {tripal_jobs} j WHERE j.job_id = :job_id';
- $args = array(':job_id' => $job_id);
- $this->job = db_query($sql, $args)->fetchObject();
- if (!$this->job) {
- throw new Exception("Cannot find a job with this ID provided.");
- }
-
- $this->job->submit_date_string = $this->job->submit_date ? format_date($this->job->submit_date) : '';
- $this->job->start_time_string = $this->job->start_time ? format_date($this->job->start_time): '';
- $this->job->end_time_string = $this->job->end_time ? format_date($this->job->end_time): '';
-
- $this->job->includes = unserialize($this->job->includes);
-
-
-
-
- $this->job->arguments = unserialize($this->job->arguments);
- if (!is_array($this->job->arguments)) {
- $this->job->arguments = explode("::", $this->job->arguments);
- }
- }
-
- public function create($details) {
-
- if (!array_key_exists('prority', $details)) {
- $details['priority'] = 10;
- }
- if (!array_key_exists('includes', $details)) {
- $details['includes'] = array();
- }
- if (!array_key_exists('ignore_duplicate', $details)) {
- $details['ignore_duplicate'] = FALSE;
- }
-
- if (!$details['job_name']) {
- throw new Exception("Must provide a 'job_name' to create a job.");
- }
- if (!$details['modulename']) {
- throw new Exception("Must provide a 'modulename' to create a job.");
- }
- if (!$details['callback']) {
- throw new Exception("Must provide a 'callback' to create a job.");
- }
- if ($details['ignore_duplicate'] !== FALSE and $details['ignore_duplicate'] !== TRUE) {
- throw new Exception("Must provide either TRUE or FALSE for the ignore_duplicate option when creating a job.");
- }
- $includes = $details['includes'];
- foreach ($includes as $path) {
- $full_path = $_SERVER['DOCUMENT_ROOT'] . base_path() . $path;
- if (!empty($path)) {
- if (file_exists($path)) {
- require_once($path);
- }
- elseif (file_exists($full_path)) {
- require_once($path);
- }
- elseif (!empty($path)) {
- throw new Exception("Included files for Tripal Job must exist. This path ($full_path) doesn't exist.");
- }
- }
- }
- if (!function_exists($details['callback'])) {
- throw new Exception("Must provide a valid callback function to the tripal_add_job() function.");
- }
- if (!is_numeric($details['uid'])) {
- throw new Exception("Must provide a numeric \$uid argument to the tripal_add_job() function.");
- }
- $priority = $details['priority'];
- if (!$priority or !is_numeric($priority) or $priority < 1 or $priority > 10) {
- throw new Exception("Must provide a numeric \$priority argument between 1 and 10 to the tripal_add_job() function.");
- }
- $arguments = $details['arguments'];
- if (!is_array($arguments)) {
- throw new Exception("Must provide an array as the \$arguments argument to the tripal_add_job() function.");
- }
-
- $args = array();
- if (is_array($arguments)) {
- $args = serialize($arguments);
- }
- try {
-
-
- if ($details['ignore_duplicate'] === TRUE) {
- $query = db_select('tripal_jobs', 'tj');
- $query->fields('tj', array('job_id'));
- $query->condition('job_name', $details['job_name']);
- $query->isNull('start_time');
- $job_id = $query->execute()->fetchField();
- if ($job_id) {
- $this->load($job_id);
- return FALSE;
- }
- }
- $job_id = db_insert('tripal_jobs')
- ->fields(array(
- 'job_name' => $details['job_name'],
- 'modulename' => $details['modulename'],
- 'callback' => $details['callback'],
- 'status' => 'Waiting',
- 'submit_date' => time(),
- 'uid' => $details['uid'],
- 'priority' => $priority,
- 'arguments' => $args,
- 'includes' => serialize($includes),
- ))
- ->execute();
-
- $this->load($job_id);
- return TRUE;
- }
- catch (Exception $e) {
- throw new Exception('Cannot create job: ' . $e->getMessage());
- }
- }
-
- public function cancel() {
- if (!$this->job) {
- throw new Exception("There is no job associated with this object. Cannot cancel");
- }
- if ($this->job->status == 'Running') {
- throw new Exception("Job Cannot be cancelled it is currently running.");
- }
- if ($this->job->status == 'Completed') {
- throw new Exception("Job Cannot be cancelled it has already finished.");
- }
- if ($this->job->status == 'Error') {
- throw new Exception("Job Cannot be cancelled it is in an error state.");
- }
- if ($this->job->status == 'Cancelled') {
- throw new Exception("Job Cannot be cancelled it is already cancelled.");
- }
-
- try {
- if ($this->job->start_time == 0) {
- $record = new stdClass();
- $record->job_id = $this->job->job_id;
- $record->status = 'Cancelled';
- $record->progress = '0';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- }
- }
- catch (Exception $e) {
- throw new Exception('Cannot cancel job: ' . $e->getMessage());
- }
- }
-
- public function run() {
- if (!$this->job) {
- throw new Exception('Cannot launch job as no job is associated with this object.');
- }
- try {
-
- foreach ($this->job->includes as $path) {
- if ($path) {
- require_once $path;
- }
- }
-
- $record = new stdClass();
- $record->job_id = $this->job->job_id;
- $record->start_time = time();
- $record->status = 'Running';
- $record->pid = getmypid();
- drupal_write_record('tripal_jobs', $record, 'job_id');
-
-
-
-
-
- $arguments = $this->job->arguments;
- $callback = $this->job->callback;
- $ref = new ReflectionFunction($callback);
- $refparams = $ref->getParameters();
- if (count($refparams) > 0) {
- $lastparam = $refparams[count($refparams)-1];
- if ($lastparam->getName() == 'job_id') {
- $arguments[] = $this->job->job_id;
- }
- else {
- $arguments[] = $this;
- }
- }
-
- call_user_func_array($callback, $arguments);
-
- $record = new stdClass();
- $record->job_id = $this->job->job_id;
- $record->end_time = time();
- $record->error_msg = $this->job->error_msg;
- $record->progress = 100;
- $record->status = 'Completed';
- $record->pid = '';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- $this->load($this->job->job_id);
- }
- catch (Exception $e) {
- $record->end_time = time();
- $record->error_msg = $this->job->error_msg;
- $record->progress = $this->job->progress;
- $record->status = 'Error';
- $record->pid = '';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- drupal_set_message('Job execution failed: ' . $e->getMessage(), 'error');
- }
- }
-
- public function isRunning() {
- if (!$this->job) {
- throw new Exception('Cannot check running status as no job is associated with this object.');
- }
- $status = shell_exec('ps -p ' . escapeshellarg($this->job->pid) . ' -o pid=');
- if ($this->job->pid && $status) {
-
- return TRUE;
- }
-
- return FALSE;
- }
-
- public function getJob(){
- return $this->job;
- }
-
- public function getJobID(){
- return $this->job->job_id;
- }
-
- public function getUID() {
- return $this->job->uid;
- }
-
- public function getJobName() {
- return $this->job->job_name;
- }
-
- public function getModuleName() {
- return $this->job->modulename;
- }
-
- public function getCallback() {
- return $this->job->callback;
- }
-
- public function getArguments() {
- return $this->job->arguments;
- }
-
- public function getProgress() {
- return $this->job->progress;
- }
-
- public function setProgress($percent_done) {
- if (!$this->job) {
- throw new Exception('Cannot set progress as no job is associated with this object.');
- }
- $this->job->progress = $percent_done;
- $progress = sprintf("%d", $percent_done);
- db_update('tripal_jobs')
- ->fields(array(
- 'progress' => $progress,
- ))
- ->condition('job_id', $this->job->job_id)
- ->execute();
- }
-
- public function getStatus() {
- return $this->job->status;
- }
-
- public function getSubmitTime() {
- return $this->job->submit_date;
- }
-
- public function getStartTime() {
- return $this->job->start_time;
- }
-
- public function getEndTime() {
- return $this->job->end_time;
- }
-
- public function getLog() {
- return $this->job->error_msg;
- }
-
- public function getPID() {
- return $this->job->pid;
- }
-
- public function getPriority() {
- return $this->job->priority;
- }
-
- public function getMLock() {
- return $this->job->mlock;
- }
-
- public function getLock() {
- return $this->job->lock;
- }
-
- public function getIncludes() {
- return $this->job->includes;
- }
-
- public function logMessage($message, $variables = array(), $severity = TRIPAL_INFO) {
-
- $tmessage = t($message, $variables);
-
-
- print $tmessage . "\n";
-
- $this->job->error_msg .= "\n" . $tmessage;
-
- if ($severity == TRIPAL_CRITICAL or $severity == TRIPAL_ERROR) {
- tripal_report_error('tripal_job', $severity, $message, $variables);
- $this->job->status = 'Error';
- }
- }
- }
|