Beanstalk,一個高性能、輕量級的分佈式內存隊列系統,最初設計的目的是想經過後臺異步執行耗時的任務來下降高容量Web應用系統的頁面訪問延遲,支持過有9.5 million用戶的Facebook Causes應用。
後來開源,如今有PostRank大規模部署和使用,天天處理百萬級任務。Beanstalkd是典型的類Memcached設計,協議和使用方式都是一樣的風格,因此使用過memcached的用戶會以爲Beanstalkd似曾相識。php
job典型的生命週期html
put reserve delete -----> [READY] ---------> [RESERVED] --------> *poof*
job可能的狀態遷移git
put with delay release with delay ----------------> [DELAYED] <------------. | | kick | (time passes) | | | put v reserve | delete -----------------> [READY] ---------> [RESERVED] --------> *poof* ^ ^ | | | \ release | | | `-------------' | | | | kick | | | | bury | [BURIED] <---------------' | | delete `--------> *poof*
安裝略過,如需請參考 點擊參考github
注:須要安裝Composer,教程連接 Composer安裝方法
用於操做Beanstalkd
的PHP
第三方庫,安裝略過,如需請參考 點擊參考segmentfault
<?php require __DIR__ . '/vendor/autoload.php'; use Pheanstalk\Pheanstalk; /** * 實例化beanstalk * 參數依次爲:ip地址 端口號默認11300 鏈接超時時間 是否長鏈接 */ $pheanstalk = new Pheanstalk('127.0.0.1', 11300, 3, false); ?>
Beanstalkd狀態異步
$stats = $pheanstalk->stats();
返回:分佈式
Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [current-jobs-urgent] => 0 //當前存在優先級的任務數 [current-jobs-ready] => 0 //當前準備就緒的任務數 [current-jobs-reserved] => 0 //當前處於阻塞的任務數 [current-jobs-delayed] => 0 //當前處於延遲狀態的任務數 [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 //發出reserved指令,但沒有響應的數量 [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 [hostname] => lmmlwendeMacBook-Air.local ) )
當前的管道列表memcached
$listTubes = $pheanstalk->listTubes();
查看管道的詳細信息性能
$stats = $pheanstalk->statsTube();
返回:ui
Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [name] => default //當前管道名 [current-jobs-urgent] => 0 //當前管道存在優先級的任務數 [current-jobs-ready] => 0 //當前管道準備就緒的任務書 [current-jobs-reserved] => 0 //當前管道處於阻塞的任務數 [current-jobs-delayed] => 0 //當前管道處於延遲狀態的任務數 [current-jobs-buried] => 0 //當前管道預留的任務數 [total-jobs] => 0 //當前管道總任務數 [current-using] => 1 //當前管道生產者數量 [current-watching] => 1 //當前管道消費者數量 [current-waiting] => 0 //發出reserved指令,但沒有響應的數量 [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\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [id] => 1 //任務job ID [tube] => test //所處管道 [state] => reserved //當前狀態 [pri] => 1024 //任務優先級(默認1024) [age] => 469 //任務存活時間(秒) [delay] => 0 //任務延遲時間(秒) [ttr] => 60 //任務執行時間 [time-left] => 59 //任務在reserve狀態維持的秒數 [file] => 0 // binlog-4 默認-0 [reserves] => 2 //總共reserve次數 [timeouts] => 0 //任務超時次數 [releases] => 0 //重設任務次數 [buries] => 0 //預留次數 [kicks] => 0 //釋放預留任務次數 ) )
查看任務的詳細信息(經過ID)
$job = $pheanstalk->peek(1); $job_stats = $pheanstalk->statsJob($job);
指定須要使用的管道
$tube = $pheanstalk->useTube('default');
向管道插入數據
$tube = $pheanstalk->useTube('default'); $put = $tube->put( 'hello, beanstalk', // 任務內容 1024, // 任務的優先級 10, // 不等待直接放到ready隊列中 60 // 處理任務的時間 );
或者:
$pheanstalk->putInTube('default', 'test1', 1024, 10, 60);
監聽管道
$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
將任務預留
$job = $pheanstalk->watch('default')->reserve(); $pheanstalk->bury($job);
將預留任務釋放(變爲reday狀態)
$job = $pheanstalk->peekBuried('default'); $pheanstalk->kickJob($job);
批量將預留任務釋放
$pheanstalk->userTube('default')->kick(999); //將id小於999的預留任務所有釋放
讀取當前準備就緒的任務(ready)
$job = $pheanstalk->peekReady('default');
讀取當前處於延遲狀態的任務(delayed)
$job = $pheanstalk->peekDelayed('default');
對管道設置延遲
$pheanstalk->pauseTube('default', 100); //設置100秒延遲
取消對管道的延遲
$pheanstalk->resumeTube('default');