Beanstalkd简介什么是BeanstalkdBeanstalk是一个高性能、轻量级的分布式内存队列系统,最初旨在通过在后台异步执行耗时任务来减少大容量Web应用程序的页面访问系统的延迟支持了拥有950万用户的FacebookCauses应用程序。后来开源了,现在PostRank被大规模部署使用,每天处理数百万任务。Beanstalkd是典型的类Memcached设计,协议和用法都一样,所以用过memcached的用户都会觉得Beanstalkd很眼熟。Beanstalkdrunningprocessjobtypicallifecycleputreservedelete----->[READY]-------->[RESERVED]-------->*poof*jobpossiblestatetransitionputwith延迟释放----------------->[DELAYED]<------------。||踢球|(时间流逝)|||保留v|删除---------------->[准备]-------->[保留]-------->*噗*^^|||\发布|||`------------'||||踢球||||埋葬|[埋葬]<--------------'||删除`-------->*poof*Beanstalkd安装略过,如有需要请参考点击参考注:需安装Composer,教程链接Composer安装方法Pheanstalk安装PHP第三方库进行操作Beanstalkd,安装略过但是,如果需要,请参考点击参考Pheanstalk操作连接BeanstalkdBeanstalkd状态方法Beanstalkd状态$stats=$pheanstalk->stats();return:Pheanstalk\Response\ArrayResponseObject([_name:Pheanstalk\Response\ArrayResponse:private]=>OK[storage:ArrayObject:private]=>Array([current-jobs-urgent]=>0//当前人数taskswithpriority[current-jobs-ready]=>0//当前就绪的任务数[current-jobs-reserved]=>0//当前阻塞的任务数[current-jobs-delayed]=>0//当前处于延迟状态的任务数Numberoftasks[current-jobs-buried]=>0//当前保留的任务数[cmd-put]=>0//cmd命令是累计运行时间[cmd-peek]=>0[cmd-peek-ready]=>0[cmd-peek-delayed]=>0[cmd-peek-buried]=>0[cmd-reserve]=>0[cmd-reserve-with-timeout]=>0[cmd-delete]=>0[cmd-release]=>0[cmd-use]=>0[cmd-watch]=>0[cmd-ignore]=>0[cmd-bury]=>0[cmd-kick]=>0[cmd-touch]=>0[cmd-stats]=>1[cmd-stats-job]=>0[cmd-stats-tube]=>0[cmd-list-tubes]=>3[cmd-list-tube-used]=>0[cmd-list-tubes-watched]=>0[cmd-pause-tube]=>0[job-timeouts]=>0//超时任务[total-jobs]=>0//总计任务数[max-job-size]=>65535//任务字符串大小[current-tubes]=>1//当前管道数[current-connections]=>1//当前打开的连接数[current-producers]=>0//当前生产者的数量[current-workers]=>0//当前消费者数量[current-waiting]=>0//发出保留命令但没有响应的数量[total-connections]=>4//累计连接数[pid]=>530//Beanstalkd进程id[version]=>1.10[rusage-utime]=>0.003545[rusage-stime]=>0.007473[uptime]=>105855//运行时间(秒)[binlog-oldest-index]=>0[binlog-current-index]=>0[binlog-records-migrated]=>0[binlog-records-written]=>0[binlog-max-size]=>10485760[id]=>4199e3eca8bfdea8[主机名]=>lmmlwendeMacBook-Air.local))当前管道列表$listTubes=$pheanstalk->listTubes();查看管道的详细信息$stats=$pheanstalk->statsTube();返回:Pheanstalk\Response\ArrayResponseObject([_name:Pheanstalk\Response\ArrayResponse:private]=>OK[storage:ArrayObject:private]=>Array([name]=>default//当前管道名称[current-jobs-urgent]=>0//当前pipeline中有优先级的任务数[current-jobs-ready]=>0//当前pipeline准备好的任务本[current-jobs-reserved]=>0//当前被管道阻塞的任务数[current-jobs-delayed]=>0//当前被管道延迟的任务数[current-jobs-buried]=>0//数量oftasksreservedbythecurrentpipeline[total-jobs]=>0//当前管道任务总数[current-using]=>1//当前管道生产者数量[current-watching]=>1//当前管道消费者数量[current-waiting]=>0//发出保留命令但没有响应的数量[cmd-delete]=>0[cmd-pause-tube]=>0[pause]=>0[pause-time-left]=>0))查看任务详情$job=$pheanstalk->watch('default')->reserve();$job_stats=$pheanstalk->statsJob($job);返回:Pheanstalk\Response\ArrayResponseObject([_name:Pheanstalk\Response\ArrayResponse:private]=>OK[storage:ArrayObject:private]=>Array([id]=>1//taskjobID[tube]=>测试//管道[state]=>reserved//当前状态[pri]=>1024//任务优先级(默认1024)[age]=>469//任务生存时间(秒)[delay]=>0//任务延迟时间(seconds)[ttr]=>60//任务执行时间[time-left]=>59//任务保持保留状态的秒数[file]=>0//binlog-4default-0[reserves]=>2//reserves总数[timeouts]=>0//任务超时次数[releases]=>0//重置任务次数[buries]=>0//reservations次数[kicks]=>0//release保留任务数))查看任务的详细信息(按ID)$job=$pheanstalk->peek(1);$job_stats=$pheanstalk->statsJob($job);Beanstalkd生产者方法指定要使用的管道$tube=$pheanstalk->useTube('default');将数据插入管道$tube=$pheanstalk->useTube('default');$put=$tube->put('hello,beanstalk',//任务内容1024,//任务的优先级为10,//不等待直接放入就绪队列60//处理任务的时间任务);或者:$pheanstalk->putInTube('default','test1',1024,10,60);Beanstalkd消费者监控管道的方法$tube=$pheanstalk->watch('user');移除不需要监控的管道$tube=$Pheanstalk->watch('user')->ignore('default');以阻塞方式监控管道$job=$pheanstalk->watch('user')->reserve(4);//阻塞时间为4秒列出所有被监控的管$pheanstalk->listTubesWatched();watch+reserve方法$pheanstalk->reserveFromTube('default')删除当前任务$job=$pheanstalk->watch('default')->reserve();$pheanstalk->delete($job);将当前任务放回管道$job=$pheanstalk->watch('default')->reserve();$pheanstalk->release($job);对于任务更新(当任务处理时间小于当前任务执行时间时)$job=$pheanstalk->watch('default')->reserve();$pheanstalk->touch($job);//TODO将保留任务Leave$job=$pheanstalk->watch('default')->reserve();$pheanstalk->bury($job);释放保留的任务(更改为重日状态)$job=$pheanstalk->peekBuried('default');$pheanstalk->kickJob($job);批量释放预留任务$pheanstalk->userTube('default')->kick(999);//释放所有id小于999的保留任务读取当前就绪的任务(ready)$job=$pheanstalk->peekReady('default');读取当前延迟的任务(delayed)$job=$pheanstalk->peekDelayed('default');是Pipelinesetupdelay$pheanstalk->pauseTube('defaultt',100);//设置延迟100秒,取消管道的延迟$pheanstalk->resumeTube('default');
