freeman3s/laravel-job-status

Laravel Job Status

v0.2.1 2018-12-17 17:28 UTC

This package is auto-updated.

Last update: 2024-09-18 23:21:14 UTC


README

Latest Stable Version Total Downloads License

Laravel 包,用于添加跟踪 Job 进度、状态和结果的功能,这些结果被派遣到 Queue

  • 队列名称、尝试次数、状态以及创建/更新/开始/结束的时间戳。

  • 进度更新,包含任意当前/最大值和自动计算的百分比

  • 处理失败的任务,并带有异常消息

  • 自定义输入/输出

  • 本地 Eloquent 模型 JobStatus

  • 支持 Laravel 中包含的所有驱动程序(null/sync/database/beanstalkd/redis/sqs)

  • 此包故意不提供任何用于显示 Job 进度的 UI。

    如果您有此类需求,请参阅 https://github.com/imTigger/laravel-job-status-progress-view

    或使用 JobStatus 模型自行实现

要求

  • PHP >= 5.6.4
  • Laravel >= 5.3

安装

此插件只能通过 Composer 安装。

运行以下命令

composer require imtigger/laravel-job-status

1. 添加服务提供者(Laravel < 5.5)

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

'providers' => [
    ...
    Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class,
]

2. 发布迁移和配置

php artisan vendor:publish --provider="Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider"

3. 迁移数据库

php artisan migrate

使用方法

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

<?php
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Imtigger\LaravelJobStatus\Trackable;

class TrackableJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels, Trackable;

    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 分发器中,调用 $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, write to database only when $v % $every == 0
$this->incrementProgress(int $offset)             // Increase current number progress by $offset
$this->incrementProgress(int $offset, int $every) // Increase current number progress by $offset, write to database only when $v % $every == 0
$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 的结果因驱动程序而异