Skip to content

Commit

Permalink
cron added
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav-Andryushchenkov committed Jun 16, 2014
1 parent ffc70aa commit d9e5e68
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
4 changes: 2 additions & 2 deletions amcr/amcr.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,10 @@ function amcr_report_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/services/amcr/';

if (isset($form_state['values']['id'])) {
drupal_set_message(t('The report %report has been updated.', array('%report' => $form_state['values']['name'])));
drupal_set_message(t('The report config %report has been updated.', array('%report' => $form_state['values']['name'])));
}
else {
drupal_set_message(t('The report %report has been added.', array('%report' => $form_state['values']['name'])));
drupal_set_message(t('The report config %report has been added.', array('%report' => $form_state['values']['name'])));
}
}

Expand Down
58 changes: 47 additions & 11 deletions amcr/amcr.module
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,7 @@ function amcr_menu() {
* Page callbac, form amcr menu item
*/
function amcr_test_page() {
try {
$report_config = new AmcrReportConfig(11);
$data_source = new AmcrReport($report_config);
$data_source->getData();
$data_source->save();
}
catch (Exception $e) {
watchdog_exception('amcr', $e);
return $e->getMessage();
}

_amcr_update_report(11);
return 'Report completed';

}
Expand Down Expand Up @@ -134,4 +124,50 @@ function amcr_report_load($id) {
return $reports[$id];
}

/**
* Implements hook_cron
*/
function amcr_cron() {
$result = db_query('SELECT * FROM {amcr_report_description} WHERE last_update + update_frequency < :time',
array(':time' => REQUEST_TIME));

$queue = DrupalQueue::get('amcr_reports');
foreach ($result as $report) {
$queue->createItem($report->id);
}
}

/**
* Implements hook_cron_queue_info
*/
function amcr_cron_queue_info() {
$queues['amcr_reports'] = array(
'worker callback' => '_amcr_update_report',
'time' => 120,
);
return $queues;
}

/**
* Callback for hook_cron_queue_info
* updates report
*
* @param $rd_id int
* Primary keq of amcr_report_description table (config id)
*/
function _amcr_update_report($rd_id) {
try {
$report_config = new AmcrReportConfig($rd_id);
$data_source = new AmcrReport($report_config);
$data_source->getData();
$data_source->save();
db_update('{amcr_report_description}')
->condition('id', $rd_id)
->fields(array('last_update' => time()))
->execute();
watchdog('amcr','report '.$report_config->name. ' updated', array(), WATCHDOG_INFO);
}
catch (Exception $e) {
watchdog_exception('amcr', $e);
}
}
4 changes: 2 additions & 2 deletions amcr/amcr.report.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ class AmcrReportConfig {
}

if (isset($this->id)){
watchdog('amcr', 'Report %report updated. Report Id is: %id',
watchdog('amcr', 'Report config %report updated. Report Id is: %id',
array('%report' => $this->name,'%id' => $this->id ));
}
else {
watchdog('amcr', 'Report %report added. Report id is %id',
watchdog('amcr', 'Report config %report added. Report id is %id',
array('%report' => $this->name,
'%id' => $this->id),
WATCHDOG_NOTICE, l(t('view'), 'admin/config/services/amcr'));
Expand Down

0 comments on commit d9e5e68

Please sign in to comment.