symfony bundle fo asynchronous job processing

19
abc/job-bundle Background job processing bundle for Symfony

Upload: wojciech-ciolko

Post on 19-Feb-2017

755 views

Category:

Presentations & Public Speaking


9 download

TRANSCRIPT

abc/job-bundleBackground job processing bundle for Symfony

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

Contact

• Twitter @WCiolko

• https://github.com/aboutcoders

• https://aboutcoders.com/

• http://osec.pl/

www.aboutcoders.com

The problem

• Synchronous processing blocks the application

• Processing takes time and resources

• Job processing often needs scaling

• Processing logic mixed with application code

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

Tech Specs

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

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

Create a job• Job is a regular PHP class

use Abc\Bundle\JobBundle\Annotation\ParamType;use Abc\Bundle\JobBundle\Annotation\ReturnType;use Psr\Log\LoggerInterface;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; }}

Register job

• Simple registration by Container tags

demo_job: class: AppBundle\Job\DemoJob tags: - { name: "abc.job", type: "say_hello", method: "sayHello" }

Execute job• To execute a job you need to add it to the job manager

use Abc\Bundle\JobBundle\Job\Mailer\Message;use Abc\Bundle\JobBundle\Job\ManagerInterface;// retrieve job manager from the container/** @var ManagerInterface $manager */$manager = $container->get('abc.job.manager');// create a message$message = new Message();$message->setTo('[email protected]');$message->setFrom('[email protected]');$message->setSubject('Hello World');// add job to the queue$manager->addJob('abc_mailer', [$message]);

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());

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="My\Bundle\ExampleBundle\Entity\MyEntity", options={"groups"={"primarykey"}, "version"="1"}) * @ReturnType("My\Bundle\ExampleBundle\Model\SomeObject", options={"groups"={"mygroup"}, "version"="2") */public function doSomething($entity){ … }

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);

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]);

REST-APIThe AbcJobBundle ships with a JSON REST-API

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

Demo

https://github.com/aboutcoders/job-bundle-skeleton-app

Contact

• Twitter @WCiolko

• https://github.com/aboutcoders

• https://aboutcoders.com/

• http://osec.pl/

www.aboutcoders.com

Contact

• Twitter @WCiolko

• https://github.com/aboutcoders

• https://aboutcoders.com/

• http://osec.pl/