Further work on the background jobs

- started work on scheduling
- view to add scheduled tasks (still needs work)
- moved cache job bulk-code to the job model from the controller
- bootstrap timepicker
pull/217/head
iglocska 2014-01-02 15:51:41 +01:00
parent 0e744871bd
commit ae23b288e5
7 changed files with 1389 additions and 50 deletions

View File

@ -91,46 +91,17 @@ class JobsController extends AppController {
$target = 'Events visible to: '.$this->Auth->user('org');
$jobOrg = $this->Auth->user('org');
}
$extra = null;
$extra2 = null;
$shell = 'Event';
$this->Job->create();
$data = array(
'worker' => 'cache',
'job_type' => 'cache_' . $type,
'job_input' => $target,
'status' => 0,
'retries' => 0,
'org' => $jobOrg,
'message' => 'Fetching events.',
);
if ($type === 'md5' || $type === 'sha1') {
$extra = $type;
$type = 'hids';
}
if ($type === 'csv_all' || $type === 'csv_sig') {
$extra = $type;
$type = 'csv';
}
if ($type === 'suricata' || $type === 'snort') {
$extra = $type;
$type = 'nids';
$extra2 = $this->Auth->user('sid');
}
$this->Job->save($data);
$id = $this->Job->id;
$process_id = CakeResque::enqueue(
'cache',
$shell . 'Shell',
array('cache' . $type, $this->Auth->user('org'), $this->_isSiteAdmin(), $id, $extra, $extra2),
true
);
$this->Job->saveField('process_id', $process_id);
$id = $this->Job->cache($type, $this->_isSiteAdmin(), $this->Auth->user('org'), $target, $jobOrg);
return new CakeResponse(array('body' => json_encode($id)));
}
public function jobScheduler() {
public function jobScheduler($type) {
if (!$this->_isSiteAdmin()) throw new MethodNotAllowedException();
if ($type === 'cache_exports') $this->$_cacheScheduler();
}
private function _cacheScheduler() {
}
public function stopWorker() {

View File

@ -41,8 +41,47 @@ class TasksController extends AppController {
}
}
public function setTask($id) {
public function setTask() {
if (!$this->_isSiteAdmin()) {
throw new MethodNotAllowedException('You are not authorised to do that.');
}
$today = $this->_getTodaysTimestamp();
if ($this->request->is('post') || $this->request->is('put')) {
$tasks = $this->Task->find('all', array('fields' => array('id', 'timer', 'scheduled_time')));
foreach ($tasks as $k => $task) {
if ($this->request->data['Task'][$task['Task']['id']]['timer'] == $task['Task']['timer']) unset($this->request->data['Task'][$task['Task']['id']]['timer']);
if ($this->request->data['Task'][$task['Task']['id']]['scheduled_time'] == $task['Task']['scheduled_time']) unset($this->request->data['Task'][$task['Task']['id']]['scheduled_time']);
if (empty($this->request->data['Task'][$task['Task']['id']])) {
unset($this->request->data['Task'][$task['Task']['id']]);
} else {
$this->request->data['Task'][$task['Task']['id']]['id'] = $task['Task']['id'];
$this->request->data['Task'][$task['Task']['id']]['next_execution_time'] = strtotime(date("Y-m-d") . ' ' . $this->request->data['Task'][$task['Task']['id']]['scheduled_time']);
if ($this->request->data['Task'][$task['Task']['id']]['next_execution_time'] < time()) {
$this->request->data['Task'][$task['Task']['id']]['next_execution_time'] = strtotime('+1 day', $this->request->data['Task'][$task['Task']['id']]['next_execution_time']);
}
if (!isset($this->request->data['Task'][$task['Task']['id']]['timer'])) $this->request->data['Task'][$task['Task']['id']]['timer'] = $task['Task']['timer'];
$this->Task->save($this->request->data['Task'][$task['Task']['id']]);
// schedule task
if ($this->request->data['Task'][$task['Task']['id']]['timer'] != 0) {
}
}
}
throw new Exception();
/*
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Task edited');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The Task could not be edited. Please, try again.');
}
*/
}
//$this->redirect(array('action' => 'index'));
}
private function _getTodaysTimestamp() {
return strtotime(date("d/m/Y") . ' 00:00:00');
}
}

View File

@ -6,4 +6,43 @@ App::uses('AppModel', 'Model');
* @property Job $Job
*/
class Job extends AppModel {
public function cache($type, $isSiteAdmin, $org, $target, $jobOrg) {
$extra = null;
$extra2 = null;
$shell = 'Event';
$this->create();
$data = array(
'worker' => 'cache',
'job_type' => 'cache_' . $type,
'job_input' => $target,
'status' => 0,
'retries' => 0,
'org' => $jobOrg,
'message' => 'Fetching events.',
);
if ($type === 'md5' || $type === 'sha1') {
$extra = $type;
$type = 'hids';
}
if ($type === 'csv_all' || $type === 'csv_sig') {
$extra = $type;
$type = 'csv';
}
if ($type === 'suricata' || $type === 'snort') {
$extra = $type;
$type = 'nids';
$extra2 = $this->Auth->user('sid');
}
$this->save($data);
$id = $this->id;
$process_id = CakeResque::enqueue(
'cache',
$shell . 'Shell',
array('cache' . $type, $org, $isSiteAdmin, $id, $extra, $extra2),
true
);
$this->saveField('process_id', $process_id);
return $id;
}
}

View File

@ -13,6 +13,7 @@
echo $this->Html->css('roboto');
echo $this->Html->css('bootstrap'); // see http://twitter.github.io/bootstrap/base-css.html
echo $this->Html->css('datepicker');
echo $this->Html->css('bootstrap-timepicker');
echo $this->Html->css('main');
// FIXME chri: re-add print stylesheet
@ -57,6 +58,7 @@
echo $this->element('sql_dump');
echo $this->Html->script('bootstrap');
// echo $this->Html->script('bootstrap.min');
echo $this->Html->script('bootstrap-timepicker');
echo $this->Html->script('bootstrap-datepicker');
echo $this->Html->script('main');
?>

View File

@ -16,37 +16,70 @@
?>
</ul>
</div>
<?php
echo $this->Form->create('Task', array(
'action' => 'setTask',
'controller' => 'Tasks',
'inputDefaults' => array(
'label' => false
)));
?>
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('type');?></th>
<th><?php echo $this->Paginator->sort('timer');?></th>
<th><?php echo $this->Paginator->sort('timer', 'Frequency (h)');?></th>
<th><?php echo $this->Paginator->sort('scheduled_time');?></th>
<th><?php echo $this->Paginator->sort('recurring');?></th>
<th><?php echo $this->Paginator->sort('next_execution_time', 'Next Run');?></th>
<th><?php echo $this->Paginator->sort('description');?></th>
<th class="actions"><?php echo __('Actions');?></th>
<th><?php echo $this->Paginator->sort('job_id', 'Job ID');?></th>
</tr><?php
foreach ($list as $item):?>
<tr>
<td class="short"><?php echo h($item['Task']['id']);?>&nbsp;</td>
<td class="short"><?php echo h($item['Task']['type']);?>&nbsp;</td>
<td class="short"><?php echo h($item['Task']['timer']);?>&nbsp;</td>
<td class="short"><?php echo h($item['Task']['scheduled_time']);?>&nbsp;</td>
<td class="short">
<?php
echo $this->Form->input($item['Task']['id'] . '.timer', array(
'style' => 'display:none',
'class' => 'input-mini',
'default' => h($item['Task']['timer']),
'id' => $item['Task']['id'] . '-timer-active'
));
?>
<div id="<?php echo $item['Task']['id'];?>-timer-passive" onClick="activate1(<?php echo $item['Task']['id'];?>, 'timer')">
<?php echo h($item['Task']['timer']); ?>
</div>
</td>
<td class="short">
<div class="input-append bootstrap-timepicker" id="<?php echo $item['Task']['id'] . '-scheduled_time-active';?>" style="display:none">
<?php
echo $this->Form->input($item['Task']['id'] . '.scheduled_time', array(
'class' => 'input-small',
'type' => 'text',
'default' => h($item['Task']['scheduled_time']),
'id' => 'timepicker' . $item['Task']['id']
));
?>
</div>
<div id="<?php echo $item['Task']['id'];?>-scheduled_time-passive" onClick="activate2(<?php echo $item['Task']['id'];?>, 'scheduled_time', '<?php echo h($item['Task']['scheduled_time']);?>')">
<?php echo h($item['Task']['scheduled_time']); ?>
</div>
</td>
<td class="short">
<?php
if (!$item['Task']['recurring']) echo 'No';
else echo 'Yes';
echo h(date("d/m/Y", $item['Task']['next_execution_time']));
?>
&nbsp;</td>
<td><?php echo h($item['Task']['description']);?>&nbsp;</td>
<td class="short action-links">
<?php echo $this->Html->link('', array('action' => 'setTask', $item['Task']['id']), array('class' => 'icon-edit', 'title' => 'Edit'));?>
</td>
<td class="short"><?php echo $item['Task']['job_id']; ?></td>
</tr><?php
endforeach;?>
endforeach; ?>
</table>
<p>
<?php
echo $this->Form->button('Update all', array('class' => 'btn btn-primary'));
echo $this->Form->end();
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
@ -64,4 +97,16 @@ endforeach;?>
</div>
<?php
echo $this->element('side_menu', array('menuList' => 'task', 'menuItem' => 'index'));
?>
?>
<script type="text/javascript">
function activate1(id, type){
$("#"+id+"-"+type+"-active").show();
$("#"+id+"-"+type+"-passive").hide();
}
function activate2(id, type, defaultValue){
$("#"+id+"-"+type+"-active").show();
$("#"+id+"-"+type+"-passive").hide();
$('#timepicker'+id).timepicker({defaultTime: defaultValue});
}
</script>

View File

@ -0,0 +1,146 @@
/*!
* Timepicker Component for Twitter Bootstrap
*
* Copyright 2013 Joris de Wit
*
* Contributors https://github.com/jdewit/bootstrap-timepicker/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
.bootstrap-timepicker {
position: relative;
}
.bootstrap-timepicker.pull-right .bootstrap-timepicker-widget.dropdown-menu {
left: auto;
right: 0;
}
.bootstrap-timepicker.pull-right .bootstrap-timepicker-widget.dropdown-menu:before {
left: auto;
right: 12px;
}
.bootstrap-timepicker.pull-right .bootstrap-timepicker-widget.dropdown-menu:after {
left: auto;
right: 13px;
}
.bootstrap-timepicker .add-on {
cursor: pointer;
}
.bootstrap-timepicker .add-on i {
display: inline-block;
width: 16px;
height: 16px;
}
.bootstrap-timepicker-widget.dropdown-menu {
padding: 4px;
}
.bootstrap-timepicker-widget.dropdown-menu.open {
display: inline-block;
}
.bootstrap-timepicker-widget.dropdown-menu:before {
border-bottom: 7px solid rgba(0, 0, 0, 0.2);
border-left: 7px solid transparent;
border-right: 7px solid transparent;
content: "";
display: inline-block;
position: absolute;
}
.bootstrap-timepicker-widget.dropdown-menu:after {
border-bottom: 6px solid #FFFFFF;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
content: "";
display: inline-block;
position: absolute;
}
.bootstrap-timepicker-widget.timepicker-orient-left:before {
left: 6px;
}
.bootstrap-timepicker-widget.timepicker-orient-left:after {
left: 7px;
}
.bootstrap-timepicker-widget.timepicker-orient-right:before {
right: 6px;
}
.bootstrap-timepicker-widget.timepicker-orient-right:after {
right: 7px;
}
.bootstrap-timepicker-widget.timepicker-orient-top:before {
top: -7px;
}
.bootstrap-timepicker-widget.timepicker-orient-top:after {
top: -6px;
}
.bootstrap-timepicker-widget.timepicker-orient-bottom:before {
bottom: -7px;
border-bottom: 0;
border-top: 7px solid #999;
}
.bootstrap-timepicker-widget.timepicker-orient-bottom:after {
bottom: -6px;
border-bottom: 0;
border-top: 6px solid #ffffff;
}
.bootstrap-timepicker-widget a.btn,
.bootstrap-timepicker-widget input {
border-radius: 4px;
}
.bootstrap-timepicker-widget table {
width: 100%;
margin: 0;
}
.bootstrap-timepicker-widget table td {
text-align: center;
height: 30px;
margin: 0;
padding: 2px;
}
.bootstrap-timepicker-widget table td:not(.separator) {
min-width: 30px;
}
.bootstrap-timepicker-widget table td span {
width: 100%;
}
.bootstrap-timepicker-widget table td a {
border: 1px transparent solid;
width: 100%;
display: inline-block;
margin: 0;
padding: 8px 0;
outline: 0;
color: #333;
}
.bootstrap-timepicker-widget table td a:hover {
text-decoration: none;
background-color: #eee;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
border-color: #ddd;
}
.bootstrap-timepicker-widget table td a i {
margin-top: 2px;
font-size: 18px;
}
.bootstrap-timepicker-widget table td input {
width: 25px;
margin: 0;
text-align: center;
}
.bootstrap-timepicker-widget .modal-content {
padding: 4px;
}
@media (min-width: 767px) {
.bootstrap-timepicker-widget.modal {
width: 200px;
margin-left: -100px;
}
}
@media (max-width: 767px) {
.bootstrap-timepicker {
width: 100%;
}
.bootstrap-timepicker .dropdown-menu {
width: 100%;
}
}

File diff suppressed because it is too large Load Diff