kargnas / laravel-job-status
Laravel Job Status
v0.1.10
2017-09-13 08:14 UTC
Requires
- php: >=5.6.4
- laravel/framework: >=5.3
This package is not auto-updated.
Last update: 2024-09-15 03:55:11 UTC
README
Laravel 包,用于添加跟踪 Job 进度、状态和结果的功能,这些结果被分发到 Queue。
- 队列名称、尝试次数、状态以及创建/更新/开始/完成的时间戳。
- 进度更新,带有任意当前/最大值和自动计算出的百分比
- 处理带有异常信息的失败作业
- 自定义输入/输出
- 原生 Eloquent 模型
JobStatus - 支持 Laravel 包含的所有驱动程序(null/sync/database/beanstalkd/redis/sqs)
要求
- PHP >= 5.6.4
- Laravel >= 5.3
安装
此插件只能通过 Composer 安装。
运行以下命令
composer require imtigger/laravel-job-status
Laravel 5.5
只需运行迁移脚本。您无需执行其他操作,此包自动加载 Service Provider,使用新的自动发现功能。
php artisan migrate
Laravel 5.4 或更低版本
将以下内容添加到您的 config/app.php
'providers' => [ ... Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class, ]
然后运行
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->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 的结果会因驱动程序而异