Improve job daemon to update dashboard

This commit is contained in:
Eike Foken
2011-09-15 20:17:35 +02:00
parent 766f8c95f9
commit c6c7e3c629
5 changed files with 64 additions and 10 deletions

View File

@@ -61,13 +61,23 @@ class Ajax extends CI_Controller {
$unseen = $this->job->getUnseenResults(); $unseen = $this->job->getUnseenResults();
foreach ($unseen as $job) { foreach ($unseen as $job) {
$this->job->update($job['id'], array('seen' => 1)); if ($job['notified'] == 0) {
$this->job->update($job['id'], array('notified' => 1));
$experiment = anchor('experiments/detail/' . $job['experiment_id'], $job['experiment_name']); $experiment = anchor('experiments/detail/' . $job['experiment_id'], $job['experiment_name']);
$project = anchor('projects/detail/' . $job['project_id'], $job['project_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'); $this->messages->add(sprintf(_('The job for %s in project %s is ready.'), $experiment, $project), 'success');
}
} }
$data = array(
'jobs_finished' => count($unseen),
'jobs_running' => $this->job->countRunning(),
'jobs_pending' => $this->job->countPending(),
);
$this->output->set_output(json_encode($data));
} }
/** /**

View File

@@ -50,26 +50,30 @@ class Dashboard extends CI_Controller {
$tpl['recent_buttons'] = array( $tpl['recent_buttons'] = array(
array( array(
'count' => 4, 'count' => $this->job->countUnseenResults(),
'text' => _('Jobs finished'), 'text' => _('Jobs finished'),
'id' => 'jobs_finished',
'title' => sprintf(_('%d jobs finished recently'), 3), 'title' => sprintf(_('%d jobs finished recently'), 3),
'target' => '#', 'target' => '#',
), ),
array( array(
'count' => 2, 'count' => 0,
'text' => _('Newly shared projects'), 'text' => _('Newly shared projects'),
'id' => 'shared_projects',
'title' => sprintf(_('You were invited to join %d projects'), 2), 'title' => sprintf(_('You were invited to join %d projects'), 2),
'target' => '#', 'target' => '#',
), ),
array( array(
'count' => 1, 'count' => $this->job->countRunning(),
'text' => _('Job running'), 'text' => _('Job running'),
'id' => 'jobs_running',
'title' => sprintf(_('There is %d job currently running'), 1), 'title' => sprintf(_('There is %d job currently running'), 1),
'target' => '#', 'target' => '#',
), ),
array( array(
'count' => 2, 'count' => $this->job->countPending(),
'text' => _('Jobs pending'), 'text' => _('Jobs pending'),
'id' => 'jobs_pending',
'title' => sprintf(_('There are %2 job currently pending'), 1), 'title' => sprintf(_('There are %2 job currently pending'), 1),
'target' => '#', 'target' => '#',
), ),

View File

@@ -129,6 +129,14 @@ class Job extends CI_Model {
return $this->db->get('jobs')->result_array(); return $this->db->get('jobs')->result_array();
} }
/**
*
*/
public function getUnnotifiedResults() {
$this->db->where('notified', 0);
return $this->getUnseenResults();
}
/** /**
* Gets a list of jobs that have not yet started running. * Gets a list of jobs that have not yet started running.
* *
@@ -138,6 +146,33 @@ class Job extends CI_Model {
$query = $this->db->order_by('created_at', 'asc')->get_where('jobs', array('started_at' => '0000-00-00 00:00:00'), 1); $query = $this->db->order_by('created_at', 'asc')->get_where('jobs', array('started_at' => '0000-00-00 00:00:00'), 1);
return $this->db->count_all_results() > 0 ? $query->row() : FALSE; return $this->db->count_all_results() > 0 ? $query->row() : FALSE;
} }
/**
* Counts all unseen jobs.
*
* @return integer
*/
public function countUnseenResults() {
return $this->db->where(array('finished_at !=' => 0, 'seen' => 0))->count_all_results('jobs');
}
/**
* Counts all running jobs.
*
* @return integer
*/
public function countRunning() {
return $this->db->where(array('started_at !=' => 0, 'finished_at' => 0))->count_all_results('jobs');
}
/**
* Counts all pending jobs.
*
* @return integer
*/
public function countPending() {
return $this->db->where('started_at', 0)->count_all_results('jobs');
}
} }
/* End of file job.php */ /* End of file job.php */

View File

@@ -46,7 +46,7 @@
} else { } else {
$button['class'] = 'middle'; $button['class'] = 'middle';
} }
?><a class="button <?=$button['class'];?> big" href="<?=$button['target'];?>" title="<?=$button['title'];?>"><strong><?=$button['count'];?></strong><br /><?=$button['text'];?></a><? ?><a class="button <?=$button['class'];?> big" id="<?=$button['id'];?>" href="<?=$button['target'];?>" title="<?=$button['title'];?>"><strong><?=$button['count'];?></strong><br /><?=$button['text'];?></a><?
$i++; $i++;
endforeach; endforeach;
endif; endif;

View File

@@ -13,7 +13,11 @@ function getNotifications() {
* Looks for finished jobs. * Looks for finished jobs.
*/ */
function jobDaemon() { function jobDaemon() {
$.get(SITE_URL + 'ajax/check_jobs'); $.getJSON(SITE_URL + 'ajax/check_jobs', function(data) {
$.each(data, function(key, value) {
$('#' + key).find('strong').html(value);
});
});
} }
/** /**
@@ -128,6 +132,7 @@ $(document).ready(function() {
$('#notifications').hide(); $('#notifications').hide();
getNotifications(); getNotifications();
setInterval('getNotifications()', '5000'); setInterval('getNotifications()', '5000');
jobDaemon();
setInterval('jobDaemon()', JOBS_CHECK_INTERVAL * 1000); setInterval('jobDaemon()', JOBS_CHECK_INTERVAL * 1000);
/* /*