2017 © Pedro Peláez
 

symfony-bundle cron-event

Current symfony2 bundle generates events inside the application based on timers stored in DB.

image

litvinab/cron-event

Current symfony2 bundle generates events inside the application based on timers stored in DB.

  • Thursday, September 22, 2016
  • by litvinab
  • Repository
  • 1 Watchers
  • 1 Stars
  • 481 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

cron-event

Symfony2 bundle to set and run cron-based timers and events. It generates events inside the application based on timers stored in DB., (*1)

Currently bundle supports MongoDB only., (*2)

Steps to install and check

  1. run command in project root folder: composer require litvinab/cron-event, (*3)

  2. add new Litvinab\Bundle\CronEventBundle\CronEventBundle() in AppKernel.php, (*4)

  3. setup cron task: php app/console cron:run for each minute, (*5)

  4. for testing purposes add bundle test routes to routing.yml:, (*6)

cron:
    resource: "@CronEventBundle/Resources/config/routing.yml"
    prefix:   /cron
  1. To add test timer (1 minute timer) and event go to http://your-domain/cron/, (*7)

  2. Added schedules will be displayed on this page: http://your-domain/cron/show status fields should be unexpired, (*8)

  3. Refresh http://your-domain/cron/show page after 1-2 minutes after point #5. status fields should be expired it means that bundle works right., (*9)

!! Do not forget to remove test routes from routing.yml. It's not secure to leave it there., (*10)

Supported event types

timer - event in application will be triggered after N milliseconds., (*11)

event - event in application will be triggered in specified date and time., (*12)

How to use

1. Set event in your code

In controller:, (*13)

// get cron manager
$cronManager = $this->get('cron_event.manager');

// set timer with: human name, name, period 
// name of the symfony event: `cron_event.` + name
$timer = $cronManager->setTimer('My timer', 'test_timer', 60);

2. Add cron event subscriber

Of course you are able to create event listeners if you wish., (*14)

services.yml:, (*15)

services:
     app.subscriber.cron:
          class: AppBundle\EventSubscriber\CronSubscriber
          calls:
            - [setLogger, [@cron_event.logger]]
          tags:
            - { name: kernel.event_subscriber }

/YourBundle/EventSubscriber/CronSubscriber.php:, (*16)

<?php
namespace AppBundle\EventSubscriber;

use Litvinab\Bundle\CronEventBundle\Events\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Class CronSubscriber
 */
class CronSubscriber implements EventSubscriberInterface
{
    /**
     * @var Cron Logger
     */
    private $logger;

    /**
     * Set cron logger
     *
     * @param $logger
     */
    public function setLogger($logger)
    {
        $this->logger = $logger;
    }

    /**
     * Get subscribed events
     *
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return array(
            'cron_event.test_timer' => array('onCronTestEvent', 0)
        );
    }

    /**
     * Test event
     *
     * @param CronEvent $cronEvent
     */
    public function onCronTestEvent(CronEvent $cronEvent)
    {
        // confirm that event is executed
        $this->logger->addInfo('onCronTestEvent');

        // delete cron event from DB
        $cronEvent->delete();
    }
} 

3. Check logs

CronEvent bundle providing it's own logger. Service name of logger is cron_event.logger., (*17)

onCronTestEvent string should be appeared in app/logs/cron.log file after 2-3 minutes., (*18)

Console Commands

cron:run - this command should be launched each minute or another period of time in crontab;, (*19)

cron:list - list all schedules;, (*20)

The Versions

22/09 2016

dev-master

9999999-dev https://github.com/litvinab/cron-event

Current symfony2 bundle generates events inside the application based on timers stored in DB.

  Sources   Download

The Requires

 

by Anton Litvin

event cron cron event

22/09 2016

v1.0

1.0.0.0 https://github.com/litvinab/cron-event

Current symfony2 bundle generates events inside the application based on timers stored in DB.

  Sources   Download

The Requires

 

by Anton Litvin

event cron cron event