protected function _delete() { $ids = $this->getUsersToBeNotifiedOfPresence(); if (!empty($ids)) { // Announce presence to all online friends $eventTable = Engine_Api::_()->getDbtable('events', 'chat'); foreach ($ids as $id) { $eventTable->insert(array('user_id' => $id, 'date' => date('Y-m-d H:i:s'), 'type' => 'presence', 'body' => array('user_id' => $this->user_id, 'state' => '0'))); } // Increment event count for each user Engine_Api::_()->getDbtable('users', 'chat')->update(array('event_count' => new Zend_Db_Expr('event_count+1')), array('user_id IN(\'' . join("', '", $ids) . '\')')); // Do rooms now foreach ($this->getRoomUsers() as $roomuser) { $roomuser->delete(); } } parent::_delete(); }
/** * Post-insert hook. If overridden, should be called at end of function. * * @return void */ protected function _postDelete() { if ($this->_disableHooks) { return; } parent::_postDelete(); Engine_Hooks_Dispatcher::getInstance()->callEvent('on' . $this->getType(true) . 'DeleteAfter', array('type' => $this->getType(), 'identity' => $this->getIdentity())); Engine_Hooks_Dispatcher::getInstance()->callEvent('onItemDeleteAfter', array('type' => $this->getType(), 'identity' => $this->getIdentity())); }
protected function _executeTaskCheck(Engine_Db_Table_Row $task) { // We've executed at least as many tasks as count if ($this->_runCount >= $this->_config['count']) { return false; } // We've reached the time limit for this request if (microtime(true) >= _ENGINE_REQUEST_START + $this->_config['time']) { return false; } // Task is not ready to be executed again yet if ($task->timeout > 0) { if (time() < $task->started_last + $task->timeout) { return false; } if (time() < $task->completed_last + $task->timeout) { return false; } } // If semaphore limit is reached, and the timeout // has been reached, check if lock needs to be cleared if ($task->semaphore >= $task->processes) { // Sanity - wth is this? if ($task->processes < 1) { $task->processes = 1; $task->save(); return false; } // Get all processes matching task plugin $taskProcesses = $this->getAdapter()->select()->from('engine4_core_processes')->where('name = ?', $task->plugin)->query()->fetchAll(); // There was nothing, flush mutexes if (empty($taskProcesses)) { $affected = $this->update(array('semaphore' => new Zend_Db_Expr(sprintf('semaphore - %d', $task->semaphore))), array('task_id = ?' => $task->task_id)); if (1 !== $affected) { // Log if (APPLICATION_ENV == 'development') { $this->getLog()->log(sprintf('Execution Mutex Flush Failed [%d] : %s', $this->_pid, $task->title), Zend_Log::DEBUG); } return false; } } else { $activeProcesses = 0; foreach ($taskProcesses as $process) { $started = !empty($process['started']) ? $process['started'] : 0; $timeout = !empty($process['timeout']) ? $process['timeout'] : $this->_config['timeout']; // It's timed out if (time() > $started + $timeout) { // Delete $this->getAdapter()->delete('engine4_core_processes', array('pid' => $process['pid'])); // Log if (APPLICATION_ENV == 'development') { $this->getLog()->log(sprintf('Process Timeout [%d] : %d ', $this->_pid, $process['pid']), Zend_Log::DEBUG); } continue; } $activeProcesses++; } if ($activeProcesses >= $task->processes) { // Log if (APPLICATION_ENV == 'development') { $this->getLog()->log(sprintf('Execution Process Flush Failed [%d] : %d ', $this->_pid, $activeProcesses), Zend_Log::DEBUG); } return false; } } } // Task is ready return true; }
public function shouldTaskExecute(Engine_Db_Table_Row $task, $refresh = true, $isAutomatic = false) { // Refresh to check if run in separate thread if ($refresh) { $task->refresh(); } // Don't run it if it's disabled if (!$task->enabled || $task->type == 'disabled') { return false; } // Don't start manual tasks in automatic mode when they're dormant if (in_array($task->type, array('manual', 'semi-automatic')) && $task->state == 'dormant') { return false; } // Don't start executing tasks again unless they have been executing for > 15 minutes // We assume that 15 min means they have died and failed to cancel executing status if ($task->executing || $task->state == 'active') { if (time() > $task->started_last + $this->getTimeout()) { // Update the task, assuming it has timed out $newState = $task->type == 'semi-automatic' ? 'sleeping' : 'dormant'; $task->getTable()->update(array('state' => $newState, 'executing' => false, 'executing_id' => 0), array('task_id = ?' => $task->task_id, 'state = ?' => $task->state, 'executing = ?' => $task->executing, 'executing_id = ?' => $task->executing_id)); } return false; } // Tasks is not ready to be executed again yet if ($task->timeout > 0) { if (time() < $task->started_last + $task->timeout) { return false; } if (time() < $task->completed_last + $task->timeout) { return false; } } // Task is dormant if ($task->state == 'dormant') { // Automatic task is ready if ($task->type == 'automatic') { $task->getTable()->update(array('state' => 'ready'), array('task_id = ?' => $task->task_id, 'state = ?' => $task->state)); } return false; } // Sanity check if ($task->state != 'ready' && $task->state != 'sleeping') { $this->getLog()->log('Weird task state: ' . $task->state, Zend_Log::WARN); return false; } // Task is ready return true; }
protected function _delete() { // Decrement room user count $room = $this->getRoom(); //$room->user_count--; if (null !== $room) { $room->user_count = new Zend_Db_Expr('user_count - 1'); $room->save(); // Announce loss of prescence $ids = $this->getRoom()->getUserIds(); // Remove self if (false !== ($index = array_search($this->user_id, $ids))) { unset($ids[$index]); } if (!empty($ids)) { $eventTable = Engine_Api::_()->getDbtable('events', 'chat'); foreach ($ids as $id) { $eventTable->createRow()->setFromArray(array('user_id' => $id, 'date' => date('Y-m-d H:i:s'), 'type' => 'grouppresence', 'body' => array('room_id' => $this->room_id, 'user_id' => $this->user_id, 'state' => '0')))->save(); } // Increment event count for each user Engine_Api::_()->getDbtable('users', 'chat')->update(array('event_count' => new Zend_Db_Expr('event_count+1')), array('user_id IN(\'' . join("', '", $ids) . '\')')); } } parent::_delete(); }