Permalink
Browse files

Revert "Moved TaskBundle out."

This reverts commit b7ee481.
  • Loading branch information...
1 parent eaf3823 commit f57fc3e05b34d15a8181aba700c01c681e910b68 @hollodk committed Jul 25, 2011
View
18 src/Club/TaskBundle/ClubTaskBundle.php
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Club\TaskBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class ClubTaskBundle extends Bundle
+{
+}
View
84 src/Club/TaskBundle/Controller/TaskController.php
@@ -0,0 +1,84 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Club\TaskBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
+use Symfony\Component\HttpFoundation\Response;
+
+class TaskController extends Controller
+{
+ /**
+ * @Route("/task", name="admin_task")
+ * @Template()
+ */
+ public function indexAction()
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $tasks = $em->getRepository('ClubTaskBundle:Task')->findAll();
+
+ return array(
+ 'tasks' => $tasks
+ );
+ }
+
+ /**
+ * @Route("/task/disable/{id}", name="admin_task_disable")
+ */
+ public function disableAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $task = $em->find('ClubTaskBundle:Task',$id);
+ $task->setEnabled(0);
+
+ $em->persist($task);
+ $em->flush();
+
+ return $this->redirect($this->generateUrl('admin_task'));
+ }
+
+ /**
+ * @Route("/task/enable/{id}", name="admin_task_enable")
+ */
+ public function enableAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $task = $em->find('ClubTaskBundle:Task',$id);
+ $task->setEnabled(1);
+
+ $em->persist($task);
+ $em->flush();
+
+ return $this->redirect($this->generateUrl('admin_task'));
+ }
+
+ /**
+ * @Route("/task/run/{id}", name="admin_task_run")
+ */
+ public function runAction($id)
+ {
+ $em = $this->getDoctrine()->getEntityManager();
+
+ $task = $em->find('ClubTaskBundle:Task',$id);
+ $task->setNextRunAt(new \DateTime());
+ $task->setLocked(0);
+
+ $em->persist($task);
+ $em->flush();
+
+ return $this->redirect($this->generateUrl('admin_task'));
+ }
+}
View
73 src/Club/TaskBundle/DataFixtures/ORM/LoadTaskData.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Club\TaskBundle\DataFixtures\ORM;
+
+use Doctrine\Common\DataFixtures\FixtureInterface;
+
+class LoadTaskData implements FixtureInterface
+{
+ public function load($manager)
+ {
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Update dynamic groups');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onGroupTask');
+ $manager->persist($task);
+
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Cleanup logs');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onLogTask');
+ $manager->persist($task);
+
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Renewal memberships');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onAutoRenewalTask');
+ $manager->persist($task);
+
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Cleanup login logs');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onLoginAttemptTask');
+ $manager->persist($task);
+
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Cleanup ban logs');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onBanTask');
+ $manager->persist($task);
+
+ $task = new \Club\TaskBundle\Entity\Task();
+ $task->setTaskName('Send emails');
+ $task->setEnabled(1);
+ $task->setLocked(0);
+ $task->setNextRunAt(new \DateTime());
+ $task->setTaskInterval('+1 hour');
+ $task->setEvent('\Club\TaskBundle\Event\Events');
+ $task->setMethod('onMailTask');
+ $manager->persist($task);
+
+ $manager->flush();
+ }
+}
View
17 src/Club/TaskBundle/DependencyInjection/ClubTaskExtension.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Club\TaskBundle\DependencyInjection;
+
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+use Symfony\Component\Config\FileLocator;
+
+class ClubTaskExtension extends Extension
+{
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ $loader = new YamlFileLoader($container,new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('listener.yml');
+ }
+}
View
323 src/Club/TaskBundle/Entity/Task.php
@@ -0,0 +1,323 @@
+<?php
+
+namespace Club\TaskBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
+
+/**
+ * @ORM\Entity(repositoryClass="Club\TaskBundle\Repository\Task")
+ * @ORM\Table(name="club_task_task")
+ * @ORM\HasLifecycleCallbacks()
+ */
+class Task
+{
+ /**
+ * @ORM\Id
+ * @ORM\Column(type="integer")
+ * @ORM\GeneratedValue(strategy="AUTO")
+ *
+ * @var integer $id
+ */
+ private $id;
+
+ /**
+ * @ORM\Column(type="string")
+ *
+ * @var integer $task_name
+ */
+ private $task_name;
+
+ /**
+ * @ORM\Column(type="boolean")
+ *
+ * @var boolean $enabled
+ */
+ private $enabled;
+
+ /**
+ * @ORM\Column(type="boolean")
+ *
+ * @var boolean $locked
+ */
+ private $locked;
+
+ /**
+ * @ORM\Column(type="string")
+ *
+ * @var string $event
+ */
+ private $event;
+
+ /**
+ * @ORM\Column(type="string")
+ *
+ * @var string $method
+ */
+ private $method;
+
+ /**
+ * @ORM\Column(type="datetime", nullable="true")
+ *
+ * @var date $last_run_at
+ */
+ private $last_run_at;
+
+ /**
+ * @ORM\Column(type="datetime")
+ *
+ * @var date $next_run_at
+ */
+ private $next_run_at;
+
+ /**
+ * @ORM\Column(type="string")
+ *
+ * @var date $task_interval
+ */
+ private $task_interval;
+
+ /**
+ * @ORM\Column(type="datetime")
+ *
+ * @var date $created_at
+ */
+ private $created_at;
+
+ /**
+ * @ORM\Column(type="datetime")
+ *
+ * @var date $updated_at
+ */
+ private $updated_at;
+
+ /**
+ * Get id
+ *
+ * @return integer $id
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Set task_name
+ *
+ * @param string $taskName
+ */
+ public function setTaskName($taskName)
+ {
+ $this->task_name = $taskName;
+ }
+
+ /**
+ * Get task_name
+ *
+ * @return string $taskName
+ */
+ public function getTaskName()
+ {
+ return $this->task_name;
+ }
+
+ /**
+ * Set enabled
+ *
+ * @param boolean $enabled
+ */
+ public function setEnabled($enabled)
+ {
+ $this->enabled = $enabled;
+ }
+
+ /**
+ * Get enabled
+ *
+ * @return boolean $enabled
+ */
+ public function getEnabled()
+ {
+ return $this->enabled;
+ }
+
+ /**
+ * Set locked
+ *
+ * @param boolean $locked
+ */
+ public function setLocked($locked)
+ {
+ $this->locked = $locked;
+ }
+
+ /**
+ * Get locked
+ *
+ * @return boolean $locked
+ */
+ public function getLocked()
+ {
+ return $this->locked;
+ }
+
+ /**
+ * Set created_at
+ *
+ * @param datetime $createdAt
+ */
+ public function setCreatedAt($createdAt)
+ {
+ $this->created_at = $createdAt;
+ }
+
+ /**
+ * Get created_at
+ *
+ * @return datetime $createdAt
+ */
+ public function getCreatedAt()
+ {
+ return $this->created_at;
+ }
+
+ /**
+ * Set updated_at
+ *
+ * @param datetime $updatedAt
+ */
+ public function setUpdatedAt($updatedAt)
+ {
+ $this->updated_at = $updatedAt;
+ }
+
+ /**
+ * Get updated_at
+ *
+ * @return datetime $updatedAt
+ */
+ public function getUpdatedAt()
+ {
+ return $this->updated_at;
+ }
+
+ /**
+ * Set last_run_at
+ *
+ * @param datetime $lastRunAt
+ */
+ public function setLastRunAt($lastRunAt)
+ {
+ $this->last_run_at = $lastRunAt;
+ }
+
+ /**
+ * Get last_run_at
+ *
+ * @return datetime $lastRunAte
+ */
+ public function getLastRunAt()
+ {
+ return $this->last_run_at;
+ }
+
+ /**
+ * Set next_run_at
+ *
+ * @param datetime $nextRunAt
+ */
+ public function setNextRunAt($nextRunAt)
+ {
+ $this->next_run_at = $nextRunAt;
+ }
+
+ /**
+ * Get next_run_at
+ *
+ * @return datetime $nextRunAt
+ */
+ public function getNextRunAt()
+ {
+ return $this->next_run_at;
+ }
+
+
+ /**
+ * Set task_interval
+ *
+ * @param string $taskInterval
+ */
+ public function setTaskInterval($taskInterval)
+ {
+ $this->task_interval = $taskInterval;
+ }
+
+ /**
+ * Get task_interval
+ *
+ * @return string $taskInterval
+ */
+ public function getTaskInterval()
+ {
+ return $this->task_interval;
+ }
+
+ /**
+ * Set event
+ *
+ * @param string $event
+ */
+ public function setEvent($event)
+ {
+ $this->event = $event;
+ }
+
+ /**
+ * Get event
+ *
+ * @return string $event
+ */
+ public function getEvent()
+ {
+ return $this->event;
+ }
+
+ /**
+ * @ORM\PrePersist()
+ */
+ public function prePersist()
+ {
+ if (!$this->getId()) {
+ $this->setCreatedAt(new \DateTime());
+ $this->setUpdatedAt(new \DateTime());
+ }
+ }
+
+ /**
+ * @ORM\preUpdate()
+ */
+ public function preUpdate()
+ {
+ $this->setUpdatedAt(new \DateTime());
+ }
+
+ /**
+ * Set method
+ *
+ * @param string $method
+ */
+ public function setMethod($method)
+ {
+ $this->method = $method;
+ }
+
+ /**
+ * Get method
+ *
+ * @return string
+ */
+ public function getMethod()
+ {
+ return $this->method;
+ }
+}
View
13 src/Club/TaskBundle/Event/Events.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Club\TaskBundle\Event;
+
+final class Events
+{
+ const onGroupTask = 'group.task';
+ const onLogTask = 'log.task';
+ const onLoginAttemptTask = 'login_attempt.task';
+ const onBanTask = 'ban.task';
+ const onMailTask = 'mail.task';
+ const onAutoRenewalTask = 'auto_renewal.task';
+}
View
20 src/Club/TaskBundle/Event/FilterTaskEvent.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Club\TaskBundle\Event;
+
+use Symfony\Component\EventDispatcher\Event;
+
+class FilterTaskEvent extends Event
+{
+ protected $task;
+
+ public function __construct(\Club\TaskBundle\Entity\Task $task)
+ {
+ $this->task = $task;
+ }
+
+ public function getTask()
+ {
+ return $this->task;
+ }
+}
View
60 src/Club/TaskBundle/Listener/TaskListener.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Club\TaskBundle\Listener;
+
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+
+class TaskListener
+{
+ protected $em;
+ protected $event_dispatcher;
+
+ public function __construct($em, $event_dispatcher)
+ {
+ $this->em = $em;
+ $this->event_dispatcher = $event_dispatcher;
+ }
+
+ public function onKernelRequest(GetResponseEvent $event)
+ {
+ try {
+ $tasks = $this->em->getRepository('ClubTaskBundle:Task')->getTasksToExecute();
+
+ foreach ($tasks as $task) {
+ $this->startTask($task);
+
+ $event = new \Club\TaskBundle\Event\FilterTaskEvent($task);
+ $this->event_dispatcher->dispatch(constant($task->getEvent().'::'.$task->getMethod()), $event);
+
+ $this->stopTask($task);
+ }
+
+ } catch (\Exception $e) {
+ }
+ }
+
+ protected function startTask($task)
+ {
+ $task->setLocked(1);
+ $task->setLastRunAt(new \DateTime());
+
+ $this->em->persist($task);
+ $this->em->flush();
+
+ return $task;
+ }
+
+ protected function stopTask($task)
+ {
+ $task->setLocked(0);
+
+ $date = new \DateTime();
+ $date->modify($task->getTaskInterval());
+ $task->setNextRunAt($date);
+ $this->em->persist($task);
+ $this->em->flush();
+
+ return $task;
+ }
+}
View
29 src/Club/TaskBundle/Repository/Task.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Club\TaskBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+/**
+ * Task
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class Task extends EntityRepository
+{
+ public function getTasksToExecute()
+ {
+ $tasks = $this->_em->createQueryBuilder()
+ ->select('t')
+ ->from('\Club\TaskBundle\Entity\Task','t')
+ ->where('t.next_run_at < ?1')
+ ->andWhere('t.locked = 0')
+ ->andWhere('t.enabled = 1')
+ ->setParameter(1,date('Y-m-d H:i:s'))
+ ->getQuery()
+ ->getResult();
+
+ return $tasks;
+ }
+}
View
6 src/Club/TaskBundle/Resources/config/listener.yml
@@ -0,0 +1,6 @@
+services:
+ club.taskbundle.listener.task:
+ class: Club\TaskBundle\Listener\TaskListener
+ arguments: [@doctrine.orm.entity_manager, @event_dispatcher]
+ tags:
+ - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
View
4 src/Club/TaskBundle/Resources/config/routing.yml
@@ -0,0 +1,4 @@
+_task:
+ resource: "@ClubTaskBundle/Controller/TaskController.php"
+ type: annotation
+ prefix: /admin
View
50 src/Club/TaskBundle/Resources/views/Task/index.html.twig
@@ -0,0 +1,50 @@
+{% extends "ClubTaskBundle::base.html.twig" %}
+
+{% block body %}
+<h2>{% trans %}Task{% endtrans %}</h2>
+
+<table>
+ <thead>
+ <tr>
+ <th>{% trans %}Task{% endtrans %}</th>
+ <th>{% trans %}Enabled{% endtrans %}</th>
+ <th>{% trans %}Locked{% endtrans %}</th>
+ <th>{% trans %}Last run at{% endtrans %}</th>
+ <th>{% trans %}Next run at{% endtrans %}</th>
+ <th>{% trans %}Task interval{% endtrans %}</th>
+ <th>{% trans %}Action{% endtrans %}</th>
+ </tr>
+ </thead>
+ <tfoot>
+ <tr class="bg">
+ <td colspan="7">&#160;</td>
+ </tr>
+ </tfoot>
+ <tbody>
+ {% for task in tasks %}
+ <tr>
+ <td>{{ task.taskName }}</td>
+ <td>{% include "ClubLayoutBundle:Default:boolean.html.twig" with {'flag':task.enabled} %}</td>
+ <td>{% include "ClubLayoutBundle:Default:boolean_inverse.html.twig" with {'flag':task.locked} %}</td>
+ {% if task.lastRunAt == '' %}
+ <td>Never</td>
+ {% else %}
+ <td>{{ task.lastRunAt|date('Y-m-d H:i:s') }}</td>
+ {% endif %}
+ <td>{{ task.nextRunAt|date('Y-m-d H:i:s') }}</td>
+ <td>{{ task.taskInterval }}</td>
+ <td>
+ {% include "ClubLayoutBundle:Default:disable_link.html.twig" with {'url':path('admin_task_disable', {'id':task.id})} %}
+ {% include "ClubLayoutBundle:Default:enable_link.html.twig" with {'url':path('admin_task_enable', {'id':task.id})} %}
+ {% include "ClubLayoutBundle:Default:run_link.html.twig" with {'url':path('admin_task_run', {'id':task.id})} %}
+ </td>
+ </tr>
+ {% else %}
+ <tr>
+ <td colspan="7">{% trans %}There are no tasks.{% endtrans %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+</table>
+
+{% endblock %}
View
3 src/Club/TaskBundle/Resources/views/base.html.twig
@@ -0,0 +1,3 @@
+{% extends "ClubLayoutBundle:Default:base.html.twig" %}
+
+{% block header %}<h1>{% trans %}Task{% endtrans %}</h1>{% endblock %}

0 comments on commit f57fc3e

Please sign in to comment.