From 672f1a5186046ca3f297f0fede1ee57e6903d539 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Thu, 15 Sep 2011 18:48:58 +0200 Subject: [PATCH] Add daemon for checking finished jobs --- application/controllers/ajax.php | 17 +++++++++++++++++ application/models/job.php | 16 +++++++++------- application/views/header.php | 1 + assets/js/scattport.js | 8 ++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/application/controllers/ajax.php b/application/controllers/ajax.php index 3408f54..6d73ec9 100644 --- a/application/controllers/ajax.php +++ b/application/controllers/ajax.php @@ -53,6 +53,23 @@ class Ajax extends CI_Controller { $this->load->view('global/notifications', $tpl); } + /** + * + */ + public function check_jobs() { + $this->load->model('job'); + $unseen = $this->job->getUnseenResults(); + + foreach ($unseen as $job) { + $this->job->update($job['id'], array('seen' => 1)); + + $experiment = anchor('experiments/detail/' . $job['experiment_id'], $job['experiment_name']); + $project = anchor('projects/detail/' . $job['project_id'], $job['project_name']); + + $this->messages->add(sprintf(_('The job for %s in project %s is ready.'), $experiment, $project), 'success'); + } + } + /** * Saves the projects description. * diff --git a/application/models/job.php b/application/models/job.php index c3ac190..4de6875 100644 --- a/application/models/job.php +++ b/application/models/job.php @@ -116,15 +116,17 @@ class Job extends CI_Model { * @return array */ public function getUnseenResults() { - $query = $this->db->order_by('started_at', 'asc') - ->get_where('jobs', array('started_by' => $this->session->userdata('user_id'), 'seen' => '0')); - $jobs = $query->result_array(); + $this->db->select('jobs.*, experiments.name AS experiment_name, projects.id AS project_id, projects.name AS project_name'); + $this->db->join('experiments', 'experiments.id = jobs.experiment_id', 'left'); + $this->db->join('projects', 'projects.id = experiments.project_id', 'left'); - for($i=0; $idb->select('name')->get_where('projects', array('id' => $jobs[$i]['project_id']))->row()->name; - } + $this->db->where('started_by', $this->session->userdata('user_id')); + $this->db->where('finished_at !=', 0); + $this->db->where('seen', 0); - return $jobs; + $this->db->order_by('started_at ASC'); + + return $this->db->get('jobs')->result_array(); } /** diff --git a/application/views/header.php b/application/views/header.php index 15fd6f3..eb3990b 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -25,6 +25,7 @@ diff --git a/assets/js/scattport.js b/assets/js/scattport.js index 563381b..d5acea7 100644 --- a/assets/js/scattport.js +++ b/assets/js/scattport.js @@ -9,6 +9,13 @@ function getNotifications() { }); } +/** + * Looks for finished jobs. + */ +function jobDaemon() { + $.get(SITE_URL + 'ajax/check_jobs'); +} + /** * Sets a cookie. * @@ -121,6 +128,7 @@ $(document).ready(function() { $('#notifications').hide(); getNotifications(); setInterval('getNotifications()', '5000'); + setInterval('jobDaemon()', JOBS_CHECK_INTERVAL * 1000); /* * Tables