安装
安装 gearmand (gearman 服务端)
brew install gearmand
上面的命令会一并安装 libgearman
库,而 PHP 扩展依赖该库,因此请确保 gearmand
先于 PHP 扩展安装。直接使用 pecl
来安装 php 扩展:
pecl install gearman
基础使用
如果需要开机启动 gearman:
ln -sfv /usr/local/opt/gearman/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.gearman.plist
如果你不需要或者不想要 launchctl
, 可以通过下面的命令来启动 gearman 守护进程:
gearmand -d
命令行工具介绍
安装完毕以后会生成三个可执行文件:
- gearmand gearman 守护进程,类似
mysqld
命令 - gearadmin 如文件名,用于管理 gearman,类似
mysqladmin
命令 - gearman gearman 命令行客户端,类似
mysql
命令
gearmand
Allowed options:
Allowed options:
一般选项:
-b [ --backlog ] arg (=32) Number of backlog connections for
listen.
-d [ --daemon ] 守护进程模式。立即结束命令行,在后台运行 gearmand
--exceptions 默认启用协议层的异常 Enable protocol exceptions by default.
-f [ --file-descriptors ] arg 进程最大可用的文件描述符数(总连接数会比这个数字略少)
默认使用的是当前用户最大可用数。
-h [ --help ] 打印出这份帮助文档。
-j [ --job-retries ] arg (=0) 在任务服务器移除任务之前尝试执行的最大次数。设置
这个值这可以确保那些糟糕的任务不会 crash 掉所有
的工作线程。默认值是 0 (即不限制次数)。
--job-handle-prefix arg 用于生成任务处理器的字符串前缀。如果没有提供,
默认值是“H:<host_name>。
--hashtable-buckets arg (=991) 内部用于存放任务的哈希表(hash tables)的容量
默认使用的是 991 ,可以支持包含300万个任务的
队列。
如果预计队列中的任务数将超过300万,请按比例设
置一个更大的数字。例如,为了容纳 2^32 个任务,
本参数可设置为 1733003。这会消耗大约 26MB 的
额外内存。到目前为止,`Gearmand` 暂不支持超过
2^32 个任务的队列。
--keepalive 启用链接保持
--keepalive-idle arg (=-1) 如果启用了链接保持(--keepalive),请在支持
TCP_KEEPIDLE 的系统上设置这个参数。如果设置为
-1,则表示当系统不支持或者获取默认值事发生错误
使用该值。
If keepalive is enabled, set the value
for TCP_KEEPIDLE for systems that
support it. A value of -1 means that
either the system does not support it
or an error occurred when trying to
retrieve the default value.
--keepalive-interval arg (=-1) 如果启用了链接保持(--keepalive),请在支持
TCP_KEEPINTVL 的系统上设置这个参数。如果设
置为-1,则表示当系统不支持或者获取默认值事发
生错误使用该值。
If keepalive is enabled, set the value
for TCP_KEEPINTVL for systems that
support it. A value of -1 means that
either the system does not support it
or an error occurred when trying to
retrieve the default value.
--keepalive-count arg (=-1) 如果启用了链接保持(--keepalive),请在支持
TCP_KEEPCNT 的系统上设置这个参数。如果设
置为-1,则表示当系统不支持或者获取默认值事发
生错误使用该值。
If keepalive is enabled, set the value
for TCP_KEEPCNT for systems that
support it. A value of -1 means that
either the system does not support it
or an error occurred when trying to
retrieve the default value.
-l [ --log-file ] arg (=/usr/local/Cellar/gearman/1.1.9/var/log/gearmand.log)
用于写入错误日志和提示信息的日志文件。如果
使用 'stderr' 作为该参数的值,则会输出到
stderr。如果设置为 'none' ,则不会生成日志
文件。
Log file to write errors and
information to. If the log-file
parameter is specified as 'stderr',
then output will go to stderr. If
'none', then no logfile will be
generated.
-L [ --listen ] arg 服务器监听的地址,默认是任意地址(INADDR_ANY)
Address the server should listen on.
Default is INADDR_ANY.
-P [ --pid-file ] arg (=/usr/local/Cellar/gearman/1.1.9/var/gearmand.pid)
用于写入进程 ID 的文件
File to write process ID out to.
-r [ --protocol ] arg 加载指定协议模块Load protocol module.
-R [ --round-robin ] 按照工作线程连接服务器的顺序来分配任务。默认值
是按照函数添加到工作线程的顺序来分配任务。
Assign work in round-robin order per
worker connection. The default is to
assign work in the order of functions
added by the worker.
-q [ --queue-type ] arg (=builtin) 指定持久化队列的类型。默认使用内建的队列。
Persistent queue type to use.
--config-file arg (=/usr/local/Cellar/gearman/1.1.9/etc/gearmand.conf)
也可以使用 `@name` 来指定。
Can be specified with '@name', too
--syslog 使用系统日志(syslog)。
Use syslog.
--coredump 是否为未捕捉的信号创建一个核心转储。
Whether to create a core dump for
uncaught signals.
-t [ --threads ] arg (=4) I/O 线程的个数,如设置为 0 `gearmand`
将尝试猜测它能用的最大线程数。默认是4。
Number of I/O threads to use, 0 means
that gearmand will try to guess the
maximum number it can use. Default=4.
-u [ --user ] arg 启动后切换到给定的用户
Switch to given user after startup.
--verbose arg (=ERROR) 设置日志记录级别
Set verbose level (FATAL, ALERT,
CRITICAL, ERROR, WARNING, NOTICE, INFO,
DEBUG).
-V [ --version ] 显示 `gearmand` 的版本号并退出。
Display the version of gearmand and exit.
-w [ --worker-wakeup ] arg (=0) 当接收到任务时唤醒多少个工作线程。默认会唤醒所
有可用的工作线程。
Number of workers to wakeup for each
job received. The default is to wakeup
all available workers.
HTTP:
--http-port arg (=8080) 监听的 HTTP 端口号 Port to listen on.
Gear:
-p [ --port ] arg (=4730) 服务将监听的端口号。(Port the server should listen on.
--ssl 启用 SSL 连接 (Enable ssl connections.
--ssl-ca-file arg 用于 SSL 连接的 CA 文件。(CA file.
--ssl-certificate arg SSL 证书 (SSL certificate.
--ssl-key arg SSL 证书的密钥。(SSL key for certificate.
builtin:
libmemcached:
--libmemcached-servers arg 要使用的 `Memcached` 服务器列表。
List of Memcached servers to use.
libsqlite3:
--libsqlite3-db arg 使用的数据库文件。Database file to use.
--store-queue-on-shutdown 关闭时持久化队列。Store queue on shutdown.
--libsqlite3-table arg (=gearman_queue)
使用的表名,默认是 `gearman_queue`。
Table to use.
首先来看看 gearadmin
命令。
Options:
--help 查看相关帮助
-h [ --host ] arg (=localhost) 要连接的 host ,默认为「localhost」(Connect to the host)
-p [ --port ] arg (=4730) 要连接到的端口号,默认为 4730( Port number or service to use for connection)
--server-version 获得 gearman 服务器的版本号
--server-verbose 获得服务器的详细设置
--create-function arg 从服务器创建一个函数
--cancel-job arg 从服务器中取消置顶的任务
--drop-function arg 从服务器中删除一个函数
--show-unique-jobs 列出服务器上不重复的任务
--show-jobs 列出服务器上所有的任务
--getpid 获取服务器的进程 ID
--status 服务器的状态
--workers 服务器的 workers
--shutdown 关闭服务器
-S [ --ssl ] 启用 SSL 链接的支持
输入 gearman
命令即可查看 gearman
的相关帮助
客户端模式: gearman [options] [<data>]
工作端模式: gearman -w [options] [<command> [<args> ...]]
客户端和工作端模式通用选项:
-f <function> - 任务使用的函数名(可用指定多个)
Function name to use for jobs (can give many)
-h <host> - 任务服务器 host
Job server host
-H - 打印这份帮助
Print this help menu
-v - 打印诊断信息到标准输出,默认为 `false`
Print diagnostic information to stdout(false)
-p <port> - 任务服务器端口号
Job server port
-t <timeout> - 超时毫秒数
Timeout in milliseconds
-i <pidfile> - 为进程创建一个 pid 文件
Create a pidfile for the process
-S - 启用 SSL 连接
Enable SSL connections
Client options:
-b - 在后台执行任务,默认为`false`(Run jobs in the background(false)
-I - 高优先级执行任务。(Run jobs as high priority
-L - 低优先级执行任务。(Run jobs as low priority
-n - 每行执行一个任务。(Run one job per line(false)
-N - 等同 `-n`,但去掉换行符。
Same as -n, but strip off the newline(false)
-P - 使用指定函数名前置处理所有输出行。
Prefix all output lines with functions names
-s - 不读取标准输入直接发送任务
Send job without reading from standard input
-u <unique> - 为任务指定一个唯一 key
Unique key to use for job
Worker options:
-c <count> - 退出前执行多少个任务。
Number of jobs for worker to run before exiting
-n - 为每一行发送数据包。Send data packet for each line(false)
-N - 等同 `-n`,但去掉换行符。
Same as -n, but strip off the newline(false)
-w - 使用工作端模式运行。(Run in worker mode(false)