Top Banner
It’s all about eXperience Gaetano Giunta PHP London July 2015 Making Symfony Services async with RabbitMq (and more Symfony) PechaKucha style!
15

Making Symfony Services async with RabbitMq (and more Symfony)

Aug 08, 2015

Download

Internet

Gaetano Giunta
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Making Symfony Services async with RabbitMq (and more Symfony)

It’s all about eXperience

Gaetano Giunta

PHP LondonJuly 2015

Making Symfony Services async with

RabbitMq

(and more Symfony)

PechaKucha style!

Page 2: Making Symfony Services async with RabbitMq (and more Symfony)

2

A case study: generating M$Office docs

The needs

• Produce content in Microsoft Office formats• Many formats for each document

• Both Word and Excel docs

• XML content generated by CMS

• LibreOffice used to generate MS Office and PDF versions• Has anyone tried generating OOXML by hand?

Page 3: Making Symfony Services async with RabbitMq (and more Symfony)

3

A case study: generating M$Office docs

Reality bites

• Slooow• Rest assured that some of the docs will be pretty big

• Unreliable• Depending on version, LO crashes from a-bit to almost-

always

• Race-prone• Two conversion jobs in parallel might step on each other

Page 4: Making Symfony Services async with RabbitMq (and more Symfony)

4

A case study: generating M$Office docs

…and the results are:

• Ugly code: lots of polling, copying of files around, manual locking

• Does not scale at all: only one conversion process active at any given time

Web server

PHP LibreOffice

Waiting processes

Page 5: Making Symfony Services async with RabbitMq (and more Symfony)

5

TextBook scenario for introducing queues

Web server

Worker?

RabbitMQ

LibreOffice

PHP

Page 6: Making Symfony Services async with RabbitMq (and more Symfony)

6

TextBook scenario for introducing queues

Web server

Worker?

RabbitMQ

LibreOffice

PHP

Page 7: Making Symfony Services async with RabbitMq (and more Symfony)

7

TextBook scenario for introducing queues

Web server

PHP!

RabbitMQ

LibreOffice

PHP

Page 8: Making Symfony Services async with RabbitMq (and more Symfony)

8

TextBook scenario for introducing queues

Web server

PHP!

RabbitMQ

LibreOffice

PHP

Page 9: Making Symfony Services async with RabbitMq (and more Symfony)

9

TextBook scenario for introducing queues

Web server

Symfony!!!

RabbitMQ

LibreOffice

PHP

Page 10: Making Symfony Services async with RabbitMq (and more Symfony)

10

TextBook scenario for introducing queues

Web server

Symfony!!!

RabbitMQ

LibreOffice

PHP

Page 11: Making Symfony Services async with RabbitMq (and more Symfony)

11

The rationale

• Same library handling both sides of the connection• Easier to troubleshoot

• Sf Console component is lovely

• Everything developed in a single repository

• No need to learn new languages

• Everything which I can rewrite in PHP, I eventually will*

* = 3rd whimsical law of Gaetano

Page 12: Making Symfony Services async with RabbitMq (and more Symfony)

12

The details

• Json used as payload encoding• The worker has been made thread-safe

• Again, thanks to Symfony: the « Process » Component• It spins up a new php process each time it receives a command• The new process executes the command

• Bonus: a ‘watchdog’ to restart workers via crontab if they die

Symfonylistener

Symfonylistener

Symfonylistener

Symfonyworker

LibreOffice

LibreOffice

W

Page 13: Making Symfony Services async with RabbitMq (and more Symfony)

13

The evolution

• What is exactly a « queued command » ?

• For starters, each existing Symfony console command!

• Why not every existing Symfony Service?• Allows to run existing services asynchronously

• As long as their arguments are serializable to json

• Not a project-specific tool any more

• Could be turned into a Bundle

• …

• Profit!

Page 14: Making Symfony Services async with RabbitMq (and more Symfony)

14

The problems

• Assumes good security on the network

• Low throughput• Spinning up a new php worker process takes time

• Could we use php-fpm to communicate to workers instead?

• Am I reinventing the wheel?

• Others?• Suggestions are welcome

Page 15: Making Symfony Services async with RabbitMq (and more Symfony)

15

I’ll be here all night

THANK YOU

https://github.com/kaliop-uk/kueueingbundle

• @gggeek• www.kaliop.co.uk