用於PHP的Gearman Worker管理工具GearmanManager


項目地址:https://github.com/brianlmoon/GearmanManager

PHP環境要求

  • PHP 5.5.9
  • POSIX extension
  • Process Control extension
  • pecl/gearman or Net_Gearman

使用GearmanManager的理由

運行Gearman的Worker是項比較讓人討厭的任務。千篇一律的代碼...GearmanManager的目標是讓運行worker成為一項運維性任務而不是開發任務。

文件名直接對應於Gearmand服務器中的function,這種方式極大簡化了function在worker中的注冊。

工作原理

我們創建一個比如叫“worker_dir”的目錄來存放所有的worker代碼。以下寫法基於安裝了pecl/gearman擴展的情形下的寫法:

過程式的代碼:

# cat worker_dir/example_function.php

function example_function($job, &$log) {

    $workload = $job->workload();

    // do work on $job here as documented in pecl/gearman docs

    // Log is an array that is passed in by reference that can be
    // added to for logging data that is not part of the return data
    $log[] = "Success";

    // return your result for the client
    return $result;

}

該文件的代碼會在gearmand中注冊一個名叫“example_function”的function。

面向對象式的代碼:

# cat worker_dir/ExampleFunction.php

class ExampleFunction {

    public function run($job, &$log) {

        $workload = $job->workload();

        // do work on $job here as documented in pecl/gearman docs

        // Log is an array that is passed in by reference that can be
        // added to for logging data that is not part of the return data
        $log[] = "Success";

        // return your result for the client
        return $result;

    }

}

該文件的代碼會在gearmand中注冊一個名叫“ExampleFunction”的function。

更多特性

GearmanManager不止是讓創建worker更簡單,它還提供進程管理。如果進程掛掉,會重新啟動。也可以設置讓worker運行一段時間后銷毀以免內存溢出。

GearmanManager有個選項可以設置監控worker目錄,當新代碼被部署時重啟相應的worker。

在關閉GearmanManager時,它會允許worker進程完成任務的執行后退出。

高級內容

配置Worker

默認情況下,GearmanManager會確保至少有一個Worker並且會執行所有的任務。也就是默認只創建一個進程。在生產環境這很明顯不夠理想。

GearmanManager的ini配置文件中由幾段內容組成。先有全局部分:[GearmanManager] ,剩下的針對每個function可以定義一段內容。

worker_dir - 定義worker function文件存放目錄,可以通過逗號分隔的形式指定多個目錄

include - 要注冊到該服務器的function清單,表示包含worker中所有function,默認為

count - 該設置定義了用於執行所有funciton所需運行的最低worker數。例如如果設為10,將會啟動10個進程,並注冊所有的function。默認值為0。

dedicated_count - 該設置定義了專用於某一個function的進程數。比如你有5個function,設置dedicated_count = 2,即為每個function啟動了兩個進程,總共10個進程。默認值為1。

max_worker_lifetime - 設置每個worker進程的最大生命周期,單位為秒。當worker完成任務后,worker會檢測它是否運行到了最大生命周期,如果到了就會退出。manager進程就好啟動一個新進程並注冊同一個任務來替代退出的進程。默認值為1個小時。

auto_update - 如果設置為1,manager進程會啟動一個助手進程來監控worker存放目錄的變更。如果發現新代碼文件,會發送信號給父進程以便殺掉worker進程來加載新的代碼。

對於每個注冊function的worker,也有單獨的針對設置項。

count - 設置一個整數值來確保至少有多少個worker注冊了該function。因為每個進程可以注冊多個function。

dedicated_count - 設置一個整數值來確保至少有多少個進程專用於該function。該進程不會做其它事情。

日志記錄

命令行有很多選擇,通過-h可以查看。

-v :

-v Logs only information about the start up and shutdown
-vv Logs information about process creation and exiting
-vvv Logs information about workers and the work they are doing
-vvvv Logs debug information
-vvvvv Logs crazy amounts of data about all manner of things

-l 指定日志文件存放目錄。如果未指定,日志數據將被發送到stdout。也可以設置為syslog以便讓日志數據發送到syslog中。

指定Gearmand服務器

兩種方式可以指定服務器:在命令行或配置文件中。

命令行: -h [HOST[:PORT][,[HOST[:PORT]]]]。 例如: -h 10.1.1.1:4730,10.1.1.2:4730

配置文件:在 [GearmanManager] 全局段:

host - 10.1.1.1:4730,10.1.1.2:4730

; exclude - A list of workers in worker_dir to exclude ; host - The gearmand host ; log_file - Path to log file or syslog ; max_runs_per_worker - Maximum number of jobs a worker will process before restarting ; pid_file - Path to the file where the pid will be stored for the main process ; prefix - Prefix for class names used by workers ; user - System user to run as ; worker_restart_splay - Number of seconds to stagger restarting and launching of workers to prevent all workers dying a t once

運行守護進程

最簡命令:

./pecl-manager.php -c /path/to/config.ini

還有其它一些參數:

-P - 管理進程的pid文件所在目錄,也可以在配置文件中通過pid_file指定

-d - 如果在命令行指定該參數,管理器會以守護進程形式運行

-u - 指定守護進程的用戶,也可在配置文件中通過user指定

調試

GearmanManager中用了(@),造成錯誤消息不提示,相應error_log是不會記錄的,這樣調試比較困難。

解決方案就是安裝Xdebug:

  • Install Xdebug
  • Configure it
  • Profit!

安裝調試工具

pecl install xdebug

配置調試工具

加上xdebug.scream參數到 xdebug.ini中:

zend_extension="/path/to/where/your/xdebug.so"
xdebug.scream = 1
xdebug.show_exception_trace = 1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM