sebkay/wp-queued-jobs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebkay/wp-queued-jobs

Composer 安装命令:

composer require sebkay/wp-queued-jobs

包简介

A Laravel-like queue system for WordPress.

README 文档

README

Validate PHP

A Laravel-like queue system for WordPress.

Easily create background jobs for things like sending emails or importing large amounts of data. All with a fluent API.

I highly recommend using a plugin like WP Crontrol so you can easily see, and manually run, cron jobs from the WordPress dashboard.

Requires PHP 7.4+

Install

The recommended way to install this package is via Composer.

composer require sebkay/wp-queued-jobs

Usage

1. Create job

To create a job, you need to extend the WpQueuedJobs\Jobs\Job class:

use WpQueuedJobs\Jobs\Job;

class BackgroundJob extends Job
{
    public function handle()
    {
        // Handle the job
        // Use $this->data to access what was passed with the job when it was added to the queue
    }
}

2. Add job to queue and dispatch

wpj()
    ->addJob(BackgroundJob::class, 'Data for the background job.')
    ->dispatch();

Anything passed as the second parameter to addJob() will be available in the handle() method of the job (and the rest of the class) as $this->data.

The data can be anything you want. A string, array, integer, class etc...

The "queue worker" will look for new jobs every 1 minute and run them (if there are any).

The system runs on a "first-in first-out" basis. So whatever gets dispatched first will be run first.

Important

As the lowest cron time in WordPress defaults to 1 minute, jobs scheduled to run at 6am might not actually run until 6:01am. This isn't an issue in the majority of cases, but it's worth knowing.

The point of background jobs is that they run in the background, not immediately, so they should never be used for time-sensitive tasks.

Example #1 (Send Email)

  1. Load the "successful registration" page template.
  2. Add the "send welcome email" job and dispatch it to the queue.
add_action('template_redirect', function () {
    if (!is_page_template('register-success.php')) {
        return;
    }

    wpj()
        ->addJob(SendWelcomeEmailJob::class, wp_get_current_user())
        ->dispatch();
}, 10, 0);

Example #2 (Import Posts from API)

  1. Create a custom cron event.
  2. Add two jobs and dispatch them to the queue.
  3. Each job is given an offset and a max so the same posts aren't imported twice.
  4. Schedule the cron even to run once per day at 6am, starting tomorrow.
add_action('import_api_posts', function () {
    wpj()
        ->addJob(ImportApiPostsJob::class, ['offset' => 0, 'max' => 100])
        ->addJob(ImportApiPostsJob::class, ['offset' => 100, 'max' => 100])
        ->dispatch();
}, 10, 0);

if (!\wp_next_scheduled('import_api_posts')) {
    \wp_schedule_event(
        \strtotime("+ 1 days 6am"),
        'daily',
        'import_api_posts'
    );
}

Why

Unfortunatetly there's no concept of a "queue worker" in WordPress. At it's core WordPress is just a bunch of PHP files. There's no command line runner, such as Artisan in Laravel.

Although the WordPress CLI exists, a lot of people host their sites on shared hosting, so that isn't an option.

This package tries to circumvent that by utilising the WordPress Cron system. It runs a WP Cron task every minute to see if there are new jobs to run. If there are then it locks the connection to the database, runs the jobs, then unlocks the connection.

It's important to lock the queue to prevent the same jobs from running multiple times. The "queue worker" checks if there are new jobs every minute, so if any queue takes longer than a minute to finish jobs won't overlap.

sebkay/wp-queued-jobs 适用场景与选型建议

sebkay/wp-queued-jobs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 242 次下载、GitHub Stars 达 39, 最近一次更新时间为 2023 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sebkay/wp-queued-jobs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sebkay/wp-queued-jobs 我们能提供哪些服务?
定制开发 / 二次开发

基于 sebkay/wp-queued-jobs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 242
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-12