123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- <?php
- function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid, $priority = 10) {
-
- $args = implode("::", $arguments);
- $record = new stdClass();
- $record->job_name = $job_name;
- $record->modulename = $modulename;
- $record->callback = $callback;
- $record->status = 'Waiting';
- $record->submit_date = time();
- $record->uid = $uid;
- $record->priority = $priority;
- if ($args) {
- $record->arguments = $args;
- }
- if (drupal_write_record('tripal_jobs', $record)) {
- $jobs_url = url("admin/tripal/tripal_jobs");
- 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)));
- }
- else {
- drupal_set_message(t("Failed to add job %job_name.", array('%job_name' => $job_name)), 'error');
- }
- return $record->job_id;
- }
- function tripal_job_set_progress($job_id, $percentage) {
- if (preg_match("/^(\d+|100)$/", $percentage)) {
- $record = new stdClass();
- $record->job_id = $job_id;
- $record->progress = $percentage;
- if (drupal_write_record('tripal_jobs', $record, 'job_id')) {
- return TRUE;
- }
- }
- return FALSE;
- }
- function tripal_get_module_active_jobs($modulename) {
- $sql = "SELECT * FROM {tripal_jobs} TJ ".
- "WHERE TJ.end_time IS NULL and TJ.modulename = '%s' ";
- return db_fetch_object(db_query($sql, $modulename));
- }
- function tripal_jobs_report_form($form, &$form_state = NULL) {
- $form = array();
-
-
- $default_status = $form_state['values']['job_status'];
-
- if (!$default_status) {
- $default_status = $_SESSION['tripal_job_status_filter'];
- }
-
- $form['job_status'] = array(
- '#type' => 'select',
- '#title' => t('Filter by Job Status'),
- '#default_value' => $default_status,
- '#options' => array(
- 0 => 'All Jobs',
- 'Running' => 'Running',
- 'Waiting' => 'Waiting',
- 'Completed' => 'Completed',
- 'Cancelled' => 'Cancelled',
- 'Error' => 'Error',
- ),
- );
-
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Filter'),
- );
- return $form;
- }
- function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
- $job_status = $form_state['values']['job_status'];
- $_SESSION['tripal_job_status_filter'] = $job_status;
- }
- function tripal_jobs_report() {
- $jobs_status_filter = $_SESSION['tripal_job_status_filter'];
-
- $sql = "
- SELECT
- TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
- TJ.status as job_status, TJ,submit_date,TJ.start_time,
- TJ.end_time,TJ.priority,U.name as username
- FROM {tripal_jobs} TJ
- INNER JOIN {users} U on TJ.uid = U.uid ";
- if ($jobs_status_filter) {
- $sql .= "WHERE TJ.status = '%s' ";
- }
- $sql .= "ORDER BY job_id DESC";
-
- $jobs = pager_query($sql, 25, 0, "SELECT count(*) FROM ($sql) as t1", $jobs_status_filter);
- $header = array(
- 'Job ID',
- 'User',
- 'Job Name',
- array('data' => 'Dates', 'style'=> "white-space: nowrap"),
- 'Priority',
- 'Progress',
- 'Status',
- 'Action');
- $rows = array();
-
-
- while ($job = db_fetch_object($jobs)) {
- $submit = tripal_jobs_get_submit_date($job);
- $start = tripal_jobs_get_start_time($job);
- $end = tripal_jobs_get_end_time($job);
- $cancel_link = '';
- if ($job->start_time == 0 and $job->end_time == 0) {
- $cancel_link = "<a href=\"" . url("admin/tripal/tripal_jobs/cancel/" . $job->job_id) . "\">Cancel</a><br />";
- }
- $rerun_link = "<a href=\"" . url("admin/tripal/tripal_jobs/rerun/" . $job->job_id) . "\">Re-run</a><br />";
- $view_link ="<a href=\"" . url("admin/tripal/tripal_jobs/view/" . $job->job_id) . "\">View</a>";
- $rows[] = array(
- $job->job_id,
- $job->username,
- $job->job_name,
- "Submit Date: $submit<br>Start Time: $start<br>End Time: $end",
- $job->priority,
- $job->progress . '%',
- $job->job_status,
- "$cancel_link $rerun_link $view_link",
- );
- }
-
-
- $output .= "Waiting jobs are executed first by priority level (the lower the ".
- "number the higher the priority) and second by the order they ".
- "were entered";
- $output .= drupal_get_form('tripal_jobs_report_form');
- $output .= theme('table', $header, $rows);
- $output .= theme_pager();
- return $output;
- }
- function tripal_jobs_get_start_time($job) {
- if ($job->start_time > 0) {
- $start = format_date($job->start_time);
- }
- else {
- if (strcmp($job->job_status, 'Cancelled')==0) {
- $start = 'Cancelled';
- }
- else {
- $start = 'Not Yet Started';
- }
- }
- return $start;
- }
- function tripal_jobs_get_end_time($job) {
- if ($job->end_time > 0) {
- $end = format_date($job->end_time);
- }
- else {
- $end = '';
- }
- return $end;
- }
- function tripal_jobs_get_submit_date($job) {
- return format_date($job->submit_date);
- }
- function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) {
-
-
-
- if (!$do_parallel and tripal_jobs_check_running()) {
- return;
- }
-
-
- if ($job_id) {
- $sql = "SELECT * FROM {tripal_jobs} TJ ".
- "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = %d ".
- "ORDER BY priority ASC,job_id ASC";
- $job_res = db_query($sql,$job_id);
- }
- else {
- $sql = "SELECT * FROM {tripal_jobs} TJ ".
- "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
- "ORDER BY priority ASC,job_id ASC";
- $job_res = db_query($sql);
- }
- while ($job = db_fetch_object($job_res)) {
-
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->start_time = time();
- $record->status = 'Running';
- $record->pid = getmypid();
- drupal_write_record('tripal_jobs', $record, 'job_id');
-
-
-
- $callback = $job->callback;
- $args = split("::", $job->arguments);
- $args[] = $job->job_id;
- print "Calling: $callback(" . implode(", ", $args) . ")\n";
- call_user_func_array($callback, $args);
-
- $record->end_time = time();
- $record->status = 'Completed';
- $record->progress = '100';
- drupal_write_record('tripal_jobs', $record, 'job_id');
-
- }
- }
- function tripal_jobs_check_running() {
-
-
-
-
- $sql = "SELECT * FROM {tripal_jobs} TJ ".
- "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
- $jobs = db_query($sql);
- while ($job = db_fetch_object($jobs)) {
- $status = `ps --pid=$job->pid --no-header`;
- if ($job->pid && $status) {
-
-
- print "Job is still running (pid $job->pid)\n";
- return TRUE;
- }
- else {
-
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->end_time = time();
- $record->status = 'Error';
- $record->error_msg = 'Job has terminated unexpectedly.';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- }
- }
-
- return FALSE;
- }
- function tripal_jobs_view($job_id) {
- return theme('tripal_core_job_view', $job_id);
- }
- function tripal_core_preprocess_tripal_core_job_view(&$variables) {
-
- $job_id = $variables['job_id'];
- $sql =
- "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
- TJ.status as job_status, TJ,submit_date,TJ.start_time,
- TJ.end_time,TJ.priority,U.name as username,TJ.arguments,
- TJ.callback,TJ.error_msg,TJ.pid
- FROM {tripal_jobs} TJ
- INNER JOIN users U on TJ.uid = U.uid
- WHERE TJ.job_id = %d";
- $job = db_fetch_object(db_query($sql, $job_id));
-
-
-
-
-
- $args = preg_split("/::/", $job->arguments);
- $arg_hook = $job->modulename . "_job_describe_args";
- if (is_callable($arg_hook)) {
- $new_args = call_user_func_array($arg_hook, array($job->callback, $args));
- if (is_array($new_args) and count($new_args)) {
- $job->arguments = $new_args;
- }
- else {
- $job->arguments = $args;
- }
- }
- else {
- $job->arguments = $args;
- }
-
- $job->submit_date = tripal_jobs_get_submit_date($job);
- $job->start_time = tripal_jobs_get_start_time($job);
- $job->end_time = tripal_jobs_get_end_time($job);
-
- $variables['job'] = $job;
- }
- function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) {
- global $user;
- $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d";
- $job = db_fetch_object(db_query($sql, $job_id));
- $args = explode("::", $job->arguments);
- $job_id = tripal_add_job(
- $job->job_name,
- $job->modulename,
- $job->callback,
- $args,
- $user->uid,
- $job->priority);
-
- if ($goto_jobs_page) {
- drupal_goto("admin/tripal/tripal_jobs");
- }
- return $job_id;
- }
- function tripal_jobs_cancel($job_id) {
- $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d";
- $job = db_fetch_object(db_query($sql, $job_id));
-
- if ($job->start_time == 0) {
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->end_time = time();
- $record->status = 'Cancelled';
- $record->progress = '0';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- drupal_set_message(t("Job #%job_id cancelled", array('%job_id' => $job_id)));
- }
- else {
- drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id)));
- }
- drupal_goto("admin/tripal/tripal_jobs");
- }
|