commit 35b1aac6be58af585ecddb1ec2fb8d8de3ddf490
parent e37377089feab9f4249ae9a248b7d951b316fd36
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 7 Nov 2015 21:13:02 +0100
Added API call to run all crons
Diffstat:
4 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/app/config/routing.yml b/app/config/routing.yml
@@ -23,6 +23,11 @@ PartKeeprStatisticBundle:
type: annotation
prefix: /
+PartKeeprCronloggerBundle:
+ resource: "@PartKeeprCronLoggerBundle/Controller/"
+ type: annotation
+ prefix: /
+
PartKeeprPartBundle:
resource: "@PartKeeprPartBundle/Controller/"
type: annotation
diff --git a/src/PartKeepr/CronLoggerBundle/Controller/CronRunnerController.php b/src/PartKeepr/CronLoggerBundle/Controller/CronRunnerController.php
@@ -0,0 +1,19 @@
+<?php
+namespace PartKeepr\CronLoggerBundle\Controller;
+
+
+use FOS\RestBundle\Controller\FOSRestController;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Component\HttpFoundation\Response;
+
+class CronRunnerController extends FOSRestController
+{
+ /**
+ * @Route("/api/cron/run")
+ */
+ public function runCronsAction () {
+ $this->get("partkeepr.cronlogger_service")->runCrons();
+
+ return new Response("", 200);
+ }
+}
diff --git a/src/PartKeepr/CronLoggerBundle/Resources/config/services.xml b/src/PartKeepr/CronLoggerBundle/Resources/config/services.xml
@@ -7,6 +7,7 @@
<services>
<service id="partkeepr.cronlogger_service" class="PartKeepr\CronLoggerBundle\Services\CronLoggerService">
<argument type="service" id="doctrine.orm.entity_manager"/>
+ <argument type="service" id="kernel"/>
</service>
</services>
</container>
diff --git a/src/PartKeepr/CronLoggerBundle/Services/CronLoggerService.php b/src/PartKeepr/CronLoggerBundle/Services/CronLoggerService.php
@@ -3,6 +3,10 @@ namespace PartKeepr\CronLoggerBundle\Services;
use Doctrine\ORM\EntityManager;
use PartKeepr\CronLoggerBundle\Entity\CronLogger;
+use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\HttpKernel\Kernel;
class CronLoggerService
{
@@ -11,9 +15,15 @@ class CronLoggerService
*/
private $entityManager;
- public function __construct(EntityManager $entityManager)
+ /**
+ * @var Kernel
+ */
+ private $kernel;
+
+ public function __construct(EntityManager $entityManager, Kernel $kernel)
{
$this->entityManager = $entityManager;
+ $this->kernel = $kernel;
}
/**
@@ -87,4 +97,34 @@ class CronLoggerService
$query->execute();
}
+
+ public function runCrons () {
+ $this->entityManager->beginTransaction();
+ $repository = $this->entityManager->getRepository("PartKeeprCronLoggerBundle:CronLogger");
+
+ $cronJobs = $repository->findAll();
+
+ $application = new Application($this->kernel);
+ $application->setAutoExit(false);
+ $output = new NullOutput();
+
+ $minRunDate = new \DateTime();
+ $minRunDate->sub(new \DateInterval('PT6H'));
+
+ foreach ($cronJobs as $cronJob) {
+ if ($minRunDate->getTimestamp() - $cronJob->getLastRunDate()->getTimestamp() < 0) {
+ break;
+ }
+
+ $command = $cronJob->getCronjob();
+
+ $input = new ArrayInput(array(
+ 'command' => $command,
+ ));
+
+ $application->run($input, $output);
+ }
+
+ $this->entityManager->commit();
+ }
}