KEMBAR78
Symfony bundle fo asynchronous job processing | PDF
abc/job-bundle
Background 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 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;

}

}
Register job
• Simple registration by Container tags
demo_job:

class: AppBundleJobDemoJob

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 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]);
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="MyBundleExampleBundleEntityMyEntity",
options={"groups"={"primarykey"}, "version"="1"})

* @ReturnType("MyBundleExampleBundleModelSomeObject",
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-API
The 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/

Symfony bundle fo asynchronous job processing

  • 1.
  • 2.
    Wojciech Ciołko About me • SoftwareEngineer (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
  • 3.
    Contact • Twitter @WCiolko •https://github.com/aboutcoders • https://aboutcoders.com/ • http://osec.pl/ www.aboutcoders.com
  • 4.
    The problem • Synchronousprocessing blocks the application • Processing takes time and resources • Job processing often needs scaling • Processing logic mixed with application code
  • 5.
    AbcJobBundle • A Symfonybundle 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
  • 6.
  • 7.
    Features • Asynchronous executionof 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 useit • 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;
 }
 }
  • 10.
    Register job • Simpleregistration by Container tags demo_job:
 class: AppBundleJobDemoJob
 tags:
 - { name: "abc.job", type: "say_hello", method: "sayHello" }
  • 11.
    Execute job • Toexecute 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 • Youcan 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 • Symfonycommand • 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
 ]);
  • 16.
    REST-API The AbcJobBundle shipswith a JSON REST-API
  • 17.
    Clustered Environment • AbcJobBundleintegrates concept of a resource lock. • This feature is disabled by default. In order to enable it you have to install the AbcResourceLockBundle
  • 18.
  • 19.
    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/