SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
QUEUES & WORKERS
FASTER PHP APPS USING
PHP DORSET 6/6/16
RICHARD BAKER
twitter.com/r_bake_r
github.com/rjbaker
uk.linkedin.com/in/richjbaker
We’re hiring!
BACKGROUND
MOBILE API
▸ 140+ apps
▸ ~4.5m users
▸ REST/JSON
▸ Apache + PHP + ElasticSearch
▸ 20+ servers / various tasks
▸ Hosted on AWS
THE PROBLEM
TRAFFIC
THE PROBLEM
LATENCY GROWS
ELIMINATE EXPENSIVE, TIME
CONSUMING OPERATIONS AND
CALLS TO EXTERNAL SERVICES
USE MYSQL
WHAT TO QUEUE
MYSQL AS A QUEUE
job_id job_name data status
1 sendEmail {payload} completed
2 sendPush {payload} processing
3 requestThing {payload} waiting
4 resizeSelfie {payload} waiting
WHAT TO QUEUE
WHY NOT USE TRANSACTIONS TO OBTAIN A LOCK?
THE DATABASE IS NOT A
QUEUE. THE DATABASE IS
NOT A QUEUE.
Stephen Corona
WHAT TO QUEUE
https://www.scalingphpbook.com
USE A QUEUE!
QUEUES & WORKERS
MESSAGE QUEUES VS JOB QUEUES
▸ Kind of similar
▸ Most job queues built upon some kind of message queue
▸ Broker messages between systems
▸ Provide transport, storage and protocol
▸ Job queues abstract the lower level message component
▸ Integrate with most applications fairly easily
QUEUES & WORKERS
JOBS VS SCHEDULED TASKS
▸ Run at a predefined point in time ⏰
▸ May repeat at regular intervals or according to
calendar 📅
▸ Typically triggered by cron or other scheduler 🔫
▸ Scheduled tasks can trigger jobs! 💥
CHOOSE A QUEUE WITH
FEATURES BEST SUITED TO
YOUR APPLICATION
CHOOSING A JOB QUEUE
CONSIDERATIONS
▸ Job priority & time sensitivity
▸ Job ordering and consistency (FIFO)
▸ Payload size limits
▸ Message data type & protocol
▸ Support for other languages / client libraries
▸ Failure management / retry policy
▸ Fault tolerance & redundancy
▸ One-time delivery guarantee
▸ Monitoring & statistics
▸ Distribution by task to specific workers e.g. Video encoding
CHOOSING A JOB QUEUE
BEANSTALKD
▸ Protocol similar to Memcached
▸ Clients need to know about all Beanstalkd servers (like memcached!)
▸ Beanstalkd servers can persist jobs, handle restarts without losing jobs
▸ Uses “tubes” to differentiate different queues
▸ Supports job TTR. Failed/hung jobs get put back into queue.
▸ Supports blocking. Client connects and waits for a new job.
▸ Requires setup and maintenance
▸ Loads of client libraries
http://kr.github.io/beanstalkd/
CHOOSING A JOB QUEUE
AMAZON SQS
▸ SAAS - Already using AWS, literally no setup
▸ Massively redundant, cheap, maintenance free
▸ HTTP/JSON under the hood, simple
▸ Supports long-polling.
▸ Best effort FIFO (no guarantees)
▸ No concept of job priority. Use different queues.
▸ Retry policy allows jobs to reappear in queue if not completed in specified time
▸ Configurable number of retries
▸ Queue stats and alarms integrate with autoscaling
▸ Scale worker instances based on queue length/backlog/rate
https://aws.amazon.com/sqs/
CHOOSING A JOB QUEUE
OTHER POPULAR QUEUES
▸ Celery (backed by RabbitMQ) - http://www.celeryproject.org
▸ php-resque (backed by Redis) -https://github.com/
chrisboulton/php-resque
▸ Kafka - http://kafka.apache.org
▸ Gearman - http://gearman.org
▸ Iron.io (SAAS) - https://www.iron.io
▸ Loads more - www.queues.io
PROCESSING
JOBS
PROCESSING JOBS
WORKER PROCESS
▸ Essentially an infinite loop
▸ Executed on command line
▸ Asks queue for new job
▸ Resolves job method
▸ Execute with payload
▸ Delete job from queue
▸ Repeat
PROCESSING JOBS
<?php
$queue = new Queue();
while(true) {
$job = $queue->pop('queue-name');
try {
if ($job->execute()) {
$job->delete();
} else {
$job->release();
}
} catch (Exception $e) {
$job->release();
}
}
IMPROVING THE
WORKER
pcntl_signal_dispatch();
PROCESSING JOBS
PROCESS CONTROL EXTENSIONS (PCNTL)
▸ Respond to unix process signals
▸ Gracefully stop worker processes
▸ Complete current job before exiting
▸ Careful if using Apache mod_php on same
server
▸ http://php.net/manual/en/book.pcntl.php
IMPROVED WORKER
<?php
namespace Demo;
use DemoQueueQueueInterface;
class Worker
{
protected $shouldRun = true;
protected $queue;
public function __construct(QueueInterface $queue)
{
declare(ticks = 1);
$this->queue = $queue;
pcntl_signal(SIGTERM, [$this, 'signalHandler']);
pcntl_signal(SIGINT, [$this, 'signalHandler']);
pcntl_signal(SIGQUIT, [$this, 'signalHandler']);
}
public function run($queueName)
{
echo "Starting worker on queue '{$queueName}' n";
while ($this->shouldRun) {
$job = $this->queue->pop($queueName);
try {
if ($job->execute()) {
$job->delete();
} else {
$job->release();
}
} catch (Exception $e) {
$job->release();
error_log($e->getTraceAsString());
}
pcntl_signal_dispatch();
}
}
public function signalHandler($signal)
{
switch ($signal) {
case SIGTERM:
case SIGINT:
case SIGQUIT:
echo "Job completed. Exiting... n";
$this->shouldRun = false;
break;
}
}
}
IMPROVED WORKER
WTF IS DECLARE(TICKS = 1);?
▸ Officially deprecated
▸ Triggered after php has executed a certain
number of statements
▸ Interacts with pcntl_signal_dispatch()
▸ I admit i’ve not fully tested this with PHP7.0
PROCESSING JOBS
KEEPING WORKERS RUNNING
PROCESSING JOBS
SUPERVISOR
▸ Process manager in similar vein to forever, pm2, php-fpm
▸ Runs as service
▸ Starts and restarts php worker processes
▸ Has CLI client (supervisorctl)
▸ Web interface
▸ Easy to install and configure
http://supervisord.org
PROCESSING JOBS
SUPERVISOR CONFIG [program:alertworker]
command = /usr/bin/php /path/to/queueRunner.php -q=prod-alerts
autorestart = true
autostart = true
directory = /path/to/scripts
environment = DEPLOYMENT='production'
exitcodes = 0,2
numprocs = 1
numprocs_start = 0
priority = 999
startretries = 3
startsecs = 4
stderr_capture_maxbytes = 1MB
stderr_events_enabled = false
stderr_logfile = AUTO
stderr_logfile_backups = 10
stderr_logfile_maxbytes = 50MB
stderr_syslog = false
stdout_capture_maxbytes = 1MB
stdout_events_enabled = true
stdout_logfile = AUTO
stdout_logfile_backups = 10
stdout_logfile_maxbytes = 40MB
stdout_syslog = false
stopsignal = TERM
stopwaitsecs = 10
umask = 022
user = worker
DEMO TIME
THANKS FOR LISTENING!
twitter.com/r_bake_r
github.com/rjbaker
uk.linkedin.com/in/richjbaker

Weitere ähnliche Inhalte

Was ist angesagt?

Airflow at lyft
Airflow at lyftAirflow at lyft
Airflow at lyftTao Feng
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Roberto Franchini
 
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTOClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTOAltinity Ltd
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration TestersNikhil Mittal
 
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버준철 박
 
Event-driven serverless functions with Next.js and Inngest
Event-driven serverless functions with Next.js and InngestEvent-driven serverless functions with Next.js and Inngest
Event-driven serverless functions with Next.js and InngestDan Farrelly
 
Grokking TechTalk #33: High Concurrency Architecture at TIKI
Grokking TechTalk #33: High Concurrency Architecture at TIKIGrokking TechTalk #33: High Concurrency Architecture at TIKI
Grokking TechTalk #33: High Concurrency Architecture at TIKIGrokking VN
 
Grokking Techtalk #39: Gossip protocol and applications
Grokking Techtalk #39: Gossip protocol and applicationsGrokking Techtalk #39: Gossip protocol and applications
Grokking Techtalk #39: Gossip protocol and applicationsGrokking VN
 
Lightning Locker Services
Lightning Locker ServicesLightning Locker Services
Lightning Locker ServicesAmit Chaudhary
 
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016Frans Rosén
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life CycleXinchen Hui
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconPeter Lawrey
 
[225]yarn 기반의 deep learning application cluster 구축 김제민
[225]yarn 기반의 deep learning application cluster 구축 김제민[225]yarn 기반의 deep learning application cluster 구축 김제민
[225]yarn 기반의 deep learning application cluster 구축 김제민NAVER D2
 
OWASP AppSecEU 2018 – Attacking "Modern" Web Technologies
OWASP AppSecEU 2018 – Attacking "Modern" Web TechnologiesOWASP AppSecEU 2018 – Attacking "Modern" Web Technologies
OWASP AppSecEU 2018 – Attacking "Modern" Web TechnologiesFrans Rosén
 
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourSoroush Dalili
 
Grokking TechTalk #31: Asynchronous Communications
Grokking TechTalk #31: Asynchronous CommunicationsGrokking TechTalk #31: Asynchronous Communications
Grokking TechTalk #31: Asynchronous CommunicationsGrokking VN
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Schedulermatthewrdale
 
Bug Bounty Hunter Methodology - Nullcon 2016
Bug Bounty Hunter Methodology - Nullcon 2016Bug Bounty Hunter Methodology - Nullcon 2016
Bug Bounty Hunter Methodology - Nullcon 2016bugcrowd
 
Low level java programming
Low level java programmingLow level java programming
Low level java programmingPeter Lawrey
 

Was ist angesagt? (20)

Airflow at lyft
Airflow at lyftAirflow at lyft
Airflow at lyft
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
 
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTOClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration Testers
 
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버
[NDC2017 : 박준철] Python 게임 서버 안녕하십니까 - 몬스터 슈퍼리그 게임 서버
 
Event-driven serverless functions with Next.js and Inngest
Event-driven serverless functions with Next.js and InngestEvent-driven serverless functions with Next.js and Inngest
Event-driven serverless functions with Next.js and Inngest
 
Grokking TechTalk #33: High Concurrency Architecture at TIKI
Grokking TechTalk #33: High Concurrency Architecture at TIKIGrokking TechTalk #33: High Concurrency Architecture at TIKI
Grokking TechTalk #33: High Concurrency Architecture at TIKI
 
Grokking Techtalk #39: Gossip protocol and applications
Grokking Techtalk #39: Gossip protocol and applicationsGrokking Techtalk #39: Gossip protocol and applications
Grokking Techtalk #39: Gossip protocol and applications
 
Lightning Locker Services
Lightning Locker ServicesLightning Locker Services
Lightning Locker Services
 
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @Geecon
 
[225]yarn 기반의 deep learning application cluster 구축 김제민
[225]yarn 기반의 deep learning application cluster 구축 김제민[225]yarn 기반의 deep learning application cluster 구축 김제민
[225]yarn 기반의 deep learning application cluster 구축 김제민
 
OWASP AppSecEU 2018 – Attacking "Modern" Web Technologies
OWASP AppSecEU 2018 – Attacking "Modern" Web TechnologiesOWASP AppSecEU 2018 – Attacking "Modern" Web Technologies
OWASP AppSecEU 2018 – Attacking "Modern" Web Technologies
 
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
 
Grokking TechTalk #31: Asynchronous Communications
Grokking TechTalk #31: Asynchronous CommunicationsGrokking TechTalk #31: Asynchronous Communications
Grokking TechTalk #31: Asynchronous Communications
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Scheduler
 
Bug Bounty Hunter Methodology - Nullcon 2016
Bug Bounty Hunter Methodology - Nullcon 2016Bug Bounty Hunter Methodology - Nullcon 2016
Bug Bounty Hunter Methodology - Nullcon 2016
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
 

Andere mochten auch

Distributed Queue System using Gearman
Distributed Queue System using GearmanDistributed Queue System using Gearman
Distributed Queue System using GearmanEric Cho
 
Case study: iTunes for K-12
Case study: iTunes for K-12Case study: iTunes for K-12
Case study: iTunes for K-12Giorgio Sironi
 
Chansonnier: web application for multimedia search on song videos
Chansonnier: web application for multimedia search on song videosChansonnier: web application for multimedia search on song videos
Chansonnier: web application for multimedia search on song videosGiorgio Sironi
 
Queue System and Zend\Queue implementation
Queue System and Zend\Queue implementationQueue System and Zend\Queue implementation
Queue System and Zend\Queue implementationGianluca Arbezzano
 
Blind detection of image manipulation @ PoliMi
Blind detection of image manipulation @ PoliMiBlind detection of image manipulation @ PoliMi
Blind detection of image manipulation @ PoliMiGiorgio Sironi
 
VLANs in the Linux Kernel
VLANs in the Linux KernelVLANs in the Linux Kernel
VLANs in the Linux KernelKernel TLV
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqRuben Tan
 
Crypto With OpenSSL
Crypto With OpenSSLCrypto With OpenSSL
Crypto With OpenSSLZhi Guan
 
Case study: Khan Academy
Case study: Khan AcademyCase study: Khan Academy
Case study: Khan AcademyGiorgio Sironi
 
Navigation system for blind using GPS & GSM
Navigation system for blind using GPS & GSMNavigation system for blind using GPS & GSM
Navigation system for blind using GPS & GSMPrateek Anand
 
Map Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneMap Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneDr. Geophysics
 
Gearman: A Job Server made for Scale
Gearman: A Job Server made for ScaleGearman: A Job Server made for Scale
Gearman: A Job Server made for ScaleMike Willbanks
 
Smart blind stick book
Smart blind stick bookSmart blind stick book
Smart blind stick bookAhmed Moawad
 
Vehicle tracking system using gps and google map
Vehicle tracking system using gps and google mapVehicle tracking system using gps and google map
Vehicle tracking system using gps and google mapsanchit bhargava
 
Vehicle tracking system using gps and gsm
Vehicle tracking system using gps and gsmVehicle tracking system using gps and gsm
Vehicle tracking system using gps and gsmanita maharjan
 

Andere mochten auch (20)

Distributed Queue System using Gearman
Distributed Queue System using GearmanDistributed Queue System using Gearman
Distributed Queue System using Gearman
 
CakePHP REST Plugin
CakePHP REST PluginCakePHP REST Plugin
CakePHP REST Plugin
 
Gearman for MySQL
Gearman for MySQLGearman for MySQL
Gearman for MySQL
 
CouchDB @ PoliMi
CouchDB @ PoliMiCouchDB @ PoliMi
CouchDB @ PoliMi
 
Case study: iTunes for K-12
Case study: iTunes for K-12Case study: iTunes for K-12
Case study: iTunes for K-12
 
Case study: Insegnalo
Case study: InsegnaloCase study: Insegnalo
Case study: Insegnalo
 
Chansonnier: web application for multimedia search on song videos
Chansonnier: web application for multimedia search on song videosChansonnier: web application for multimedia search on song videos
Chansonnier: web application for multimedia search on song videos
 
Queue System and Zend\Queue implementation
Queue System and Zend\Queue implementationQueue System and Zend\Queue implementation
Queue System and Zend\Queue implementation
 
Blind detection of image manipulation @ PoliMi
Blind detection of image manipulation @ PoliMiBlind detection of image manipulation @ PoliMi
Blind detection of image manipulation @ PoliMi
 
PHP and node.js Together
PHP and node.js TogetherPHP and node.js Together
PHP and node.js Together
 
VLANs in the Linux Kernel
VLANs in the Linux KernelVLANs in the Linux Kernel
VLANs in the Linux Kernel
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Crypto With OpenSSL
Crypto With OpenSSLCrypto With OpenSSL
Crypto With OpenSSL
 
Case study: Khan Academy
Case study: Khan AcademyCase study: Khan Academy
Case study: Khan Academy
 
Navigation system for blind using GPS & GSM
Navigation system for blind using GPS & GSMNavigation system for blind using GPS & GSM
Navigation system for blind using GPS & GSM
 
Map Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for EveryoneMap Projections, Datums, GIS and GPS for Everyone
Map Projections, Datums, GIS and GPS for Everyone
 
Gearman: A Job Server made for Scale
Gearman: A Job Server made for ScaleGearman: A Job Server made for Scale
Gearman: A Job Server made for Scale
 
Smart blind stick book
Smart blind stick bookSmart blind stick book
Smart blind stick book
 
Vehicle tracking system using gps and google map
Vehicle tracking system using gps and google mapVehicle tracking system using gps and google map
Vehicle tracking system using gps and google map
 
Vehicle tracking system using gps and gsm
Vehicle tracking system using gps and gsmVehicle tracking system using gps and gsm
Vehicle tracking system using gps and gsm
 

Ähnlich wie Faster PHP apps using Queues and Workers

Node, can you even in CPU intensive operations?
Node, can you even in CPU intensive operations?Node, can you even in CPU intensive operations?
Node, can you even in CPU intensive operations?The Software House
 
Introduction to LAVA Workload Scheduler
Introduction to LAVA Workload SchedulerIntroduction to LAVA Workload Scheduler
Introduction to LAVA Workload SchedulerNopparat Nopkuat
 
Confitura 2018 — Apache Beam — Promyk Nadziei Data Engineera
Confitura 2018 — Apache Beam — Promyk Nadziei Data EngineeraConfitura 2018 — Apache Beam — Promyk Nadziei Data Engineera
Confitura 2018 — Apache Beam — Promyk Nadziei Data EngineeraPiotr Wikiel
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextPrateek Maheshwari
 
Mass Report Generation Using REST APIs
Mass Report Generation Using REST APIsMass Report Generation Using REST APIs
Mass Report Generation Using REST APIsSalesforce Developers
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersoazabir
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisationgrooverdan
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA PresentationRob Tweed
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow enginedmoebius
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011rob_dimarco
 
NGINX Can Do That? Test Drive Your Config File!
NGINX Can Do That? Test Drive Your Config File!NGINX Can Do That? Test Drive Your Config File!
NGINX Can Do That? Test Drive Your Config File!Jeff Anderson
 
Automating Software Development Life Cycle - A DevOps Approach
Automating Software Development Life Cycle - A DevOps ApproachAutomating Software Development Life Cycle - A DevOps Approach
Automating Software Development Life Cycle - A DevOps ApproachAkshaya Mahapatra
 
NoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATNoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATHenryBowers
 
Yaetos Tech Overview
Yaetos Tech OverviewYaetos Tech Overview
Yaetos Tech Overviewprevota
 
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...Chris Fregly
 
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Masahiro Nagano
 
Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarOrient Technologies
 

Ähnlich wie Faster PHP apps using Queues and Workers (20)

Node, can you even in CPU intensive operations?
Node, can you even in CPU intensive operations?Node, can you even in CPU intensive operations?
Node, can you even in CPU intensive operations?
 
Introduction to LAVA Workload Scheduler
Introduction to LAVA Workload SchedulerIntroduction to LAVA Workload Scheduler
Introduction to LAVA Workload Scheduler
 
Confitura 2018 — Apache Beam — Promyk Nadziei Data Engineera
Confitura 2018 — Apache Beam — Promyk Nadziei Data EngineeraConfitura 2018 — Apache Beam — Promyk Nadziei Data Engineera
Confitura 2018 — Apache Beam — Promyk Nadziei Data Engineera
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's Next
 
Mass Report Generation Using REST APIs
Mass Report Generation Using REST APIsMass Report Generation Using REST APIs
Mass Report Generation Using REST APIs
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of users
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisation
 
About Node.js
About Node.jsAbout Node.js
About Node.js
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow engine
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011Deferred Processing in Ruby - Philly rb - August 2011
Deferred Processing in Ruby - Philly rb - August 2011
 
NGINX Can Do That? Test Drive Your Config File!
NGINX Can Do That? Test Drive Your Config File!NGINX Can Do That? Test Drive Your Config File!
NGINX Can Do That? Test Drive Your Config File!
 
Automating Software Development Life Cycle - A DevOps Approach
Automating Software Development Life Cycle - A DevOps ApproachAutomating Software Development Life Cycle - A DevOps Approach
Automating Software Development Life Cycle - A DevOps Approach
 
NoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATNoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RAT
 
Yaetos Tech Overview
Yaetos Tech OverviewYaetos Tech Overview
Yaetos Tech Overview
 
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...
PipelineAI + TensorFlow AI + Spark ML + Kuberenetes + Istio + AWS SageMaker +...
 
To AWS with Ansible
To AWS with AnsibleTo AWS with Ansible
To AWS with Ansible
 
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015
 
Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - Webinar
 

Kürzlich hochgeladen

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Servicesexy call girls service in goa
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxellan12
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsThierry TROUIN ☁
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$kojalkojal131
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607dollysharma2066
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkataanamikaraghav4
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...Diya Sharma
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts servicesonalikaur4
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebJames Anderson
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Roomgirls4nights
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 

Kürzlich hochgeladen (20)

VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Model Towh Delhi 💯Call Us 🔝8264348440🔝
 
AlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with FlowsAlbaniaDreamin24 - How to easily use an API with Flows
AlbaniaDreamin24 - How to easily use an API with Flows
 
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
Call Girls Dubai Prolapsed O525547819 Call Girls In Dubai Princes$
 
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls KolkataLow Rate Call Girls Kolkata Avani 🤌  8250192130 🚀 Vip Call Girls Kolkata
Low Rate Call Girls Kolkata Avani 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls KolkataRussian Call Girls in Kolkata Samaira 🤌  8250192130 🚀 Vip Call Girls Kolkata
Russian Call Girls in Kolkata Samaira 🤌 8250192130 🚀 Vip Call Girls Kolkata
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Porur Phone 🍆 8250192130 👅 celebrity escorts service
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girls
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Rohini 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 

Faster PHP apps using Queues and Workers

  • 1. QUEUES & WORKERS FASTER PHP APPS USING PHP DORSET 6/6/16
  • 4. BACKGROUND MOBILE API ▸ 140+ apps ▸ ~4.5m users ▸ REST/JSON ▸ Apache + PHP + ElasticSearch ▸ 20+ servers / various tasks ▸ Hosted on AWS
  • 7. ELIMINATE EXPENSIVE, TIME CONSUMING OPERATIONS AND CALLS TO EXTERNAL SERVICES
  • 9. WHAT TO QUEUE MYSQL AS A QUEUE job_id job_name data status 1 sendEmail {payload} completed 2 sendPush {payload} processing 3 requestThing {payload} waiting 4 resizeSelfie {payload} waiting
  • 10. WHAT TO QUEUE WHY NOT USE TRANSACTIONS TO OBTAIN A LOCK?
  • 11. THE DATABASE IS NOT A QUEUE. THE DATABASE IS NOT A QUEUE. Stephen Corona WHAT TO QUEUE https://www.scalingphpbook.com
  • 13. QUEUES & WORKERS MESSAGE QUEUES VS JOB QUEUES ▸ Kind of similar ▸ Most job queues built upon some kind of message queue ▸ Broker messages between systems ▸ Provide transport, storage and protocol ▸ Job queues abstract the lower level message component ▸ Integrate with most applications fairly easily
  • 14. QUEUES & WORKERS JOBS VS SCHEDULED TASKS ▸ Run at a predefined point in time ⏰ ▸ May repeat at regular intervals or according to calendar 📅 ▸ Typically triggered by cron or other scheduler 🔫 ▸ Scheduled tasks can trigger jobs! 💥
  • 15. CHOOSE A QUEUE WITH FEATURES BEST SUITED TO YOUR APPLICATION
  • 16. CHOOSING A JOB QUEUE CONSIDERATIONS ▸ Job priority & time sensitivity ▸ Job ordering and consistency (FIFO) ▸ Payload size limits ▸ Message data type & protocol ▸ Support for other languages / client libraries ▸ Failure management / retry policy ▸ Fault tolerance & redundancy ▸ One-time delivery guarantee ▸ Monitoring & statistics ▸ Distribution by task to specific workers e.g. Video encoding
  • 17. CHOOSING A JOB QUEUE BEANSTALKD ▸ Protocol similar to Memcached ▸ Clients need to know about all Beanstalkd servers (like memcached!) ▸ Beanstalkd servers can persist jobs, handle restarts without losing jobs ▸ Uses “tubes” to differentiate different queues ▸ Supports job TTR. Failed/hung jobs get put back into queue. ▸ Supports blocking. Client connects and waits for a new job. ▸ Requires setup and maintenance ▸ Loads of client libraries http://kr.github.io/beanstalkd/
  • 18. CHOOSING A JOB QUEUE AMAZON SQS ▸ SAAS - Already using AWS, literally no setup ▸ Massively redundant, cheap, maintenance free ▸ HTTP/JSON under the hood, simple ▸ Supports long-polling. ▸ Best effort FIFO (no guarantees) ▸ No concept of job priority. Use different queues. ▸ Retry policy allows jobs to reappear in queue if not completed in specified time ▸ Configurable number of retries ▸ Queue stats and alarms integrate with autoscaling ▸ Scale worker instances based on queue length/backlog/rate https://aws.amazon.com/sqs/
  • 19. CHOOSING A JOB QUEUE OTHER POPULAR QUEUES ▸ Celery (backed by RabbitMQ) - http://www.celeryproject.org ▸ php-resque (backed by Redis) -https://github.com/ chrisboulton/php-resque ▸ Kafka - http://kafka.apache.org ▸ Gearman - http://gearman.org ▸ Iron.io (SAAS) - https://www.iron.io ▸ Loads more - www.queues.io
  • 21. PROCESSING JOBS WORKER PROCESS ▸ Essentially an infinite loop ▸ Executed on command line ▸ Asks queue for new job ▸ Resolves job method ▸ Execute with payload ▸ Delete job from queue ▸ Repeat
  • 22. PROCESSING JOBS <?php $queue = new Queue(); while(true) { $job = $queue->pop('queue-name'); try { if ($job->execute()) { $job->delete(); } else { $job->release(); } } catch (Exception $e) { $job->release(); } }
  • 25. PROCESSING JOBS PROCESS CONTROL EXTENSIONS (PCNTL) ▸ Respond to unix process signals ▸ Gracefully stop worker processes ▸ Complete current job before exiting ▸ Careful if using Apache mod_php on same server ▸ http://php.net/manual/en/book.pcntl.php
  • 26. IMPROVED WORKER <?php namespace Demo; use DemoQueueQueueInterface; class Worker { protected $shouldRun = true; protected $queue; public function __construct(QueueInterface $queue) { declare(ticks = 1); $this->queue = $queue; pcntl_signal(SIGTERM, [$this, 'signalHandler']); pcntl_signal(SIGINT, [$this, 'signalHandler']); pcntl_signal(SIGQUIT, [$this, 'signalHandler']); }
  • 27. public function run($queueName) { echo "Starting worker on queue '{$queueName}' n"; while ($this->shouldRun) { $job = $this->queue->pop($queueName); try { if ($job->execute()) { $job->delete(); } else { $job->release(); } } catch (Exception $e) { $job->release(); error_log($e->getTraceAsString()); } pcntl_signal_dispatch(); } } public function signalHandler($signal) { switch ($signal) { case SIGTERM: case SIGINT: case SIGQUIT: echo "Job completed. Exiting... n"; $this->shouldRun = false; break; } } }
  • 28. IMPROVED WORKER WTF IS DECLARE(TICKS = 1);? ▸ Officially deprecated ▸ Triggered after php has executed a certain number of statements ▸ Interacts with pcntl_signal_dispatch() ▸ I admit i’ve not fully tested this with PHP7.0
  • 30. PROCESSING JOBS SUPERVISOR ▸ Process manager in similar vein to forever, pm2, php-fpm ▸ Runs as service ▸ Starts and restarts php worker processes ▸ Has CLI client (supervisorctl) ▸ Web interface ▸ Easy to install and configure http://supervisord.org
  • 31. PROCESSING JOBS SUPERVISOR CONFIG [program:alertworker] command = /usr/bin/php /path/to/queueRunner.php -q=prod-alerts autorestart = true autostart = true directory = /path/to/scripts environment = DEPLOYMENT='production' exitcodes = 0,2 numprocs = 1 numprocs_start = 0 priority = 999 startretries = 3 startsecs = 4 stderr_capture_maxbytes = 1MB stderr_events_enabled = false stderr_logfile = AUTO stderr_logfile_backups = 10 stderr_logfile_maxbytes = 50MB stderr_syslog = false stdout_capture_maxbytes = 1MB stdout_events_enabled = true stdout_logfile = AUTO stdout_logfile_backups = 10 stdout_logfile_maxbytes = 40MB stdout_syslog = false stopsignal = TERM stopwaitsecs = 10 umask = 022 user = worker