public function onFinish() { if (!count($this->messages)) { return; } // everything went alright $contents = implode(PHP_EOL, $this->messages) . PHP_EOL; // Only write the |$contents| to the file when this is not ran as part of a unit test. // Ideally we would verify that the write was successful, but there's no good fallback. if (!$this->isTest) { file_put_contents(self::ERROR_LOG, $contents, FILE_APPEND); } // Early-return if there are no failures, because there is no need to send an alert message // for successful service execution. if ($this->failures == 0) { return; } $configuration = Configuration::getInstance(); // E-mail alerts can be disabled by the configuration, but force-enable them for tests. if (!$configuration->get('serviceLog/alerts') && !$this->isTest) { return; } // Compose an e-mail message for sending out an alert message. The recipients of this // message are defined in the main configuration file. $alert = new Message(); $alert->setFrom($configuration->get('serviceLog/from'))->setSubject($configuration->get('serviceLog/subject'))->setBody($contents); foreach ($configuration->get('serviceLog/recipients') as $recipient) { $alert->addTo($recipient); } $this->mailer->send($alert); }
<?php // Copyright 2017 Peter Beverloo. All rights reserved. // Use of this source code is governed by the MIT license, a copy of which can // be found in the LICENSE file. namespace Anime\Services; use Anime\Configuration; require __DIR__ . '/../../vendor/autoload.php'; // This file is the entry-point for executing the periodic services. It is adviced to create a cron // job that runs every minute (for best frequency accuracy) executing this file. $serviceLog = new ServiceLogImpl(); $serviceManager = new ServiceManager($serviceLog); $serviceManager->loadState(); // Register all the services known to the configuration file with the |$serviceManager|. foreach (Configuration::getInstance()->get('services') as $service) { $serviceManager->registerService(new $service['class']($service['options'])); } $serviceManager->execute(); $serviceManager->saveState();