Laravel 队列[beanstalkd]实践

安装

Ubuntu 12.04.5 LTS

  1. sudo apt-get install beanstalkd
  2. vi /etc/default/beanstalkd #uncomment START=yes
  3. service beanstalkd start

安装Laravel 4.2下beanstalkd依赖包pda/pheanstalk

1
$ composer require pda/pheanstalk #输入2.1.*版本

网页控制台

1
composer create-project ptrofimov/beanstalk_console -s dev

Github beanstalk Console

配置

在 config/queue.php 中设置defautl选项为:beanstalkd

1
2
3
4
5
6
'beanstalkd' => array(
'driver' => 'beanstalkd',
'host' => 'localhost', //可以设置未第三方服务器
'queue' => 'default',
'ttr' => 60, //seconds
),

测试

  1. 在路由中
1
2
3
4
5
6
7
8
//推送一个闭包去队列,这个方法非常的方便及快速的来处理需要使用队列的简单的任务
Route::get('/', function(){
Queue::push(function($job){
Log::info('Queues test info');
$job->delete();//删除
});
return 'Job added to the queue';
});
  1. 启动Laravel queue 监听
1
2
$ php artisan queue:work #处理队列中事务
$ php artisan queue:listen #监听

处理错误队列

  1. 在数据库中生成存储queue job错误的表
1
2
$ php artisan queue:failed-table
$ php artisan migrate
  1. 在 config/queue.php 中设置failed中数据库配置项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
|--------------------------------------------------------------------------
| Failed Queue Jobs
|--------------------------------------------------------------------------
|
| These options configure the behavior of failed queue job logging so you
| can control which database and table are used to store the jobs that
| have failed. You may change them to any database / table you wish.
|
*/


'failed' => array(

'database' => 'mysql', 'table' => 'failed_jobs',

),
  1. 设置failed job重试次数
1
$ php artisan queue:listen --tries=3
  1. 命令行中查看错误列表
1
$ php artisan queue:failed #查询数据库表中错误列表

守护处理队列进程(Supervisor)

安装

1
$ sudo apt-get install supervisor

配置

新建 vi /etc/supervisor/conf.d/queue.conf

1
2
3
4
5
[program:queue]
command=php artisan queue:listen --tries=2
directory=/home/wwwroot/demo.example.com
stdout_logfile=/home/wwwlogs/demo.example.com.supervisor.log
redirect_stderr=true

使用

1
2
3
4
5
6
$ sudo supervisorctl
help
supervisor> reread
supervisor> add queue
supervisor> start queue
supervisor> status