這篇文章主要介紹了關於PHP操作Beanstalkd的方法及參數注釋,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
Beanstalkd介紹
什麼是Beanstalkd
Beanstalk,一個高效能、輕量級的分布式記憶體隊列系統,最初設計的目的是想通過後台非同步執行耗時的任務來降低高容量Web應用系統的頁面訪問延遲,支援過有9.5 million使用者的Facebook Causes應用。
後來開源,現在有PostRank大規模部署和使用,每天處理百萬級任務。Beanstalkd是典型的類Memcached設計,協議和使用方式都是同樣的風格,所以使用過memcached的使用者會覺得Beanstalkd似曾相識。
Beanstalkd運行流程
job典型的生命週期
put reserve delete -----> [READY] ---------> [RESERVED] --------> *poof*
job可能的狀態遷移
put with delay release with delay ----------------> [DELAYED] <------------. | | kick | (time passes) | | | put v reserve | delete -----------------> [READY] ---------> [RESERVED] --------> *poof* ^ ^ | | | \ release | | | `-------------' | | | | kick | | | | bury | [BURIED] <---------------' | | delete `--------> *poof*
Beanstalkd安裝
安裝略過,如需請參考 點擊參考
註:需要安裝Composer,教程連結 Composer安裝方法
Pheanstalk安裝
用於操作Beanstalkd
的PHP
第三方庫,安裝略過,如需請參考 點擊參考
Pheanstalk操作
串連Beanstalkd
<?phprequire __DIR__ . '/vendor/autoload.php';use Pheanstalk\Pheanstalk;/** * 執行個體化beanstalk * 參數依次為:ip地址 連接埠號碼預設11300 連線逾時時間 是否長串連 */$pheanstalk = new Pheanstalk('127.0.0.1', 11300, 3, false);?>
Beanstalkd狀態方法
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 ))
當前的管道列表
$listTubes = $pheanstalk->listTubes();
查看管道的詳細資料
$stats = $pheanstalk->statsTube();
返回:
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);
Beanstalkd生產者方法
指定需要使用的管道
$tube = $pheanstalk->useTube('default');
向管道插入資料
$tube = $pheanstalk->useTube('default');$put = $tube->put( 'hello, beanstalk', // 任務內容 1024, // 任務的優先順序 10, // 不等待直接放到ready隊列中 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
將任務預留
$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');
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!