results, and returns the observers to be updated (meant to be executed from a cron task), indexed by id_user.
function()
{
if ( $this->check() ){
$now = date('Y-m-d H:i:s');
$rows = $this->db->rselectAll([
'table' => 'bbn_observers',
'fields' => ['id', 'id_user', 'request', 'params', 'result', 'frequency'],
'where' => [
'conditions' => [
[
'field' => 'id_alias',
'operator' => 'isnull'
], [
'field' => 'next',
'operator' => '<',
'value' => $now
]
]
]
]);
$diff = [];
$timer = new \bbn\Util\Timer();
foreach ( $rows as $d ){
if ($this->checkObserver($d)) {
// Aliases are the IDs of the observers aliases of the current row
$timer->start('exec');
$aliases = $this->db->rselectAll('bbn_observers', ['id', 'id_user', 'request', 'params', 'result'], ['id_alias' => $d['id']]);
if ( \bbn\Str::isJson($d['request']) ){
$d['request'] = json_decode($d['request'], true);
$real_result = $this->_exec_array($d['request']);
}
else{
$real_result = $this->_exec($d['request'], $d['params']);
}
X::log([$timer->stop('exec'), $this->db->last()], 'obs_request');
// If the result is different we update the table
if ( $real_result !== $d['result'] ){
echo '+';
$this->db->update('bbn_observers', ['result' => $real_result], ['id' => $d['id']]);
// And if a user is attached to the observer we add it to the result
if ( $d['id_user'] ){
if ( !isset($diff[$d['id_user']]) ){
$diff[$d['id_user']] = [];
}
$diff[$d['id_user']][] = [
'id' => $d['id'],
'result' => $real_result
];
}
// For each alias we add the entry for the corresponding user...
foreach ( $aliases as $a ){
// ...If the result differs
if ( $real_result !== $a['result'] ){
$this->db->update('bbn_observers', ['result' => $real_result], ['id' => $a['id']]);
if ( !isset($diff[$a['id_user']]) ){
$diff[$a['id_user']] = [];
}
$diff[$a['id_user']][] = [
'id' => $a['id'],
'result' => $real_result
];
}
}
}
}
// And we update the next time of execution
$this->_update_next($d['id'], $d['frequency']);
}
echo '.';
$this->deleteOld();
$this->db->flush();
if ( count($diff) ){
bbn\X::dump('Returning diff!', $diff);
return $diff;
}
if ( ob_get_contents() ){
ob_end_flush();
}
return true;
}
bbn\X::dump('Canceling observer: '.date('H:i:s Y-m-d'));
return false;
}