technopers/laravel-trackable-jobs

v2.0 2023-03-27 06:49 UTC

This package is auto-updated.

Last update: 2024-09-27 09:57:43 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Laravel trackable jobs 是一个简单而有效的库,用于通过模型条目(行)跟踪后台进程。

安装

您可以通过composer安装此包

composer require technopers/laravel-trackable-jobs

用法

您有一个名为压缩用户个人头像的进程。它与特定用户相关。因此,您的任务将是

class CompressUserProfilePicture {
    
}

此任务可以轻松地通过每个用户跟踪,您可以为此进程设置状态。您需要做的是。

只需添加名为 "Trackable" 的特性,并传递模型对象

class CompressUserProfilePicture 
{
    use Trackable {
        Trackable::__construct as __trackableConstruct;
    };
    
    public $user;
    
    public function __construct($user)
    {
        $this->user = $user;
        $this->__trackableConstruct($user);
    }
}

通过用户查找进程

通过用户获取进程

为此,只需添加名为 "HasTrackedJobs" 的特性

use HasTrackedJobs;

它将添加关系并提供您查找方法,例如

public function trackedJobs(): MorphMany
{
    return $this->morphMany(TrackedJob::class, 'trackable');
}

public function finishedJobs(): MorphMany
{
    return $this->morphMany(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_FINISHED);
}

public function failedJobs(): MorphMany
{
    return $this->morphMany(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_FAILED);
}

public function runningJobs(): MorphMany
{
    return $this->morphMany(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_STARTED);
}

public function pendingJobs(): MorphMany
{
    return $this->morphMany(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_QUEUED);
}

此外,如果您为每个模型有单个任务

为此,只需添加名为 "HasTrackedJob" 的特性,而不是 "HasTrackedJobs"

use HasTrackedJob;

它具有以下方法

public function trackedJob(): MorphOne
{
    return $this->morphOne(TrackedJob::class, 'trackable');
}

public function finishedJobs(): MorphOne
{
    return $this->morphOne(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_FINISHED);
}

public function failedJobs(): MorphOne
{
    return $this->morphOne(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_FAILED);
}

public function runningJobs(): MorphOne
{
    return $this->morphOne(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_STARTED);
}

public function pendingJobs(): MorphOne
{
    return $this->morphOne(TrackedJob::class, 'trackable')
        ->where('status', TrackedJobStatuses::STATUS_QUEUED);
}

用法

获取进程

$userProcesses = $user->trackedJob()->get();

$userProcesses = $user->trackedJob;

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件hardik@technopers.com联系,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。