slavkluev/yii2-job-progress

A Yii2 extension lets you know the progress of the job in a queue.

v1.0.0 2020-07-26 12:51 UTC

This package is auto-updated.

Last update: 2024-11-26 22:31:16 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

A Yii2 extension lets you know the progress of the job in a queue.

Install

Via Composer

$ composer require slavkluev/yii2-job-progress

You need to add progress behavior for queue component. Update config file:

'components' => [
    'queue' => [
        // ...
        'as progress' => \slavkluev\JobProgress\ProgressBehavior::class,
    ],
],

To add migrations to your application, edit the console config file to configure a namespaced migration:

'controllerMap' => [
    // ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => null,
        'migrationNamespaces' => [
            // ...
            'slavkluev\JobProgress\migrations',
        ],
    ],
],

Then apply new migrations:

$ yii migrate

Usage

class TestJob extends BaseObject implements \yii\queue\JobInterface
{
    public function execute($queue)
    {
        \Yii::$app->queue->setProgressMax(100);
        for ($i = 0; $i < 100; $i++) {
            \Yii::$app->queue->incrementProgress();
            sleep(1);
        }
    }
}

If the job is completed and removed, you can find out the progress by $jobId.

$jobProgress = JobProgress::findByJobId($jobId);
echo $jobProgress->getProgressMax() . PHP_EOL;
echo $jobProgress->getProgressNow() . PHP_EOL;
echo $jobProgress->getPercent() . PHP_EOL;

Documentations

// JobProgress methods
$jobProgress->getProgressMax();                     // Integer
$jobProgress->getProgressNow();                     // Integer
$jobProgress->getPercent();                         // Float

// Queue methods (Call from your Job)
\Yii::$app->queue->setProgressMax(int $value);      // Update the max number of progress.
\Yii::$app->queue->setProgressNow(int $value);      // Update the current number of progress.
\Yii::$app->queue->incrementProgress(int $offset);  // Increase current number of progress by $offset.
\Yii::$app->queue->getProgressMax();                // Integer
\Yii::$app->queue->getProgressNow();                // Integer
\Yii::$app->queue->getPercent();                    // Float

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email slavkluev@yandex.ru instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.