10up/wp-gears

此包已被弃用且不再维护。作者建议使用10up/wp-minions包。

WordPress任务队列

安装: 8

依赖: 0

建议者: 0

安全: 0

星标: 231

关注者: 105

分支: 40

开放问题: 11

类型:wordpress-plugin

4.1.0 2018-11-05 20:41 UTC

This package is auto-updated.

Last update: 2024-05-02 18:52:29 UTC


README

提供了一个框架,用于使用WordPress异步任务运行的任务队列。它自带Gearman和RabbitMQ集成。

Support Level Build Status Release Version GPLv2 License

注意

截至2024年4月12日,此项目已归档,不再积极维护。

背景与目的

随着WordPress成为越来越大型出版商的流行发布平台,随着复杂工作流的增加,对越来越复杂和资源密集型任务的需求也在不断增加。像生成报告、昂贵的API调用、同步用户到邮件提供商,甚至从源中提取内容等任务都需要大量的时间或大量的内存(或两者都要),通常无法在常见的Web服务器限制内完成,因为像超时和内存限制这样的因素会阻碍。

WP Minions提供了一些辅助函数,允许您将任务添加到队列中,并指定应调用的操作以触发任务,只需使用add_action()钩子回调即可。

在配置过程中,指定了一些minions(工作进程)。由于minions是免费的,它们将从队列中取出下一个任务,调用操作,并且任何钩入操作的回调都将运行。

在需要更多RAM或更高超时的情况下,使用单独的服务器来处理任务是非常理想的——只需在该服务器上设置WordPress,就像标准Web服务器一样,并增加资源。确保不要将任何生产流量发送到该服务器,它将专门处理队列中的任务。

安装

  1. 在WordPress中安装插件。如果需要,您可以下载zip文件并通过WordPress插件安装程序安装。

  2. 在站点根目录(与wp-settings.php相同的目录)创建一个指向插件中wp-minions-runner.php文件的符号链接(或复制文件,但符号链接将确保在插件更新时更新文件)

  3. wp-config.php中定义一个唯一的salt,以确保多个安装不会冲突。

define( 'WP_ASYNC_TASK_SALT', 'my-unique-salt-1' );

注意:如果您使用的是多站点,您还必须在wp-config.php文件中添加以下内容,在多站点定义的块之后。这是由于多站点依赖于HTTP_HOST来检测它运行的站点/博客。您还希望确保在多站点配置中实际定义了DOMAIN_CURRENT_SITE

if ( ! isset( $_SERVER['HTTP_HOST'] ) && defined( 'DOING_ASYNC' ) && DOING_ASYNC ) {
  $_SERVER['HTTP_HOST'] = DOMAIN_CURRENT_SITE;
}
  1. 接下来,您需要选择您的工作队列系统。Gearman和RabbitMQ自带支持。

Gearman

要运行所有这些,有几个部分。首先,需要设置Gearman后端,这部分将根据您的操作系统而有所不同。完成后,我们可以安装WordPress插件,并设置WordPress的配置选项。

后端设置 - CentOS 6.x

  1. 您需要为gearman安装EPEL仓库,并为一些PHP包安装REMI仓库。确保启用适用于您正在使用的PHP版本的相应remi仓库。
    • wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm && rpm -Uvh epel-release-6*.rpm
    • wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && rpm -Uvh remi-release-6*.rpm
    • rm *.rpm
  2. 请确保已启用remi以及您可能希望在/etc/yum.repos.d/remi.repo中的任何特定版本的PHP。
  3. yum install gearmand php-pecl-gearman python-pip
  4. easy_install supervisor
  5. chkconfig supervisord on && chkconfig gearmand on
  6. 如果所有内容都在一个服务器上运行,我建议只限制localhost的连接。如果不是,您需要设置防火墙规则,只允许某些客户端连接到Gearman端口(默认为4730)
  • 编辑/etc/sysconfig/gearmand - 设置OPTIONS="--listen=localhost"
  1. service gearmand start

后端设置 - CentOS 7.x

  1. 您需要为gearman安装EPEL仓库,并为一些PHP包安装REMI仓库。
    • yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    • yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
  2. yum install gearmand php-pecl-gearman --enablerepo=remi-php<您的系统上的PHP版本>。例如,如果您使用的是php 7.2,您的命令将类似于以下yum install gearmand php-pecl-gearman --enablerepo=remi-php72
  3. 可选,如果您更喜欢,可以安装supervisord
    • yum install python-pip
    • easy_install supervisor
  4. 如果所有内容都在一个服务器上运行,我建议只限制localhost的连接。如果不是,您需要设置防火墙规则,只允许某些客户端连接到Gearman端口(默认为4730)
    • 编辑/etc/sysconfig/gearmand - 设置OPTIONS="--listen=localhost"
  5. systemctl enable gearmand
  6. systemctl start gearmand

后端设置 - Ubuntu

在您执行此操作的过程中,您可能需要安装额外的包,如果您还没有,例如php-pear或php*-dev包

  1. apt-get update
  2. apt-get install gearman python-pip libgearman-dev supervisor
  3. pecl install gearman
  4. pecl安装完成后,它将告诉您将类似extension=gearman.so的内容放入您的php.ini文件中 - 请这样做。
  5. update-rc.d gearman-job-server defaults && update-rc.d supervisor defaults
  6. 如果所有内容都在一个服务器上运行,我建议只限制localhost的连接。如果不是,您需要设置防火墙规则,只允许某些客户端连接到Gearman端口(默认为4730)
  • 编辑/etc/default/gearman-job-server - 设置PARAMS="--listen=localhost"
  1. service gearman-job-server restart

Supervisor配置

根据需要填写<brackets>中的值,将以下配置添加到/etc/supervisord.conf(CentOS)或/etc/supervisor/supervisord.conf(Ubuntu)

[program:my_wp_minions_workers]
command=/usr/bin/env php <path_to_wordpress>/wp-minions-runner.php
process_name=%(program_name)s-%(process_num)02d
numprocs=<number_of_minions>
directory=<path_to_temp_directory>
autostart=true
autorestart=true
killasgroup=true
user=<user>
  • path_to_wordpress: WordPress安装根目录的绝对路径,例如:/var/www/html/wordpress
  • number_of_minions: 应启动多少个minions(一次可以运行多少个任务)。
  • path_to_temp_directory: 可能应该与path_to_wordpress相同。
  • user: 在其下运行进程的系统用户,可能是apache(CentOS)、nginx(CentOS)或www-data(Ubuntu)。
  • 您可以选择将“my_wp_minions_workers”文本更改为更描述性的内容,如果愿意的话。

更新supervisor配置后,重启服务(CentOS或Ubuntu)

systemctl restart supervisord
service supervisor restart

Systemd配置(CentOS 7.x)

根据需要填写<brackets>中的值,将以下内容添加到/etc/systemd/system/wp-minions-runner@.service

[Unit]
Description=WP-Minions Runner %i
After=network.target

[Service]
PIDFile=/var/run/wp-minions-runner.%i.pid
User=<user>
Type=simple
ExecStart=/usr/bin/env php <path_to_wordpress>/wp-minions-runner.php
Restart=always

[Install]
WantedBy=multi-user.target
  • path_to_wordpress: WordPress安装根目录的绝对路径,例如:/var/www/html/wordpress
  • user: 在其下运行进程的系统用户,可能是apache(CentOS)、nginx(CentOS)或www-data(Ubuntu)。

重新加载systemd

systemctl daemon-reload

启用并启动您希望运行的运行程序的数量

systemctl enable wp-minions-runner@{1..n}
systemctl start wp-minions-runner@{1..n}

其中 'n' 是您想要的过程数。

WordPress配置

在您的wp-config.php中定义WP_MINIONS_BACKEND常量。有效值是gearmanrabbitmq。如果为空,则默认为cron客户端。

define( 'WP_MINIONS_BACKEND', 'gearman' );

如果您的作业队列服务不在本地运行或使用非标准端口,您需要在wp-config.php中定义服务器

# Gearman config
global $gearman_servers;
$gearman_servers = array(
  '127.0.0.1:4730',
);
# RabbitMQ config
global $rabbitmq_server;
$rabbitmq_server = array(
  'host'     => '127.0.0.1',
  'port'     => 5672,
  'username' => 'guest',
  'password' => 'guest',
);

Note: On RabbitMQ the guest/guest account is the default administrator account, RabbitMQ will only allow connections connections on that account from localhost. Connections to any non-loopback address will be denied. See the RabbitMQ manual on [user management](https://rabbitmq.cn/rabbitmqctl.8.html#User_Management) and [Access Control](https://rabbitmq.cn/rabbitmqctl.8.html#Access_Control) for information on adding users and allowing them access to RabbitMQ resources.

## MySQL Persistent Queue (Recommended)

By default, gearman will store the job queue in memory. If for whatever reason the gearman service goes away, so does the queue. For persistence, you can optionally use a MySQL database for the job queue:

#### CentOS

Edit the gearman config at ```/etc/sysconfig/gearmand```, adding the following to the OPTIONS line (or creating the line, if it doesn't exist yet), inserting database credentials as necessary:
```sh
OPTIONS="-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue"

Ubuntu

编辑/etc/default/gearman-job-server中的gearman配置,在PARAMS行添加以下内容(如果尚不存在则创建该行),并根据需要插入数据库凭证

PARAMS="-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue"

注意:对于某些配置,上述方法可能不起作用,因为/etc/default/gearman-job-server不会被读取。如果您没有看到持久的队列设置,那么

  1. 在MySQL中创建一个gearman数据库(数据库必须在数据库中存在,但当gearmand第一次初始化时,它将创建表)。
  2. /etc/gearmand.conf中创建一个文件
  3. 在文件中粘贴配置,所有配置项都在一行上
-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue

然后重新启动gearman-job-server:sudo service gearman-job-server restart

验证

一切安装完成后,您可以使用位于system-tests/YOURQUEUE目录中的test-client.phptest-worker.php文件快速确保您的作业队列正在接受作业。工作器被配置为反转传递给它的任何文本。在客户端文件中,我们将“Hello World”传递给工作器。

在一个窗口中运行php test-worker.php - 您现在将有一个工作进程正在运行,等待作业。

在另一个窗口中运行php test-client.php "Hello World" - 您应该在屏幕上看到打印出“dlroW olleH”。

ctrl-c将在您完成测试后停止工作进程。

用法

一旦配置并激活,您将可以使用wp_async_task_add()。如果您熟悉wp_schedule_single_event(),那么wp_async_task_add()的工作方式应该非常熟悉。

此函数最多接受三个参数,第一个参数是必需的

  1. $hook:这是在作业运行时要执行的动作钩子名称。您的回调函数应该使用add_action( $hook, $callback )将其挂钩
  2. $args:这是可选的,默认为空数组。您可以将任意数据数组传递给此参数,并且它将被传递给您的回调函数。
  3. $priority:这是可选的,默认为“normal”。其他有效选项是“high”或“low”。高优先级作业将在正常优先级作业之前运行,即使正常优先级作业已经在队列中等待更长时间。

当工作器可用时在数据库中设置一个选项

// Add a task, that will call the "myplugin_update_option" action when it is run
wp_async_task_add( 'myplugin_update_option', array( 'mykey' => 'myvalue' ) );

function myplugin_update_option_callback( $args ) {
	// In reality, you are probably doing a lot of resource intensive work here
	update_option( 'my-option-name', $args['mykey'] );
}

// Add the action that links the task and the callback.
// Notice the hook below is the same as the hook provided to wp_async_task_add.
add_action( 'myplugin_update_option', 'myplugin_update_option_callback' );

一旦工作器空闲,并运行上述任务,您在选项表中就会有名为“my-option-name”的选项,其值为“myvalue”,因为“myvalue”是通过$args传递的

自定义

以下常量可以用来自定义WP Minions的行为。

  1. WP_MINIONS_JOBS_PER_WORKER - 每个工作器执行的作业数,默认为1。每个工作器运行多个作业将减少生成的工作器数量,并且可以显著提高性能。然而,过大的值如果存在内存泄漏问题会导致问题。请谨慎使用。

  2. WP_MINIONS_CLIENT_CLASS - 您也可以更改用于向作业队列发送作业的客户端类。它应该匹配\WpMinions\Client的接口。

  3. WP_MINIONS_WORKER_CLASS - 类似地,您也可以更改用于执行作业的工作器类。它应该匹配\WpMinions\Worker的接口。

问题

如果您发现任何错误或有改进插件的建议,请打开一个问题。我们非常期待看到社区对这一项目的看法,并希望得到您的反馈!

支持级别

存档:该项目不再由10up维护。我们不再响应问题或拉取请求,除非它们与安全相关。我们鼓励感兴趣的开发商分叉此项目并使其成为自己的项目!

喜欢您看到的吗?

68747470733a2f2f313075702e636f6d2f75706c6f6164732f323031362f31302f313075702d4769746875622d42616e6e65722e706e67