/** * (non-PHPdoc) * @see util/Phirehose::log() */ protected function log($message) { if (!isset($this->db)) { $this->db = Storage::getDatabase(); } $this->db->log('Phirehose', $message); }
/** * @see Action::run() */ public function run() { $this->db = Storage::getDatabase(); // First, figure out how many tweets there are in the whole database. $query = 'SELECT COUNT(*) FROM `' . DB_NAME . '`.`' . POST_TABLE . '`'; $this->db->setQuery($query); $result = $this->db->query(); $numPosts = $result[0]['COUNT(*)']; $toDelete = 0; if ($this->maxAllowed > 0) { if ($this->maxAllowed > $numPosts) { return parent::SUCCESS; } $toDelete = $numPosts - $this->maxAllowed; } else { if ($this->number > 0) { if ($this->number > $numPosts) { return parent::SUCCESS; } // Delete this number of posts. $toDelete = $this->number; } else { $toDelete = intval($this->percent * (double) $numPosts); } } $query = 'DELETE FROM `' . DB_NAME . '`.`' . POST_TABLE . '` ORDER BY ' . '`date_saved` ASC LIMIT ' . $toDelete; $this->db->setQuery($query); $this->db->query(); $this->db->log($this->getName(), $toDelete . ' tweet' . ($toDelete != 1 ? 's' : '') . ' deleted!'); return parent::SUCCESS; }
/** * Overridden from parent class' declaration. Since this action relies * on a probability distribution to determine its next firing, it is not * a simple addition of terms. Thus, for this class, the "frequency" field * is ignored and replaced with "delayMean" and "delayVar". * * @see Action::setNextAttempt() */ public function setNextAttempt() { $mean = floatval($this->delayMean); $var = floatval($this->delayVar); $rand1 = floatval(mt_rand()) / floatval(mt_getrandmax()); $rand2 = floatval(mt_rand()) / floatval(mt_getrandmax()); // sample from a normal (gaussian) distribution $delay = intval(sqrt(-2 * log($rand1)) * cos(2 * pi() * $rand2) * $var + $mean); if ($delay <= 0) { $delay = 1; } // sanity check $this->nextAttempt = time() + intval($delay * 60); // log the next attempt $this->db = Storage::getDatabase(); $this->db->log($this->getName(), 'Next action firing set for ' . date('Y-m-d H:i:s', $this->nextAttempt)); unset($this->db); }
<?php define('TWITTERBOT', 1); // script for installing the twitterbot's SQL back-end // NOTE: only run this AFTER setting up config.php! include_once 'config.php'; include_once 'action.php'; include_once 'storage.php'; // first, set up any tables needed for the core install $db = Storage::getDatabase(); $sql = 'CREATE TABLE IF NOT EXISTS `' . DB_NAME . '`.`' . POST_TABLE . '` (' . '`postid` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,' . '`text` VARCHAR( 140 ) NOT NULL ,' . '`user` VARCHAR( 50 ) NOT NULL ,' . '`date_saved` DATETIME NOT NULL ,' . '`modeled` SMALLINT NOT NULL DEFAULT \'0\')'; $db->setQuery($sql); $db->query(); $sql = 'CREATE TABLE IF NOT EXISTS `' . DB_NAME . '`.`' . LOG_TABLE . '` (' . '`eventid` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,' . '`eventtime` DATETIME NOT NULL ,' . '`message` TEXT NOT NULL)'; $db->setQuery($sql); $db->query();
/** * Signal handler for child processes that have exited via SIGCHLD. * @param int $signal */ private function sig_child($signal) { $status = Action::FAILURE; while (($pid = pcntl_wait($status, WNOHANG)) > 0) { $action = $this->current[$pid]; unset($this->current[$pid]); if (pcntl_wifexited($status) && pcntl_wexitstatus($status) == Action::SUCCESS) { $status = Action::SUCCESS; } if ($action != $this->aggregator) { $action->post_run($status); } else { // the aggregator failed! this is a problem $db = Storage::getDatabase(); $db->log('Twitterbot', 'Aggregator crashed! Exiting.'); unset($db); exit; } } }
/** * This method can be called after the run() method to perform * post-processing. * * In this case, it logs any failures (if the run() return value is * Action::FAILURE) and saves the necessary values to the database. * @param int $status The return code from the child process exiting. */ public function post_run($status) { $this->db = Storage::getDatabase(); // log the status of this action if ($status !== $this->currentStatus) { $this->previousStatus = $this->currentStatus; } if ($status === self::FAILURE) { if ($this->currentStatus === self::FAILURE) { // failed consecutive times $this->db->log($this->name, "Still have not recovered from previous" . "error!"); } else { // this is the first time the action has failed $this->db->log($this->name, "Error has occurred!"); } } else { // Action::SUCCESS. Log this only if the previous status // was Action::FAILURE, so we know we've recovered from something. if ($this->previousStatus === Action::FAILURE) { $this->db->log($this->name, "Recovered from previous failure."); } else { $this->db->log($this->name, "Successful run!"); } } // set the current status $this->currentStatus = $status; // destroy the database connection unset($this->db); }