Updating the Server status.
The previous post allow us to create a new updater class and run it from the command line:
It also talked about the initialisation of the ServerUpdate class using dependency injection as a Symfony service.
This post documents the actual update process, and involves 2 classes:
- ServerUpdate - which uses the UpdaterStatus class to check the status for all required servers.
- UpdaterStatus - updates a status for one particular server, it also provides a notify method to do the required notification.
At first thought much of the ServerUpdate code could be included in the UpdateCommand class - however the Symfony documentation for the console class reccommends that the command class do very little other than check any required parameters - for this application at least this is a very good reason - as an update can be initiated from the update action in the default Controller.
Much of this code is a straight port (read copy and paste) from the excellent code in phpServerMon application.
ServerUpdate Class.
This class has an update method: called from the updateCommand or the updateAction:
$serverUpdate = $this->container =
$this->getApplication()->getKernel()->getContainer()->get('server_update');
$serverUpdate->update($serverLabel);
It does the following:
- Creates an array $servers[] of the required servers to update
- Reads the application configuration.
- Creates a Monitor log class.
- Creates a new updaterStatus class.
- Loops through each server:
If a server label is specified read that one or read all active servers:
if ($serverLabel) {
$servers = $this->em->getRepository('JMPRServerMonBundle:MonitorServers')->findByLabel($serverLabel);
} else {
$servers = $this->em->getRepository('JMPRServerMonBundle:MonitorServers')->findByActive('yes');
}
Getting the Configuration and Creating the required Classes.
//get Configuration.
$configuration = $this->em->getRepository('JMPRServerMonBundle:MonitorConfig')->getConfig();
$monitorLog = $this->em->getRepository('JMPRServerMonBundle:MonitorLog');
$updater = new UpdaterStatus($this->em, $configuration, $monitorLog);
The configuration data requires helper class in its repository: - getConfig.
The configuration is stored (persisted) as a series of database records - 1 record for each configuration setting, but is used throughout the application as an associative array:
$configArray=array();
$configs = $this->findAll();
foreach($configs as $config) {
$configArray[$config->getKey()] = $config->getValue();
}
return $configArray;
}
Main Server loop:
This is where the work happens:
- Saving the old status
- Calling UpdateStatus to updating it
- Notifying and
- Updating the server entity
(1) $status_org = $server->getStatus();
// remove the old status from the array to avoid confusion between the new and old status
$server->setStatus('unknown');
(2) $updater->setServer($server, $status_org);
//check server status
$status_new = $updater->getStatus();
(3) //notify the nerds of applicable
$updater->notify();
//update server status
(4) $server->setLastCheck(new \DateTime('now'));
$server->setStatus($status_new);
$server->setError($updater->getError());
$server->setRtime($updater->getRtime());
if ($status_new = 'on') {
$server->setLastOnline($server->getLastCheck());
}
$this->em->persist($server);
$this->em->flush();
The next post goes into details on the Updater Status class.
No comments:
Post a Comment