duckdev/wp-queue-process

WordPress 背景队列处理库。

v1.0.2 2021-08-02 07:33 UTC

This package is auto-updated.

Last update: 2024-09-29 06:20:21 UTC


README

WP Queue Process

WP Queue Process 可用于执行非阻塞的异步请求或作为后台处理工具,允许您排队任务。

需要 PHP 5.2+

安装

在您的项目中安装此库的推荐方法是使用 Composer 加载。

composer require duckdev/wp-queue-process

用法

异步请求

异步请求对于推送慢速的一次性任务很有用,例如发送电子邮件到后台进程。一旦请求被派发,它将立即在后台处理。

扩展 \DuckDev\Queue\Async

class WP_Example_Request extends \DuckDev\Queue\Async {

	/**
	 * @var string
	 */
	protected $action = 'example_request';

	/**
	 * Handle
	 *
	 * Override this method to perform any actions required
	 * during the async request.
	 */
	protected function handle() {
		// Actions to perform
	}
}
protected $action

应设置为唯一的名称。

protected function handle()

应包含在非阻塞请求期间执行的任何逻辑。传递给请求的数据将可通过 $_POST 访问。

派发请求

实例化您的请求

$this->example_request = new WP_Example_Request();

如有必要,向请求中添加数据

$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );

执行请求

$this->example_request->dispatch();

也支持链式调用

$this->example_request->data( array( 'data' => $data ) )->dispatch();

后台进程

后台进程与异步请求类似,但允许您排队任务。推送到队列中的项目将在队列被派发时在后台处理。队列还将根据可用的服务器资源进行扩展,因此高端服务器将每次批量处理更多项目。一旦一个批次完成,下一个批次将立即开始。

默认情况下,每5分钟运行一次健康检查,以确保在存在排队项目时队列正在运行。如果队列失败,它将被重新启动。

队列按先入先出的方式工作,这允许在队列正在处理时推入更多项目。

扩展 \DuckDev\Queue\Task

class WP_Example_Process extends \DuckDev\Queue\Task {

	/**
	 * @var string
	 */
	protected $action = 'example_process';

	/**
	 * Task
	 *
	 * Override this method to perform any actions required on each
	 * queue item. Return the modified item for further processing
	 * in the next pass through. Or, return false to remove the
	 * item from the queue.
	 *
	 * @param mixed  $item  Queue item to iterate over
	 * @param string $group Group name of the task (Useful when performing multiple tasks).
	 *                    
	 * @return mixed
	 */
	protected function task( $item, $group ) {
		// Actions to perform

		return false;
	}

	/**
	 * Complete
	 *
	 * Override if applicable, but ensure that the below actions are
	 * performed, or, call parent::complete().
	 */
	protected function complete() {
		parent::complete();

		// Show notice to user or perform some other arbitrary task...
	}
}
protected $action

应设置为唯一的名称。

protected function task( $item )

应包含在排队项目上执行的任何逻辑。返回 false 以从队列中删除项目,或返回 $item 以将其推回队列进行进一步处理。如果项目已被修改并推回队列,则在退出批次之前将保存当前状态。

protected function complete()

可选地包含在队列完成后执行的任何逻辑。

派发进程

实例化您的进程

$this->example_process = new WP_Example_Process();

注意:您必须无条件地实例化您的进程。所有请求都应该这样做,即使没有将项目推送到队列。

将项目推送到队列

foreach ( $items as $item ) {
    $this->example_process->push_to_queue( $item );
}

或直接设置队列项目

$this->example_process->set_queue( $items );

然后保存并派发队列

$this->example_process->save( 'my-task' ')->dispatch();

基本认证

如果您的网站在基本认证之后,异步请求和后台进程将无法完成。这是因为在 WP Background Processing 中,我们依赖于 WordPress HTTP API,它要求您将基本认证凭据附加到请求中。最简单的方法是使用以下过滤器

function ddqueue_http_request_args( $r, $url ) {
	$r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );

	return $r;
}
add_filter( 'http_request_args', 'ddqueue_http_request_args', 10, 2);

致谢

许可证

GPLv2+