ajit3790/laravel-queue

Laravel作业状态队列管理器

dev-master 2017-11-12 00:12 UTC

This package is not auto-updated.

Last update: 2024-09-22 13:27:20 UTC


README

Laravel包,用于添加跟踪通过队列派发的Job进度、状态和结果的能力。

  • 队列名称、尝试次数、状态以及创建/更新/开始/完成的时间戳。
  • 进度更新,带有任意当前/最大值和自动计算的百分比
  • 处理带有异常信息的失败作业
  • 自定义输入/输出
  • 本机Eloquent模型 JobStatus
  • 支持Laravel中包含的所有驱动程序(null/sync/database/beanstalkd/redis/sqs)

要求

  • PHP >= 5.6.4
  • Laravel >= 5.3

安装

此插件只能从Composer安装。

运行以下命令

composer require ajit3790/laravel-queue

Laravel 5.5

只需运行迁移脚本。您无需做任何事情,此包通过使用新的自动发现功能自动加载服务提供者。

php artisan migrate

Laravel 5.4或更低版本

将以下内容添加到您的config/app.php

'providers' => [
    ...
    Platform\JobStatusMaster\JobStatusServiceProvider::class,
]

然后运行

php artisan migrate

使用方法

在您的Job中,使用JobQueueFactory特质,并在构造函数中调用$this->prepareStatus()

<?php
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Platform\JobStatusMaster\JobQueueFactory;

class ExampleJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels, JobQueueFactory;

    public function __construct(array $params)
    {
        $this->prepareStatus();
        $this->params = $params; // Optional
        $this->setInput($this->params); // Optional
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $max = mt_rand(5, 30);
        $this->setProgressMax($max);

        for ($i = 0; $i <= $max; $i += 1) {
            sleep(1); // Some Long Operations
            $this->setProgressNow($i);
        }

        $this->setOutput(['total' => $max, 'other' => 'parameter']);
    }
}

在您的作业调度器中,调用$job->getJobStatusId()以获取$jobStatusId

<?php
$job = new TrackableJob([]);
$this->dispatch($job);

$jobStatusId = $job->getJobStatusId();

$jobStatusId可以用于其他地方以检索作业状态、进度和输出。

<?php
$jobStatus = JobStatus::find($jobStatusId);

文档

<?php
// Job protected methods
$this->prepareStatus();                       // Must be called in constructor before any other methods
$this->setProgressMax(int $v);                // Update the max number of progress
$this->setProgressNow(int $v);                // Update the current number progress
$this->setProgressNow(int $v, int $every);    // Update the current number progress only if $v % $every == 0 (Reduce database write)
$this->setInput(array $v);                    // Store input into database
$this->setOutput(array $v);                   // Store output into database (Typically the run result)


// Job public methods
$job->getJobStatusId();                       // Return the primary key of JobStatus (To retrieve status later)

// JobStatus fields
var_dump($jobStatus->job_id);                 // String (Result varies with driver, see note)
var_dump($jobStatus->type);                   // String
var_dump($jobStatus->queue);                  // String
var_dump($jobStatus->status);                 // String [queued|executing|finished|failed]
var_dump($jobStatus->attempts);               // Integer
var_dump($jobStatus->progress_now);           // Integer
var_dump($jobStatus->progress_max);           // Integer
var_dump($jobStatus->input);                  // Array
var_dump($jobStatus->output);                 // Array, ['message' => $exception->getMessage()] if job failed
var_dump($jobStatus->created_at);             // Carbon object
var_dump($jobStatus->updated_at);             // Carbon object
var_dump($jobStatus->started_at);             // Carbon object
var_dump($jobStatus->finished_at);            // Carbon object

// JobStatus generated fields
var_dump($jobStatus->progress_percentage);    // Double [0-100], useful for displaying progress bar
var_dump($jobStatus->is_ended);               // Boolean, true if status == finished || status == failed
var_dump($jobStatus->is_executing);           // Boolean, true if status == executing
var_dump($jobStatus->is_failed);              // Boolean, true if status == failed
var_dump($jobStatus->is_finished);            // Boolean, true if status == finished

注意

$jobStatus->job_id的结果因驱动程序而异