Get queued

35

Transcript of Get queued

Page 1: Get queued
Page 2: Get queued

DON’T MAKE ME WAIT

Let’s get queued up

Chad Windnagle - @drmmr763

Page 3: Get queued

INTRODUCTION• Recovering Joomla Addict

(Clean 2yrs)• Joomla GSoC Admin (2012-

2015)• Joomla Resource Team Editor• Joomla!Day, JWC, & JAB

Speaker• JWC 2014 Keynote Speaker

Chad Windnagle - @drmmr763

Page 4: Get queued

JOOMLA WAS MY PLAYGROUND

Page 5: Get queued

Chad Windnagle - @drmmr763

JOOMLA INSPIRED ME

Page 6: Get queued

AUDIENCE INTRODUCTION

Chad Windnagle - @drmmr763

Page 7: Get queued

WHAT ARE QUEUES ANYWAY?

Page 8: Get queued

Chad Windnagle - @drmmr763

SYNCHRONOUS FLOWUser Sends

RequestServer Sends

ResponseResponseRendered

Page 9: Get queued

Chad Windnagle - @drmmr763

A-SYNCHRONOUS FLOW

User SendsRequest

Server SendsResponse

ResponseRendered

Background TaskInitialized

Background TaskExecuted

Notify UserTask Complete

Page 10: Get queued

Chad Windnagle - @drmmr763

WHAT SHOULD WE QUEUE?•Notifications (Email /

SMS)•Statistical Calculations

•Data Matching Algorithms

•Regularly Scheduled Tasks

• Image / Video Processing

•Data Indexing

•Traditional Page Requests

•User File Uploads•Login / Logout Functions

•File Downloads•Add to Cart Functions

•REST API Endpoints

Use Queues: Don’t Use Queues:

Page 11: Get queued

COMPONENTS OF A QUEUE SYSTEM

Chad Windnagle - @drmmr763

Page 12: Get queued

THE MESSAGE

A message is string-based content which informs the application which task to

perform, and encapsulates any data needed to perform that task.

Chad Windnagle - @drmmr763

Page 13: Get queued

{ "messageType" : "sendEmail", "email" : { "to" : "[email protected]", "from" : "[email protected]", "subject": "Hello speakers!", "body" : "Can't wait to see you all." }}

MESSAGE EXAMPLE

Chad Windnagle - @drmmr763

Page 14: Get queued

THE QUEUE / TUBE

The message queue, or tube, contains a collection of messages that need to be

processed by the queue system.

Chad Windnagle - @drmmr763

Newest OldestTypical Processing Order

Page 15: Get queued

SERVICE WORKER

The service worker is code that is responsible for executing the tasks on the queue.

Chad Windnagle - @drmmr763

Newest OldestTypical Processing Order

Page 16: Get queued

EXECUTION CYCLE

Chad Windnagle - @drmmr763

User RequestsQueueable Task

Message with DataGenerated

Message AddedTo Queue

Server BootsService Worker

Service WorkerPerforms Task

Message RemovedFrom Queue

Page 17: Get queued

QUEUE SYSTEM VENDORS

Page 18: Get queued

VARIOUS VENDORS

Beanstalkd

Laravel

Redis

Gearman

RabbitMQ

AmazonSQS

IronMQ

Symfony

Chad Windnagle - @drmmr763

Beanstalkd

Page 19: Get queued

BEANSTALKD BENEFITS

Chad Windnagle - @drmmr763

RAM Based Data Storage - High Performance!

HTTP Protocol - Allows Multi-Server Set Up

PHP Composer Library Available - Fast DevelopmentCommunity Resources (Blogs, Documentation, Etc.)

Page 20: Get queued

Chad Windnagle - @drmmr763

STACK OVERVIEWPHP Application

(Joomla!)

PHP Library (Pheanstalk)

Beanstalkd Service

Queue Jobs

Queue / Execute Job

Page 21: Get queued

Chad Windnagle - @drmmr763

apt-get install beanstalkd

INSTALLING BEANSTALKD

Page 22: Get queued

Chad Windnagle - @drmmr763

php composer.phar require pda/pheanstalk

INSTALL PHEANSTALK

Page 23: Get queued

RUNNING BEANSTALKD

Chad Windnagle - @drmmr763

./beanstalkd -l 127.0.0.1 -p 11300

Options:

-l Specify the address to listen on-p specify the port to listen on

Page 24: Get queued

CURRENT STATUS?

Chad Windnagle - @drmmr763

Beanstalkd is running:

- It can add jobs to a queue- Returns job details to service worker- Jobs can be removed from the queue

Page 25: Get queued

ADD MESSAGE TO QUEUE

Chad Windnagle - @drmmr763

<?php

use Pheanstalk\Pheanstalk;

// connect to Beanstalkd Service$pheanstalk = new Pheanstalk(‘127.0.0.1');

// add the job to the queue$pheanstalk->put(‘a special message’);

Page 26: Get queued

GET MESSAGE FROM QUEUE

Chad Windnagle - @drmmr763

<?php

use Pheanstalk\Pheanstalk;

// connect to Beanstalkd Service$pheanstalk = new Pheanstalk(‘127.0.0.1');

// get next available job from queue$job = $pheanstalk ->watch(‘default’) // specific tube ->reserve(); // job in progress

Page 27: Get queued

EXECUTE A TASK

Chad Windnagle - @drmmr763

<?php

// get the message from the queued job$jobData = $job->getData();

// prints “a special message”print $jobData;

// remove job from queue$job->delete();

Page 28: Get queued

MORE USEFUL EXAMPLE

Chad Windnagle - @drmmr763

$message = [ ‘messageType => ‘sendEmail’, ‘email’ => [ ‘to’ => ‘[email protected]’, ‘from’ => ‘[email protected]’, ‘subject => ‘Hello speakers!’, ‘body’ => ‘Please come to my session!’]];

Page 29: Get queued

MORE USEFUL EXAMPLE

Chad Windnagle - @drmmr763

<?php

// add the job to the queue$pheanstalk->put(json_encode($message));

Application code would add tasks to the queue

Page 30: Get queued

MORE USEFUL EXAMPLE

Chad Windnagle - @drmmr763

// get the message from the queued job$jobData = json_decode($job->getData());

$messageType = $jobData[‘messageType’];

if ($messageType == ‘sendEmail’ { // “to”, “from”, “subject”, & “body”$this->sendEmail($jobData[‘email’]);}

Service worker executes based on message type

Page 31: Get queued

TRIGGERING SERVICE WORKER

Chad Windnagle - @drmmr763

php ./service-worker.php

Page 32: Get queued

Chad Windnagle - @drmmr763

github.com/drmmr763/jdayfloridaPHP ProjectComposer BasedUses PheanstalkUses BeanstalkdCommand Line

UtilitiesPHP Class Message

Types

EXAMPLE GITHUB CODE

Page 33: Get queued

ANY QUESTIONS?

Chad Windnagle - @drmmr763

Page 34: Get queued

ICON CREDITS

Kirby WuSchmidt Sergey

IconSphere

Carl Holdnerss

ProSymbols

Above icons licensed via Creative Commons, sourced from TheNounProject.com

GregorCresna

rOliviaStoian

RudolfHoraczek

ProSymbols icon 54

ChangHoon

BaekDaniel Baker

Page 35: Get queued

Chad Windnagle - @drmmr763

THANK YOU!Code: github.com/drmmr763/jdayflorida

Follow Me: @drmmr763

Slides available on slideshare