springbot/magento2-queue

为 Magento 2 设计的通用作业排队包

安装量: 60,756

依赖关系: 1

建议者: 0

安全性: 0

星标: 17

关注者: 30

分支: 7

开放问题: 4

类型:magento2-module

1.2.2 2020-05-18 16:31 UTC

README

GPL licensed

Magento2 作业队列

此模块提供了为 Magento2 在队列中安排和运行作业的功能。基本思想是允许开发人员延迟执行会延迟页面加载时间和造成网站缓慢的任务。示例用例可能包括发送电子邮件、与远程 API 通信或在后台运行清理任务。

虽然最初是为我们的 Springbot Magento2 集成编写的,但此模块可用于许多其他用例。

该模块提供编程和命令行方式来入队、运行和查看作业。作业的关键组件是一个完全限定的类名、一个方法名和一个参数列表。有一些要求:类必须可自动加载,方法必须是公共的,且参数不得包含对象。其他选项包括优先级、队列名称和“运行时间”。

安装

Composer 是首选的安装方法

composer require springbot/magento2-queue
php bin/magento module:enable Springbot_Queue

命令行使用

入队作业

 php bin/magento springbot:queue:enqueue <class> <method> [<queue>] [<priority>] [<params>1] ... [<params>N]

列出队列中的当前作业

 php bin/magento springbot:queue:list

处理队列

php bin/magento springbot:queue:process

编程使用

<?php

namespace Your\Name\Space;

use Springbot\Queue\Model\Queue;

class QueueExample
{
    private $queue;

    /**
     * QueueExample constructor
     * @param Queue $queue
     */
    public function __construct(Queue $queue)
    {
        $this->queue = $queue;
    }

    /**
     * Queue examples
     */
    public function runQueueExamples()
    {
        // Enqueue a job 
        $this->queue->scheduleJob('\Fully\Qualified\ClassName', 'methodToRun', ['arg1', 'arg2']);
        
        // Process the queue 
        $this->queue->runNextJob();
        
        // Get a collection of current jobs
        $jobs = $this->queue->getCollection();
    }
}

API 使用

要处理队列,请向以下 API 端点发出 GET 请求

/V1/springbot/queue/process

要查看当前作业,请向以下 API 端点发出 GET 请求

/V1/springbot/queue/jobs

出于安全考虑,无法直接从 Web API 入队作业。