Slides from the talk about abc/job-bundle, Symfony bundle for asynchronous job processing in background. You can find the bundle here: https://github.com/aboutcoders/job-bundle
Demo app that mentioned during the talk where you can check how bundle is configured https://github.com/aboutcoders/job-bundle-skeleton-app
2. Wojciech
Ciołko
About me
• Software Engineer (PayPal, Rocket
Internet, AxelSpringer, PostCon,
AboutCoders, OSEC)
• Agile oriented architect and project
leader focused on scalability,
maintainability and performance
• I always try to deliver the best
quality combined with most relevant
technologies
4. The problem
• Synchronous processing blocks the application
• Processing takes time and resources
• Job processing often needs scaling
• Processing logic mixed with application code
5. AbcJobBundle
• A Symfony bundle to process jobs asynchronously
by simply annotating a method and registering the
class within the service container.
• https://github.com/aboutcoders/job-bundle
www.aboutcoders.com
7. Features
• Asynchronous execution of jobs
• Status information about jobs
• Functionality to cancel, update, restart a job
• Repeated execution of jobs with schedules
• cron based expressions
• JSON REST-Api
• Support for multiple message queue systems:
• Doctrine, RabbitMQ, InMemory, Redis, Amazon SQS, Iron MQ, Pheanstalk
8. How to use it
• Install and configure following the docs
https://github.com/aboutcoders/job-bundle
• Create job class
• Register job in container
• Execute a job adding to job manager
9. Create a job
• Job is a regular PHP class
use AbcBundleJobBundleAnnotationParamType;
use AbcBundleJobBundleAnnotationReturnType;
use PsrLogLoggerInterface;
class DemoJob
{
/**
* @ParamType("to", type="string")
* @ParamType("logger", type="@abc.logger")
* @ReturnType("string")
*/
public function sayHello($to, LoggerInterface $logger)
{
$message = 'Hello ' . $to;
$logger->info($message);
return $message;
}
}
11. Execute job
• To execute a job you need to add it to the job manager
use AbcBundleJobBundleJobMailerMessage;
use AbcBundleJobBundleJobManagerInterface;
// retrieve job manager from the container
/** @var ManagerInterface $manager */
$manager = $container->get('abc.job.manager');
// create a message
$message = new Message();
$message->setTo('mail@domain.tld');
$message->setFrom('mail@domain.tld');
$message->setSubject('Hello World');
// add job to the queue
$manager->addJob('abc_mailer', [$message]);
12. Job status information
• You can get current status of a job
• You can get associated log messages
// get log messages of a job
$logs = $manager->getLogs($job->getTicket());
13. Serialization of parameters
• When a job is persisted to the database
• REST-Api
• The AbcJobBundle uses the JMS serializer by default for serialization
• You can configure serialization options for the parameters and return value of a job
/**
* @ParamType("entity", type="MyBundleExampleBundleEntityMyEntity",
options={"groups"={"primarykey"}, "version"="1"})
* @ReturnType("MyBundleExampleBundleModelSomeObject",
options={"groups"={"mygroup"}, "version"="2")
*/
public function doSomething($entity){
…
}
14. Scheduled jobs
• You can configure one or more schedules for a job in order to
configure repeated execution of a job.
• The bundle relies on the AbcSchedulerBundle to provide
this functionality.
//use builder
$job = JobBuilder::create('my_job')
->addSchedule('cron', '1 * * * *')
->addSchedule('cron', '30 * * * *')
->build();
//create schedule
$schedule = ScheduleBuilder::create('cron', '1 * * * *');
//remove schedule
$job->removeSchedule($schedule);
15. Consume messages
• Symfony command
• From your code
php bin/console abc:job:consume default
// retrieve job manager from the container
$consumer = $container->get('abc.job.consumer');
$consumer->consume('default', [
'max-iterations' => 250
]);
17. Clustered Environment
• AbcJobBundle integrates concept of a resource
lock.
• This feature is disabled by default. In order to
enable it you have to install the
AbcResourceLockBundle