123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?php
- function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid, $priority = 10) {
-
- $args = array();
- if (is_array($arguments)) {
- $args = serialize($arguments);
- }
- $record = new stdClass();
- $record->job_name = $job_name;
- $record->modulename = $modulename;
- $record->callback = $callback;
- $record->status = 'Waiting';
- $record->submit_date = REQUEST_TIME;
- $record->uid = $uid;
- $record->priority = $priority;
- $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_get_job($job_id) {
- $job = db_query('SELECT j.* FROM {tripal_jobs} j WHERE j.job_id=:job_id', array(':job_id' => $job_id))
- ->fetchObject();
- return $job;
- }
- function tripal_is_job_running() {
-
-
-
-
- $sql = "SELECT * FROM {tripal_jobs} TJ " .
- "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
- $jobs = db_query($sql);
- foreach ($jobs as $job) {
- $status = `ps -p $job->pid -o pid=`;
- if ($job->pid && $status) {
-
-
- return TRUE;
- }
- else {
-
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->end_time = REQUEST_TIME;
- $record->status = 'Error';
- $record->error_msg = 'Job has terminated unexpectedly.';
- drupal_write_record('tripal_jobs', $record, 'job_id');
- }
- }
-
- return FALSE;
- }
- function tripal_get_job_start($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_get_job_end($job) {
- if ($job->end_time > 0) {
- $end = format_date($job->end_time);
- }
- else {
- $end = '';
- }
- return $end;
- }
- function tripal_rerun_job($job_id, $goto_jobs_page = TRUE) {
- global $user;
- $user_id = $user->uid;
- $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
- $results = db_query($sql, array(':job_id' => $job_id));
- $job = $results->fetchObject();
-
-
-
-
- $args = unserialize($job->arguments);
- if (!$args) {
- $args = explode("::", $job->arguments);
- }
- $job_id = tripal_add_job($job->job_name, $job->modulename, $job->callback, $args, $user_id, $job->priority);
- if ($goto_jobs_page) {
- drupal_goto("admin/tripal/tripal_jobs");
- }
- return $job_id;
- }
- function tripal_cancel_job($job_id, $redirect = TRUE) {
- $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = :job_id";
- $results = db_query($sql, array(':job_id' => $job_id));
- $job = $results->fetchObject();
-
- if ($job->start_time == 0) {
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->end_time = REQUEST_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)));
- }
- if ($redirect) {
- drupal_goto("admin/tripal/tripal_jobs");
- }
- }
- function tripal_launch_job($do_parallel = 0, $job_id = NULL) {
-
-
-
- if (!$do_parallel and tripal_is_job_running()) {
- print "Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.";
- 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 = :job_id " .
- "ORDER BY priority ASC,job_id ASC";
- $job_res = db_query($sql, array(':job_id' => $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);
- }
- foreach ($job_res as $job) {
-
- $record = new stdClass();
- $record->job_id = $job->job_id;
- $record->start_time = REQUEST_TIME;
- $record->status = 'Running';
- $record->pid = getmypid();
- drupal_write_record('tripal_jobs', $record, 'job_id');
-
-
-
- $callback = $job->callback;
-
-
-
-
- $args = unserialize($job->arguments);
- if (!$args) {
- $args = explode("::", $job->arguments);
- }
- $args[] = $job->job_id;
-
-
-
- $string_args = array();
- foreach ($args as $k => $a) {
- if (is_array($a)) {
- $string_args[$k] = 'Array';
- }
- elseif (is_object($a)) {
- $string_args[$k] = 'Object';
- }
- else {
- $string_args[$k] = $a;
- }
- }
- print "Calling: $callback(" . implode(", ", $string_args) . ")\n";
- call_user_func_array($callback, $args);
-
- $record->end_time = REQUEST_TIME;
- $record->status = 'Completed';
- $record->progress = '100';
- drupal_write_record('tripal_jobs', $record, 'job_id');
-
- }
- }
- function tripal_set_job_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_active_jobs($modulename) {
- $sql = "SELECT * FROM {tripal_jobs} TJ " .
- "WHERE TJ.end_time IS NULL and TJ.modulename = :modulename ";
- $results = db_query($sql, array(':modulename' => $modulename));
- return $results->fetchObject();
- }
- function tripal_get_job_submit_date($job) {
- return format_date($job->submit_date);
- }
|