protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', 'dev', true)); parent::execute($arguments, $options); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $con = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $this->log('[INFO] Flushing old data...' . "\n"); $eventlog_flush = EtvaSettingPeer::retrieveByPK('eventlog_flush'); $flush_range = $eventlog_flush->getValue(); $con->beginTransaction(); $affected = 0; try { $offset_date = date("c", time() - $flush_range * 24 * 60 * 60); /* * get event records that have creation date outdated */ $c = new Criteria(); $c->add(EtvaEventPeer::CREATED_AT, $offset_date, Criteria::LESS_THAN); $affected = EtvaEventPeer::doDelete($c, $con); $con->commit(); $message = sprintf('Events Log - %d Record(s) deleted after %d day offset', $affected, $flush_range); $context->getEventDispatcher()->notify(new sfEvent(sfConfig::get('config_acronym'), 'event.log', array('message' => $message))); $this->log('[INFO] ' . $message); } catch (PropelException $e) { $con->rollBack(); throw $e; } $logger = new sfFileLogger($context->getEventDispatcher(), array('file' => sfConfig::get("sf_log_dir") . '/cron_status.log')); // log the message! $logger->log("[INFO] The events flush task ran!", 6); }
protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', $options['env'], true)); parent::execute($arguments, $options); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $con = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $this->log('[INFO]Checking node(s) virtual machines state...' . "\n"); // get nodes active $updated_vm_state = array(); $nodes = EtvaNodeQuery::create()->filterByState(EtvaNode::NODE_ACTIVE)->find(); $affected = 0; foreach ($nodes as $node) { $response = $node->soapSend('domains_stats'); $success = $response['success']; if ($success) { $returned_data = $response['response']; foreach ($returned_data as $i => $server) { $server_data = (array) $server; $etva_server = $node->retrieveServerByName($server_data['name']); if ($etva_server) { $server_id = $etva_server->getId(); // get server id if ($etva_server->getVmState() !== EtvaServer::STATE_MIGRATING) { // only if not in migrating mode $etva_server->setVmState($server_data['state']); $etva_server->save(); } $updated_vm_state[$server_id] = $etva_server->getVmState(); // mark as updated } } } else { $affected++; $errors[] = $response['error']; } } // change state as STOP for servers that are not in array EtvaServerQuery::create()->filterById(array_keys($updated_vm_state), Criteria::NOT_IN)->update(array('VmState' => EtvaServer::STATE_STOP)); if ($nodes) { $message = sprintf('%d Node(s) could not be checked for virtual machines state', $affected); if ($affected > 0) { $context->getEventDispatcher()->notify(new sfEvent(sfConfig::get('config_acronym'), 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR))); } } if (!empty($errors)) { $this->log('[INFO]' . $message); } $logger = new sfFileLogger($context->getEventDispatcher(), array('file' => sfConfig::get("sf_log_dir") . '/cron_status.log')); // log the message! $logger->log("The check virtual machines state task ran!", 6); }
if (file_exists($file)) { unlink($file); } $dispatcher = new sfEventDispatcher(); // ->initialize() $t->diag('->initialize()'); try { $logger = new sfFileLogger($dispatcher); $t->fail('->initialize() parameters must contains a "file" parameter'); } catch (sfConfigurationException $e) { $t->pass('->initialize() parameters must contains a "file" parameter'); } // ->log() $t->diag('->log()'); $logger = new sfFileLogger($dispatcher, array('file' => $file)); $logger->log('foo'); $lines = explode("\n", file_get_contents($file)); $t->like($lines[0], '/foo/', '->log() logs a message to the file'); $logger->log('bar'); $lines = explode("\n", file_get_contents($file)); $t->like($lines[1], '/bar/', '->log() logs a message to the file'); class TestLogger extends sfFileLogger { public function getTimeFormat() { return $this->timeFormat; } protected function getPriority($priority) { return '*' . $priority . '*'; }
protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance(sfProjectConfiguration::getApplicationConfiguration('app', 'dev', true)); parent::execute($arguments, $options); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $con = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here $this->log('[INFO] Checking VirtAgents state...' . "\n"); $offset = sfConfig::get('app_node_keepalive_update') + sfConfig::get('app_node_keepalive_update_offset'); $total_nodes = EtvaNodePeer::doCount(new Criteria()); if ($total_nodes > 0) { $con->beginTransaction(); $affected = 0; try { $offset_date = date("c", time() - $offset); /* * get nodes that have last_keepalive field outdated */ $c1 = new Criteria(); $c1->add(EtvaNodePeer::LAST_KEEPALIVE, $offset_date, Criteria::LESS_THAN); $c1->add(EtvaNodePeer::STATE, EtvaNode::NODE_ACTIVE); // only active //update statement $c2 = new Criteria(); $c2->add(EtvaNodePeer::STATE, EtvaNode::NODE_INACTIVE); $affected += BasePeer::doUpdate($c1, $c2, $con); // update maintenance and running to maintenance $c1 = new Criteria(); $c1->add(EtvaNodePeer::LAST_KEEPALIVE, $offset_date, Criteria::LESS_THAN); $c1->add(EtvaNodePeer::STATE, EtvaNode::NODE_MAINTENANCE_UP); //update statement $c2 = new Criteria(); $c2->add(EtvaNodePeer::STATE, EtvaNode::NODE_MAINTENANCE); $affected += BasePeer::doUpdate($c1, $c2, $con); // update fail and running to fail $c1 = new Criteria(); $c1->add(EtvaNodePeer::LAST_KEEPALIVE, $offset_date, Criteria::LESS_THAN); $c1->add(EtvaNodePeer::STATE, EtvaNode::NODE_FAIL_UP); // only active //update statement $c2 = new Criteria(); $c2->add(EtvaNodePeer::STATE, EtvaNode::NODE_FAIL); $affected += BasePeer::doUpdate($c1, $c2, $con); $con->commit(); $message = sprintf('%d Node(s) NOT received update in time offset of %d seconds', $affected, $offset); if ($affected > 0) { $context->getEventDispatcher()->notify(new sfEvent(sfConfig::get('config_acronym'), 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR))); } $this->log('[INFO]' . $message); } catch (PropelException $e) { $con->rollBack(); throw $e; } } $logger = new sfFileLogger($context->getEventDispatcher(), array('file' => sfConfig::get("sf_log_dir") . '/cron_status.log')); // log the message! $logger->log("[INFO] The check virtAgents task ran!", 6); }