rizeway / job-bundle
A Simple Job Bundle for Symfony 2
Installs: 167
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 3
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-11-09 15:02:21 UTC
README
A simple job bundle for symfony 2 projects (with doctrine)
Installation
-
Add this bundle into your project composer.json file:
"require": { "rizeway/job-bundle": "dev-master" },
-
Update your composer dependancies.
composer.phar update
-
Register this bundle in your app/AppKernel.php
<?php public function registerBundles() { $bundles = array( // ...some other bundles... new Rizeway\JobBundle\RizewayJobBundle(), );
-
Update your database schema
php app/console doctrine:database:update --force
Usage
-
Add the daemon to your cron tab
php app/console rizeway:job:daemon
-
Creating a Job
A job is a class that implements Rizeway/JobBundle/JobHandler/JobHandlerInterface (you can also extends Rizeway/JobBundle/JobHandler/ContainerAwareJobHandler)
Example:
<?php namespace MyBundle\JobHandler; use Rizeway\JobBundle\JobHandler\ContainerAwareJobHandler; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class MyJobHandler extends ContainerAwareJobHandler { protected function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setRequired(array( 'my_required_option', )); } public function run() { $this->log('My Option Is : '.$this->getOption('my_required_option')); .... }
- Scheduling a job
Scheduling a job is done like this
<?php namespace MyBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Rizeway\JobBundle\Entity\Job; class myController extends Controller { public function myAction() { $job = new Job(); $job->setName('Job Name'); $job->setType('Job Type'); $job->setClassname('\MyBundle\JobHandler\MyJobHandler'); $job->setOptions(array( 'my_required_option' => 'option_value' )); $this->getDoctrine()->getEntityManager()->persist($job); $this->getDoctrine()->getEntityManager()->flush(); .... }
Advanced Usage
- Logger
The DoctrineLogger instance is available through a service. If you want to create your own Logger class and use it in the DaemonCommand, just create a new class which implements the JobLoggerInterface and declare it as a service.
Below is an example:
parameters: rizeway_job.logger.class: Acme\AcmeBundle\Logger\AcmeLogger