kargnas/laravel-job-status
Composer 安装命令:
composer require kargnas/laravel-job-status
包简介
Laravel Job Status
README 文档
README
Laravel package to add ability to track Job progress, status and result dispatched to Queue.
- Queue name, attempts, status and created/updated/started/finished timestamp.
- Progress update, with arbitrary current/max value and percentage auto calculated
- Handles failed job with exception message
- Custom input/output
- Native Eloquent model
JobStatus - Support all drivers included in Laravel (null/sync/database/beanstalkd/redis/sqs)
Requirements
- PHP >= 5.6.4
- Laravel >= 5.3
Installation
This plugin can only be installed from Composer.
Run the following command:
composer require imtigger/laravel-job-status
Laravel 5.5
Just run the migration script. You don't have to do anything else, this package autoloads the Service Provider, using the new Auto-Discovery feature.
php artisan migrate
Laravel 5.4 or lower
Add the following to your config/app.php:
'providers' => [ ... Imtigger\LaravelJobStatus\LaravelJobStatusServiceProvider::class, ]
And run:
php artisan migrate
Usage
In your Job, use Trackable trait and call $this->prepareStatus() in constructor.
<?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']); } }
In your Job dispatcher, call $job->getJobStatusId() to get $jobStatusId:
<?php $job = new TrackableJob([]); $this->dispatch($job); $jobStatusId = $job->getJobStatusId();
$jobStatusId can be used elsewhere to retrieve job status, progress and output.
<?php $jobStatus = JobStatus::find($jobStatusId);
Documentations
<?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
Note
$jobStatus->job_id result varys with driver
| Driver | job_id |
|---|---|
| null | NULL (Job not run at all!) |
| sync | empty string |
| database | integer |
| beanstalkd | integer |
| redis | string(32) |
| sqs | GUID |
kargnas/laravel-job-status 适用场景与选型建议
kargnas/laravel-job-status 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「laravel」 「job」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kargnas/laravel-job-status 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kargnas/laravel-job-status 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kargnas/laravel-job-status 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP AMQP Binding Library
Provides a possibility to auto-setup crontabs required for project based on configuration file.
SlimQ Enables You to push jobs to background workers
A Laravel package to monitor queue jobs.
PHP client for Kue priority job queue API
A lightweight task queue on top of rybakit/phive-queue
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-16