Speed up with Queues

Laravel Montréal #4 - November 27th, 2014

Before we start ...

Thanks for comming !

Sponsors

Jobs

Get involved

Queues

What is the idea behind a queue?

Queues allow you to defer the processing of a time consuming task, such as sending an e-mail, until a later time, thus drastically speeding up the web requests to your application.

It's like a restaurant's buzzer

About Queues

  • Unlike JavaScript, PHP is a blocking language
  • PHP must complete each line of code before it goes on the other Using any concept like a "promise" has not being an option
  • Therefore, a use must wait... All business logic must normally be completed before some feedback is sent to the user
  • Unless, you take avantage of queues !

The process of queueing

Think of a queue as a to do list

  1. You encounter a time-consuming task
  2. Rather than executing the job inmmediatly, you send it to a todo list This is called "pushing" a job to the queue
  3. Now that the todo list is populated, someone needs to execute the jobs
  4. An instance of your application, called a "worker" is always ready to receive new jobs
  5. When a new job is available, the worker executes it

See it in action

Laravel makes it amazingly easy...

It offers 4 different drivers, one simple API

Simple command line tools


php artisan queue:listen

		

php artisan queue:listen --timeout=60
	
		

php artisan queue:listen --timeout=60 --tries=3

		

What about failed jobs ?

Failed jobs


php artisan queue:failed-table

		

php artisan queue:failed

		

php artisan queue:retry 1
	
		

php artisan queue:forget 1
	
		

php artisan queue:flush

		

Supervisor

Installing


# Debian / Ubuntu:
# already installed in homestead

$ sudo apt-get install supervisor

	

Configure


# Debian / Ubuntu:
# already installed in homestead

$ sudo nano /etc/supervisor/conf.d/myqueue.conf

	

[program:myqueue]
command=php artisan queue:listen --tries=3
directory=/home/vagrant/Code/queue
stdout_logfile=/home/vagrant/Code/queue/app/storage/logs/myqueue_supervisord.log
redirect_stderr=true
autostart=true
autorestart=true

	

Boot !


$ sudo supervisorctl
> reread # Tell supervisord to check for new items in /etc/supervisor/conf.d/
> add myqueue       # Add this process to Supervisord
> start myqueue     # May say "already started"

	

Push Queues

Simple, no architechture needed


php artisan queue:subscribe queue_name http://queue.app/queue/receive

	

Route::post('queue/receive', function()
{
    return Queue::marshal();
});

	

Further Reading

That’s All Folks!