Add daemon for checking finished jobs

This commit is contained in:
Eike Foken
2011-09-15 18:48:58 +02:00
parent 1496c44505
commit 672f1a5186
4 changed files with 35 additions and 7 deletions

View File

@@ -53,6 +53,23 @@ class Ajax extends CI_Controller {
$this->load->view('global/notifications', $tpl); $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. * Saves the projects description.
* *

View File

@@ -116,15 +116,17 @@ class Job extends CI_Model {
* @return array * @return array
*/ */
public function getUnseenResults() { public function getUnseenResults() {
$query = $this->db->order_by('started_at', 'asc') $this->db->select('jobs.*, experiments.name AS experiment_name, projects.id AS project_id, projects.name AS project_name');
->get_where('jobs', array('started_by' => $this->session->userdata('user_id'), 'seen' => '0')); $this->db->join('experiments', 'experiments.id = jobs.experiment_id', 'left');
$jobs = $query->result_array(); $this->db->join('projects', 'projects.id = experiments.project_id', 'left');
for($i=0; $i<count($jobs); $i++) { $this->db->where('started_by', $this->session->userdata('user_id'));
$jobs[$i]['project_name'] = $this->db->select('name')->get_where('projects', array('id' => $jobs[$i]['project_id']))->row()->name; $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();
} }
/** /**

View File

@@ -25,6 +25,7 @@
<script type="text/javascript"> <script type="text/javascript">
var SITE_URL = '<?=site_url("/");?>'; var SITE_URL = '<?=site_url("/");?>';
var BASE_URL = '<?=base_url("/");?>'; var BASE_URL = '<?=base_url("/");?>';
var JOBS_CHECK_INTERVAL = <?=$this->access->settings('jobs_check_interval');?>;
</script> </script>
</head> </head>

View File

@@ -9,6 +9,13 @@ function getNotifications() {
}); });
} }
/**
* Looks for finished jobs.
*/
function jobDaemon() {
$.get(SITE_URL + 'ajax/check_jobs');
}
/** /**
* Sets a cookie. * Sets a cookie.
* *
@@ -121,6 +128,7 @@ $(document).ready(function() {
$('#notifications').hide(); $('#notifications').hide();
getNotifications(); getNotifications();
setInterval('getNotifications()', '5000'); setInterval('getNotifications()', '5000');
setInterval('jobDaemon()', JOBS_CHECK_INTERVAL * 1000);
/* /*
* Tables * Tables