原文地址:http://fideloper.com/ubuntu-beanstalkd-and-laravel4

Note: TL;DR version at the bottom!

Queues are a great way to take some task out of the user-flow and put them in the background. Allowing a user to skip waiting for these tasks makes our applications appear faster, and gives us another opportunity to segment our application and business logic out further.

For example, sending emails, deleting accounts and processing images are all potentially long-running or memory-intensive tasks; They make great candidates for work which we can off-load to a queue.

Laravel can accomplish this with its Queue package. Specifically, I use the Beanstalkd work queue with Laravel.

Here's how I set that up to be just about production-ready.

Note: I use Ubuntu for development and often in production. The following is accomplishsed in Ubuntu 12.04 Server LTS. Some instructions may differ for you depending on your OS

Here's what we'll cover:

  1. Laravel and Queues
  2. Installing Beanstalkd
  3. Churning through the queue with Supervisor

Laravel and Queues

Laravel makes using queues very easy. Our application, the "producer", can simply run something likeQueue::push('SendEmail', array('message' => $message)); too add a "job" to the queue.

On the other end of the queue is the code listening for new jobs and a script to process the job (collectively, the "workers"). This means that in addition to adding jobs to the queue, we need to set up a worker to pull from the stack of available jobs.

Here's how that looks in Laravel. In this example, we'll create an image-processing queue.

Install dependencies

As noted in the docs, Laravel requires the Pheanstalk package for using Beanstalkd. We can install this using Composer:

$ composer require pda/pheanstalk:dev-master

Create a script to process it

Once our PHP dependency in installed, we can begin to write some code. In this example, we'll create aPhotoService class to handle the processing. If no method is specified, laravel assumes the class will have a fire() method. This is half of a worker - the code which does some processing.

<?php namespaceMyapp\Queue;classPhotoService{publicfunction fire($job, $data){// Minify, crop, shrink, apply filters or otherwise manipulate the image}}

Push a job to a Queue

When a user uploads an image, we'll add a job to the queue so our worker can process it.

In Laravel, we'll create a job by telling the Queue library what code will handle the job (in this case thefire() method inside of Myapp\Queue\PhotoService as defined above) and give it some data to work with. In our example, we simply pass it a path to an image file.

Queue::push('Myapp\Queue\PhotoService', array('image_path'=>'/path/to/image/file.ext'));

Process the jobs

At this point, we have code to process an image (most of a worker), and we've added a job to the queue. The last step is to have code pull a job from the queue.

This is the other half of a worker. The worker needs to both pull a job from the queue and do the processing. In Laravel, that's split into 2 functionalities - Laravel's queue listener, and the code we write ourselves - in this case, the PhotoService.

Laravel has some CLI tools to help with queues:

// Fire the latest job in the queue
$ php artisan queue:work // Listen for new jobs in the queue// and fire them off one at a time// as they are created
$ php artisan queue:listen

When not working with the "sync" driver, these tools are what you need to use in order to process the jobs in your queue. We run the queue:listen command to have laravel listen to the queue and pull jobs as they become available.

Let's install Beanstalkd to see how that works.

By default, laravel will run queue jobs synchronously - that is, it runs the job at the time of creation. This means the image will be processed in the same request that the user created when uploading an image. That's useful for testing, but not for production. We'll make this asynchronous by introducing Beanstalkd.

Beanstalkd

Let's install Beanstalkd:

# Debian / Ubuntu:
$ sudo apt-get update
$ sudo apt-get install beanstalkd

Note: You may be able to get a newer version of Beanstalkd by adding this PPA. Ubuntu 12.04 installs an older version of Beanstalkd.

Next, some quick configuration. The first thing we need to do is tell Beanstalkd to start when the system starts up or reboots. Edit /etc/default/beanstalkd and set START to "yes".

$ sudo vim /etc/default/beanstalkd
> START yes # uncomment

Then we can start Beanstalkd:

$ sudo service beanstalkd start
# Alternatively: /etc/init.d/beanstalkd start

Now we can setup Laravel. In your app/config/queue.php file, set the default queue to 'beanstalkd':

'default'=>'beanstalkd',

Then edit any connection information you need to change. I left my configuration with the defaults as I installed it on the same server as the application.

'connections'=> array('beanstalkd'=> array('driver'=>'beanstalkd','host'=>'localhost','queue'=>'default',),),

Now when we push a job to the queue in Laravel, we'll be pushing to Beanstalkd!

Installing Beanstalkd on a remote server

You may (read: should) want to consider installing Beanstalkd on another server, rather than your application server. Since Beantalkd is an in-memory service, it can eat up your servers resources under load.

To do this, you can install Beanstalkd on another server, and simply point your "host" to the proper server address, rather than localhost.

This leaves the final detail - what server runs the job? If you follow all other steps here, Supervisord will still be watching Laravel's listener on your application server. You may want to consider running your job script (or even a copy of your application which has a job script) on yet another server whose job is purely to churn through Beanstalkd queue jobs. This means having a listener and working listener/job code on yet another server.

In fact, in a basic distributed setup, we'd probably have an application server (or 2, plus a load-balancer), a database server, a queue server and a job server!

Supervisord

Let's say you pushed a job to Beanstalkd:

Queue::push('Myapp\Queue\PhotoService', array('image_path'=>'/path/to/image/file.ext'));

Now what? You might notice that it goes to Beanstalkd, but Myapp\Queue\PhotoService@fire() doesn't seem to be getting called. You've checked your error logs, you see if the image was edited, and found that the the job is just "sitting there" in your Beanstalkd queue.

Beanstalkd doesn't actually PUSH jobs to a script - instead, we need a worker to check if there are jobs available and ask for them.

This is what $ php artisan queue:listen does - It listens for jobs and runs them as they become available.

If you run that command, you'll see your job being sent to code. If all goes well, your image will beproperly manipulated.

The question then becomes: How do I make php listen at all times? We need to avoid having to "supervise" that process manually. This is where Supervisord comes in.

Supervisord will watch our queue:listen command and restart it if it fails. Let's see how to set that up.

First, we'll install it:

# Debian / Ubuntu:
$ sudo apt-get install supervisor

Next, we'll configure it. We need to define a process to listen to.

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

Add this to your new conf file, changing file paths and your environment as necessary:

[program:myqueue]
command=php artisan queue:listen --env=your_environment
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/myqueue_supervisord.log
redirect_stderr=true

We now have a process called "myqueue" which we can tell Supervisord to start and monitor.

Let's do that:

$ 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"

Now the "myqueue" process is on and being monitored. If our queue listener fails, Supervisord will restart the php artisan queue:listen --env=your_environment process.

You can check that it is indeed running that process with this command:

$ ps aux | grep php

# You should see some output like this:
php artisan queue:listen --env=your_environment
sh -c php artisan queue:work --queue="default"--delay=0--memory=128--sleep --env=your_environment
php artisan queue:work --queue=default--delay=0--memory=128--sleep --env=your_environment

Wrapping up

Now we have a full end-to-end queue working and in place!

  1. We create a script to process a queued job
  2. We installed Beanstalkd to act as the work queue
  3. We use Laravel to push jobs to our queue
  4. We use Laravel queue:listen to act as a worker and pull jobs from the queue
  5. We wrote some code to process a job from the queue
  6. We use Supervisord to ensure queue:listen is always listening for new jobs

Notes

  1. You might want to consider setting up log rotation on the Laravel and Supervisord logs
  2. You can read here for more information on setting up Supervisord on Ubuntu.
  3. Read the Laravel docs on queues to learn how and when to release or delete jobs.

TL;DR

For reference, just copy and paste the whole process from here:

$ sudo apt-get update
$ sudo apt-get install -y beanstalkd supervisor
$ sudo vim /etc/default/beanstalkd
> START yes # uncomment this line
$ sudo service beanstalkd start
$ sudo vim /etc/supervisor/conf.d/myqueue.conf

Enter this, changing as needed:

[program:myqueue]
command=php artisan queue:listen --env=your_environment
directory=/path/to/laravel
stdout_logfile=/path/to/laravel/app/storage/logs/myqueue_supervisord.log
redirect_stderr=true

Start Supervisord:

$ sudo supervisorctl
> reread # Get available jobs> add myqueue
> start myqueue

Read more on Supervisord here for info on supervisorctl.

另外还有一篇相关的不错的文章:http://www.lornajane.net/posts/2014/working-with-php-and-beanstalkd

Production-Ready Beanstalkd with Laravel 4 Queues的更多相关文章

  1. Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready

    Getting Node.js Apps Production Ready 部署程序需要知道的方面: Environment variables Express.js in production So ...

  2. CNCF CloudNative Landscape

    cncf landscape CNCF Cloud Native Interactive Landscape 1. App Definition and Development 1. Database ...

  3. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  4. 【Kubernetes 系列二】从虚拟机讲到 Kubernetes 架构

    目录 什么是虚拟机? 什么是容器? Docker Kubernetes 架构 Kubernetes 对象 基础设施抽象 在认识 Kubernetes 之前,我们需了解下容器,在了解容器之前,我们得先知 ...

  5. CNCF LandScape Summary

    CNCF Cloud Native Interactive Landscape 1. App Definition and Development 1. Database Vitess:itess i ...

  6. Spring Boot文档

    本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...

  7. Upgrade from SharePoint 2010 to SharePoint 2016

    [转]http://nikcharlebois.com/upgrade-from-sharepoint-2010-to-sharepoint-2016/ In this blog, I will go ...

  8. I finally made sense of front end build tools. You can, too.

    来源于:https://medium.freecodecamp.com/making-sense-of-front-end-build-tools-3a1b3a87043b#.nvnd2vsd8   ...

  9. redis该如何分区-译文(原创)

    写在最前,最近一直在研究redis的使用,包括redis应用场景.性能优化.可行性.这是看到redis官网中一个链接,主要是讲解redis数据分区的,既然是官方推荐的,那我就翻译一下,与大家共享. P ...

随机推荐

  1. 关于css浮动框是否脱离文档流的分析

    在了解浮动属性之前,首先我们先了解一下html中关于display属性的相关知识. display属性常用的有inline, block, inline-block. inline也就是内联的意思. ...

  2. 【Echo】实验 -- 实现 C/C++下UDP, 服务器/客户端 通讯

    本次实验利用UDP协议, 语言环境为 C/C++ 利用套接字Socket编程,实现Server/CLient 之间简单的通讯. 结果应为类似所示: 下面贴上代码(参考参考...) Server 部分: ...

  3. JAVA-2NIO之Channel

    注意:转载自并发编程网 – ifeve.com本文链接地址: Java NIO系列教程(二) Channel Channel Java NIO的通道类似流,但又有些不同: 既可以从通道中读取数据,又可 ...

  4. WCF-异步调用和两种客户端形式

    当发布一个服务端之后,客户端可以通过服务端的元数据,用VS2010添加服务引用的方式生成对应的代码.并且可以选择生成相应的异步操作. WCF实现代码,Add操作延时5秒后再返回结果. [Service ...

  5. SQL SERVER学习1——数据库概念

    <SQL Server实例教程>(科学出版社) 数据库的基本概念 数据是载荷信息的物理符号,是数据库中存储的基本对象. 信息可以通过手势,眼神表达,但是表达信息的最佳方式还是数据. 数据有 ...

  6. git merge后,后悔了如何回退

    今天将feature分支的代码merge到develop分支后我后悔了,因为feature分支的功能还没有全部开发完成,我在feature分支上commit是可以的,但是这之后我又把它merge到了d ...

  7. oracle锁表问题解决

    select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$session s where l ...

  8. MapReduce详解和WordCount模拟

    最早接触大数据,常萦绕耳边的一个词「MapReduce」.它到底是什么,能做什么,原理又是什么?且听下文讲解. 是什么 MapReduce 即是一个编程模型,又是一个计算框架,它充分采用了分治的思想, ...

  9. HBase—列族数据库的术语

    1. 列族数据库的基本组件 键空间,行键,列,列族 2. 什么是键空间 keyspace? 键空间 keyspace 是列族数据库的顶级数据结构,它在逻辑上能够容纳列族,行键以及与之相关的其他数据结构 ...

  10. python学习之老男孩python全栈第九期_day026知识点总结——封装、property、类方法、初识反射

    一. 封装 class Room: def __init__(self, name, length, width): self.__name = name self.__length = length ...