Tripal v1.1 (6.x-1.1)
|
00001 <?php 00064 function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid, $priority = 10) { 00065 00066 // convert the arguments into a string for storage in the database 00067 $args = implode("::", $arguments); 00068 $record = new stdClass(); 00069 $record->job_name = $job_name; 00070 $record->modulename = $modulename; 00071 $record->callback = $callback; 00072 $record->status = 'Waiting'; 00073 $record->submit_date = time(); 00074 $record->uid = $uid; 00075 $record->priority = $priority; # the lower the number the higher the priority 00076 if ($args) { 00077 $record->arguments = $args; 00078 } 00079 if (drupal_write_record('tripal_jobs', $record)) { 00080 $jobs_url = url("admin/tripal/tripal_jobs"); 00081 drupal_set_message(t("Job '%job_name' submitted. Check the <a href='!jobs_url'>jobs page</a> for status", array('%job_name' => $job_name, '!jobs_url' => $jobs_url))); 00082 } 00083 else { 00084 drupal_set_message(t("Failed to add job %job_name.", array('%job_name' => $job_name)), 'error'); 00085 } 00086 00087 return $record->job_id; 00088 } 00089 00098 function tripal_jobs_check_running() { 00099 00100 // iterate through each job that has not ended 00101 // and see if it is still running. If it is not 00102 // running but does not have an end_time then 00103 // set the end time and set the status to 'Error' 00104 $sql = "SELECT * FROM {tripal_jobs} TJ ". 00105 "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL "; 00106 $jobs = db_query($sql); 00107 while ($job = db_fetch_object($jobs)) { 00108 $status = `ps -p $job->pid -o pid=`; 00109 if ($job->pid && $status) { 00110 // the job is still running so let it go 00111 // we return 1 to indicate that a job is running 00112 return TRUE; 00113 } 00114 else { 00115 // the job is not running so terminate it 00116 $record = new stdClass(); 00117 $record->job_id = $job->job_id; 00118 $record->end_time = time(); 00119 $record->status = 'Error'; 00120 $record->error_msg = 'Job has terminated unexpectedly.'; 00121 drupal_write_record('tripal_jobs', $record, 'job_id'); 00122 } 00123 } 00124 00125 // return 1 to indicate that no jobs are currently running. 00126 return FALSE; 00127 } 00128 00140 function tripal_jobs_get_start_time($job) { 00141 00142 if ($job->start_time > 0) { 00143 $start = format_date($job->start_time); 00144 } 00145 else { 00146 if (strcmp($job->job_status, 'Cancelled')==0) { 00147 $start = 'Cancelled'; 00148 } 00149 else { 00150 $start = 'Not Yet Started'; 00151 } 00152 } 00153 return $start; 00154 } 00155 00167 function tripal_jobs_get_end_time($job) { 00168 00169 if ($job->end_time > 0) { 00170 $end = format_date($job->end_time); 00171 } 00172 else { 00173 $end = ''; 00174 } 00175 00176 return $end; 00177 } 00186 function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) { 00187 global $user; 00188 00189 $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d"; 00190 $job = db_fetch_object(db_query($sql, $job_id)); 00191 $args = explode("::", $job->arguments); 00192 $job_id = tripal_add_job( 00193 $job->job_name, 00194 $job->modulename, 00195 $job->callback, 00196 $args, 00197 $user->uid, 00198 $job->priority); 00199 00200 if ($goto_jobs_page) { 00201 drupal_goto("admin/tripal/tripal_jobs"); 00202 } 00203 return $job_id; 00204 } 00205 00214 function tripal_jobs_cancel($job_id, $redirect = TRUE) { 00215 $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d"; 00216 $job = db_fetch_object(db_query($sql, $job_id)); 00217 00218 // set the end time for this job 00219 if ($job->start_time == 0) { 00220 $record = new stdClass(); 00221 $record->job_id = $job->job_id; 00222 $record->end_time = time(); 00223 $record->status = 'Cancelled'; 00224 $record->progress = '0'; 00225 drupal_write_record('tripal_jobs', $record, 'job_id'); 00226 drupal_set_message(t("Job #%job_id cancelled", array('%job_id' => $job_id))); 00227 } 00228 else { 00229 drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id))); 00230 } 00231 if ($redirect) { 00232 drupal_goto("admin/tripal/tripal_jobs"); 00233 } 00234 } 00235 00251 function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) { 00252 00253 // first check if any jobs are currently running 00254 // if they are, don't continue, we don't want to have 00255 // more than one job script running at a time 00256 if (!$do_parallel and tripal_jobs_check_running()) { 00257 print "Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel."; 00258 return; 00259 } 00260 00261 // get all jobs that have not started and order them such that 00262 // they are processed in a FIFO manner. 00263 if ($job_id) { 00264 $sql = "SELECT * FROM {tripal_jobs} TJ ". 00265 "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = %d ". 00266 "ORDER BY priority ASC,job_id ASC"; 00267 $job_res = db_query($sql,$job_id); 00268 } 00269 else { 00270 $sql = "SELECT * FROM {tripal_jobs} TJ ". 00271 "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ". 00272 "ORDER BY priority ASC,job_id ASC"; 00273 $job_res = db_query($sql); 00274 } 00275 while ($job = db_fetch_object($job_res)) { 00276 // set the start time for this job 00277 $record = new stdClass(); 00278 $record->job_id = $job->job_id; 00279 $record->start_time = time(); 00280 $record->status = 'Running'; 00281 $record->pid = getmypid(); 00282 drupal_write_record('tripal_jobs', $record, 'job_id'); 00283 00284 // call the function provided in the callback column. 00285 // Add the job_id as the last item in the list of arguments. All 00286 // callback functions should support this argument. 00287 $callback = $job->callback; 00288 $args = split("::", $job->arguments); 00289 $args[] = $job->job_id; 00290 print "Calling: $callback(" . implode(", ", $args) . ")\n"; 00291 call_user_func_array($callback, $args); 00292 // set the end time for this job 00293 $record->end_time = time(); 00294 $record->status = 'Completed'; 00295 $record->progress = '100'; 00296 drupal_write_record('tripal_jobs', $record, 'job_id'); 00297 00298 // send an email to the user advising that the job has finished 00299 } 00300 } 00301 00315 function tripal_job_set_progress($job_id, $percentage) { 00316 00317 if (preg_match("/^(\d+|100)$/", $percentage)) { 00318 $record = new stdClass(); 00319 $record->job_id = $job_id; 00320 $record->progress = $percentage; 00321 if (drupal_write_record('tripal_jobs', $record, 'job_id')) { 00322 return TRUE; 00323 } 00324 } 00325 00326 return FALSE; 00327 } 00339 function tripal_get_module_active_jobs($modulename) { 00340 $sql = "SELECT * FROM {tripal_jobs} TJ ". 00341 "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' "; 00342 return db_fetch_object(db_query($sql, $modulename)); 00343 } 00344 00356 function tripal_jobs_get_submit_date($job) { 00357 return format_date($job->submit_date); 00358 }