partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit c443dd1517658215d34bdd2a954b686812694ab7
parent e4049c412cf739ea217a217f3bf00b9a5d0b6929
Author: Felicitus <felicitus@felicitus.org>
Date:   Mon, 28 Sep 2015 18:05:48 +0200

Migrated the cron logger

Diffstat:
Mapp/AppKernel.php | 1+
Mcronjobs/CheckForUpdates.php | 7+++----
Mcronjobs/CreateStatisticSnapshot.php | 9++++-----
Mcronjobs/UpdateTipsOfTheDay.php | 9++++-----
Msrc/PartKeepr/CoreBundle/DependencyInjection/Configuration.php | 5+++++
Msrc/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php | 1+
Msrc/PartKeepr/CoreBundle/Resources/config/services.xml | 1+
Msrc/PartKeepr/CoreBundle/Services/SystemService.php | 20++++++++++++++++----
Asrc/PartKeepr/CronLoggerBundle/DependencyInjection/PartKeeprCronLoggerExtension.php | 20++++++++++++++++++++
Asrc/PartKeepr/CronLoggerBundle/Entity/CronLogger.php | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/CronLoggerBundle/PartKeeprCronLoggerBundle.php | 9+++++++++
Asrc/PartKeepr/CronLoggerBundle/Resources/config/services.xml | 12++++++++++++
Asrc/PartKeepr/CronLoggerBundle/Services/CronLoggerService.php | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/CronLoggerBundle/Tests/CronLoggerTest.php | 28++++++++++++++++++++++++++++
Dsrc/backend/PartKeepr/CronLogger/CronLogger.php | 60------------------------------------------------------------
Dsrc/backend/PartKeepr/CronLogger/CronLoggerManager.php | 87-------------------------------------------------------------------------------
Msrc/backend/PartKeepr/PartKeepr.php | 2+-
Msrc/backend/PartKeepr/Setup/MiscSettingsSetup.php | 7+++----
18 files changed, 253 insertions(+), 170 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -81,6 +81,7 @@ class AppKernel extends Kernel } // PartKeepr bundles + $bundles[] = new PartKeepr\CronLoggerBundle\PartKeeprCronLoggerBundle(); $bundles[] = new PartKeepr\CategoryBundle\PartKeeprCategoryBundle(); $bundles[] = new PartKeepr\UploadedFileBundle\PartKeeprUploadedFileBundle(); $bundles[] = new PartKeepr\FrontendBundle\PartKeeprFrontendBundle(); diff --git a/cronjobs/CheckForUpdates.php b/cronjobs/CheckForUpdates.php @@ -10,11 +10,11 @@ namespace PartKeepr\Cronjobs; include(__DIR__."/../src/backend/PartKeepr/PartKeepr.php"); -use PartKeepr\PartKeepr, - PartKeepr\CronLogger\CronLoggerManager; +use PartKeepr\CronLoggerBundle\Services\CronLoggerManager; +use PartKeepr\PartKeepr; PartKeepr::initialize(); PartKeepr::doVersionCheck(); -CronLoggerManager::getInstance()->markCronRun(basename(__FILE__));- \ No newline at end of file +CronLoggerManager::getInstance()->markCronRun(basename(__FILE__)); diff --git a/cronjobs/CreateStatisticSnapshot.php b/cronjobs/CreateStatisticSnapshot.php @@ -3,11 +3,11 @@ namespace PartKeepr\Cronjobs; include(__DIR__."/../src/backend/PartKeepr/PartKeepr.php"); -use PartKeepr\PartKeepr, - PartKeepr\Statistic\StatisticSnapshotManager, - PartKeepr\CronLogger\CronLoggerManager; +use PartKeepr\CronLoggerBundle\Services\CronLoggerManager; +use PartKeepr\PartKeepr; +use PartKeepr\Statistic\StatisticSnapshotManager; PartKeepr::initialize(); StatisticSnapshotManager::getInstance()->createSnapshot(); -CronLoggerManager::getInstance()->markCronRun(basename(__FILE__));- \ No newline at end of file +CronLoggerManager::getInstance()->markCronRun(basename(__FILE__)); diff --git a/cronjobs/UpdateTipsOfTheDay.php b/cronjobs/UpdateTipsOfTheDay.php @@ -12,12 +12,12 @@ namespace PartKeepr\Cronjobs; include(__DIR__."/../src/backend/PartKeepr/PartKeepr.php"); -use PartKeepr\PartKeepr, - PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay, - PartKeepr\CronLogger\CronLoggerManager; +use PartKeepr\CronLoggerBundle\Services\CronLoggerManager; +use PartKeepr\PartKeepr; +use PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay; PartKeepr::initialize(); TipOfTheDay::syncTips(); -CronLoggerManager::getInstance()->markCronRun(basename(__FILE__));- \ No newline at end of file +CronLoggerManager::getInstance()->markCronRun(basename(__FILE__)); diff --git a/src/PartKeepr/CoreBundle/DependencyInjection/Configuration.php b/src/PartKeepr/CoreBundle/DependencyInjection/Configuration.php @@ -26,6 +26,11 @@ class Configuration implements ConfigurationInterface ->defaultValue('http://partkeepr.org/tips/%s') ->info('The URI where tips are loaded from') ->end() + ->arrayNode('required_cronjobs') + ->treatNullLike(array()) + ->prototype('scalar')->end() + ->defaultValue(array('1', '2', '3')) + ->end() ->scalarNode('tip_of_the_day_list') ->cannotBeEmpty() ->defaultValue('http://partkeepr.org/tips.json') diff --git a/src/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php b/src/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php @@ -27,6 +27,7 @@ class PartKeeprCoreExtension extends Extension $container->setParameter('partkeepr.authentication_provider', $config['authentication_provider']); $container->setParameter('partkeepr.tip_of_the_day_uri', $config['tip_of_the_day_uri']); $container->setParameter('partkeepr.tip_of_the_day_list', $config['tip_of_the_day_list']); + $container->setParameter('partkeepr.required_cronjobs', $config['required_cronjobs']); foreach ($config["directories"] as $key => $value) { $container->setParameter("partkeepr.directories.".$key, $value); diff --git a/src/PartKeepr/CoreBundle/Resources/config/services.xml b/src/PartKeepr/CoreBundle/Resources/config/services.xml @@ -9,6 +9,7 @@ <argument type="service" id="doctrine"/> <argument type="service" id="service_container"/> <argument type="service" id="partkeepr.versionservice"/> + <argument type="service" id="partkeepr.cronlogger_service"/> </service> <service id="partkeepr.systemnoticeservice" class="PartKeepr\CoreBundle\Services\SystemNoticeService"> <argument type="service" id="doctrine.orm.entity_manager"/> diff --git a/src/PartKeepr/CoreBundle/Services/SystemService.php b/src/PartKeepr/CoreBundle/Services/SystemService.php @@ -8,7 +8,7 @@ use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Version as ORMVersion; use PartKeepr\CoreBundle\System\OperatingSystem; use PartKeepr\CoreBundle\System\SystemInformationRecord; -use PartKeepr\CronLogger\CronLoggerManager; +use PartKeepr\CronLoggerBundle\Services\CronLoggerService; use PartKeepr\Util\Configuration; use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -25,11 +25,21 @@ class SystemService extends ContainerAware */ private $versionService; - public function __construct(Registry $doctrine, ContainerInterface $container, VersionService $versionService) - { + /** + * @var CronLoggerService + */ + private $cronLoggerService; + + public function __construct( + Registry $doctrine, + ContainerInterface $container, + VersionService $versionService, + CronLoggerService $cronLoggerService + ) { $this->entityManager = $doctrine->getManager(); $this->setContainer($container); $this->versionService = $versionService; + $this->cronLoggerService = $cronLoggerService; } /** @@ -92,7 +102,9 @@ class SystemService extends ContainerAware public function getSystemStatus() { if ($this->container->getParameter("partkeepr.cronjob_check")) { - $inactiveCronjobs = CronLoggerManager::getInstance()->getInactiveCronjobs(); + $inactiveCronjobs = $this->cronLoggerService->getInactiveCronjobs( + $this->container->getParameter("partkeepr.required_cronjobs") + ); } else { // Skip cronjob tests $inactiveCronjobs = array(); diff --git a/src/PartKeepr/CronLoggerBundle/DependencyInjection/PartKeeprCronLoggerExtension.php b/src/PartKeepr/CronLoggerBundle/DependencyInjection/PartKeeprCronLoggerExtension.php @@ -0,0 +1,20 @@ +<?php +namespace PartKeepr\CronLoggerBundle\DependencyInjection; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\Extension; +use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\Config\FileLocator; + +class PartKeeprCronLoggerExtension extends Extension +{ + + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } +} diff --git a/src/PartKeepr/CronLoggerBundle/Entity/CronLogger.php b/src/PartKeepr/CronLoggerBundle/Entity/CronLogger.php @@ -0,0 +1,65 @@ +<?php +namespace PartKeepr\CronLoggerBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\Util\BaseEntity; + +/** + * Holds a project attachment + * + * @ORM\Entity + **/ +class CronLogger extends BaseEntity +{ + /** + * @ORM\Column(type="datetime") + * @var \DateTime + */ + private $lastRunDate; + + /** + * @ORM\Column(type="string") + * @var string + */ + private $cronjob; + + /** + * Sets the last run date and time for this entry + * + * @param \DateTime $date The date and time + */ + public function setLastRunDate(\DateTime $date) + { + $this->lastRunDate = $date; + } + + /** + * Returns the date and time for this entry + * + * @return \DateTime the date and time for this entry + */ + public function getLastRunDate() + { + return $this->lastRunDate; + } + + /** + * Sets the cronjob for this entry + * + * @param string $cronjob the title for this entry + */ + public function setCronjob($cronjob) + { + $this->cronjob = $cronjob; + } + + /** + * Returns the cronjob for this entry + * + * @return string the title + */ + public function getCronjob() + { + return $this->cronjob; + } +} diff --git a/src/PartKeepr/CronLoggerBundle/PartKeeprCronLoggerBundle.php b/src/PartKeepr/CronLoggerBundle/PartKeeprCronLoggerBundle.php @@ -0,0 +1,9 @@ +<?php +namespace PartKeepr\CronLoggerBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class PartKeeprCronLoggerBundle extends Bundle +{ + +} diff --git a/src/PartKeepr/CronLoggerBundle/Resources/config/services.xml b/src/PartKeepr/CronLoggerBundle/Resources/config/services.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" ?> + +<container xmlns="http://symfony.com/schema/dic/services" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + <services> + <service id="partkeepr.cronlogger_service" class="PartKeepr\CronLoggerBundle\Services\CronLoggerService"> + <argument type="service" id="doctrine.orm.entity_manager"/> + </service> + </services> +</container> diff --git a/src/PartKeepr/CronLoggerBundle/Services/CronLoggerService.php b/src/PartKeepr/CronLoggerBundle/Services/CronLoggerService.php @@ -0,0 +1,80 @@ +<?php +namespace PartKeepr\CronLoggerBundle\Services; + +use Doctrine\ORM\EntityManager; +use PartKeepr\CronLoggerBundle\Entity\CronLogger; + +class CronLoggerService +{ + /** + * @var EntityManager + */ + private $entityManager; + + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + } + + /** + * Marks a specific cronjob as ran + * + * @param string $cronjob The name of the cronjob + * + * @return CronLogger The cron logger entity + */ + public function markCronRun($cronjob) + { + $dql = "SELECT c FROM PartKeepr\CronLoggerBundle\Entity\CronLogger c WHERE c.cronjob = :cronjob"; + $query = $this->entityManager->createQuery($dql); + $query->setParameter("cronjob", $cronjob); + + try { + $result = $query->getSingleResult(); + } catch (\Exception $e) { + $result = new CronLogger(); + $result->setCronjob($cronjob); + $this->entityManager->persist($result); + } + + $result->setLastRunDate(new \DateTime()); + + $this->entityManager->flush(); + + return $result; + } + + /** + * Returns a list of all inactive cronjobs + * + * @param none + * + * @return array A string of cronjob names which aren't running + */ + public function getInactiveCronjobs($requiredCronjobs) + { + $dql = "SELECT c.cronjob FROM PartKeepr\CronLoggerBundle\Entity\CronLogger c WHERE c.cronjob = :cronjob"; + $dql .= " AND c.lastRunDate > :date"; + + $query = $this->entityManager->createQuery($dql); + + $date = new \DateTime(); + $date->sub(new \DateInterval('P1D')); + $query->setParameter("date", $date); + + $failedCronjobs = array(); + + foreach ($requiredCronjobs as $cronjob) { + $query->setParameter("cronjob", $cronjob); + + try { + $query->getSingleResult(); + } catch (\Exception $e) { + $failedCronjobs[] = $cronjob; + } + + } + + return $failedCronjobs; + } +} diff --git a/src/PartKeepr/CronLoggerBundle/Tests/CronLoggerTest.php b/src/PartKeepr/CronLoggerBundle/Tests/CronLoggerTest.php @@ -0,0 +1,28 @@ +<?php +namespace PartKeepr\CronLoggerBundle\Tests; + +use Liip\FunctionalTestBundle\Test\WebTestCase; + +class CronLoggerTest extends WebTestCase +{ + public function testCronLogger() + { + $cronlogger = $this->getContainer()->get("partkeepr.cronlogger_service"); + + $cronLoggerEntry = $cronlogger->markCronRun("test"); + + $this->assertEquals("test", $cronLoggerEntry->getCronjob()); + $this->assertInstanceOf("\DateTime", $cronLoggerEntry->getLastRunDate()); + + $cronLoggerEntry->setLastRunDate(new \DateTime("1999-01-01 00:00:00")); + + $inactiveCronjobs = $cronlogger->getInactiveCronjobs(array("test")); + $this->assertEquals(false, in_array("test", $inactiveCronjobs)); + + $this->getContainer()->get("doctrine.orm.entity_manager")->flush(); + + $inactiveCronjobs = $cronlogger->getInactiveCronjobs(array("test")); + + $this->assertEquals(true, in_array("test", $inactiveCronjobs)); + } +} diff --git a/src/backend/PartKeepr/CronLogger/CronLogger.php b/src/backend/PartKeepr/CronLogger/CronLogger.php @@ -1,59 +0,0 @@ -<?php -namespace PartKeepr\CronLogger; - -use PartKeepr\UploadedFileBundle\Entity\UploadedFile, - PartKeepr\Util\BaseEntity, - PartKeepr\Util\Serializable, - PartKeepr\Util\Deserializable, - Doctrine\ORM\Mapping as ORM; - -/** - * Holds a project attachment - * @ORM\Entity - **/ -class CronLogger extends BaseEntity { - /** - * @ORM\Column(type="datetime") - * @var \DateTime - */ - private $lastRunDate; - - /** - * @ORM\Column(type="string") - * @var string - */ - private $cronjob; - - /** - * Sets the last run date and time for this entry - * @param \DateTime $date The date and time - */ - public function setLastRunDate (\DateTime $date) { - $this->lastRunDate = $date; - } - - /** - * Returns the date and time for this entry - * - * @return \DateTime the date and time for this entry - */ - public function getLastRunDate () { - return $this->lastRunDate; - } - - /** - * Sets the cronjob for this entry - * @param string $cronjob the title for this entry - */ - public function setCronjob ($cronjob) { - $this->cronjob = $cronjob; - } - - /** - * Returns the cronjob for this entry - * @return string the title - */ - public function getCronjob () { - return $this->cronjob; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/CronLogger/CronLoggerManager.php b/src/backend/PartKeepr/CronLogger/CronLoggerManager.php @@ -1,86 +0,0 @@ -<?php -namespace PartKeepr\CronLogger; - -use PartKeepr\Manager\AbstractManager, - PartKeepr\PartKeepr; - -class CronLoggerManager extends AbstractManager { - /** - * Returns the FQCN for the target entity to operate on. - * @return string The FQCN, e.g. PartKeepr\Part - */ - public function getEntityName () { - return 'PartKeepr\CronLogger\CronLogger'; - } - - /** - * Returns all fields which need to appear in the getList ResultSet. - * @return array An array of all fields which should be returned - */ - public function getQueryFields () { - return array("id", "title", "date"); - } - - /** - * Returns the default sort field - * - * @return string The default sort field - */ - public function getDefaultSortField () { - return "date"; - } - - /** - * Marks a specific cronjob as ran - * @param string $cronjob The name of the cronjob - */ - public function markCronRun ($cronjob) { - $dql = "SELECT c FROM PartKeepr\CronLogger\CronLogger c WHERE c.cronjob = :cronjob"; - $query = PartKeepr::getEM()->createQuery($dql); - $query->setParameter("cronjob", $cronjob); - - try { - $result = $query->getSingleResult(); - } catch (\Exception $e) { - $result = new CronLogger(); - $result->setCronjob($cronjob); - PartKeepr::getEM()->persist($result); - } - - $result->setLastRunDate(new \DateTime()); - - PartKeepr::getEM()->flush(); - } - - /** - * Returns a list of all inactive cronjobs - * - * @param none - * @return array A string of cronjob names which aren't running - */ - public function getInactiveCronjobs () { - $dql = "SELECT c.cronjob FROM PartKeepr\CronLogger\CronLogger c WHERE c.cronjob = :cronjob"; - $dql .= " AND c.lastRunDate > :date"; - - $query = PartKeepr::getEM()->createQuery($dql); - - $date = new \DateTime(); - $date->sub(new \DateInterval('P1D')); - $query->setParameter("date", $date); - - $failedCronjobs = array(); - - foreach (PartKeepr::getRequiredCronjobs() as $cronjob) { - $query->setParameter("cronjob", $cronjob); - - try { - $query->getSingleResult(); - } catch (\Exception $e) { - $failedCronjobs[] = $cronjob; - } - - } - - return $failedCronjobs; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -176,7 +176,7 @@ class PartKeepr 'PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory', 'PartKeepr\AuthBundle\Entity\UserPreference', 'PartKeepr\CoreBundle\Entity\SystemNotice', - 'PartKeepr\CronLogger\CronLogger', + 'PartKeepr\CronLoggerBundle\Entity\CronLogger', ); } diff --git a/src/backend/PartKeepr/Setup/MiscSettingsSetup.php b/src/backend/PartKeepr/Setup/MiscSettingsSetup.php @@ -1,8 +1,8 @@ <?php namespace PartKeepr\Setup; -use PartKeepr\CronLogger\CronLoggerManager, - PartKeepr\PartKeepr; +use PartKeepr\CronLoggerBundle\Services\CronLoggerManager; +use PartKeepr\PartKeepr; /** * Sets up misc stuff, which doesn't fit into other steps @@ -66,4 +66,4 @@ class MiscSettingsSetup extends AbstractSetup { $em->getProxyFactory()->generateProxyClasses($metadatas, $destPath); } -}- \ No newline at end of file +}