luyadev/luya-module-admin

View on GitHub
src/jobs/ScheduleJob.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace luya\admin\jobs;

use luya\admin\models\Scheduler;
use yii\base\BaseObject;
use yii\base\InvalidCallException;
use yii\queue\JobInterface;

/**
 * The Scheduler Job
 *
 * @author Basil Suter <basil@nadar.io>
 * @since 2.0.0
 */
class ScheduleJob extends BaseObject implements JobInterface
{
    /**
     * @var integer The integer value of the scheduler table job id.
     */
    public $schedulerId;

    /**
     * {@inheritDoc}
     */
    public function execute($queue)
    {
        $model = Scheduler::findOne($this->schedulerId);

        // ensure delete schedule jobs wont trigger an error.
        if ($model) {
            return $model->triggerJob();
        }

        // throwing an exception will turn is_error on queue log to 1
        throw new InvalidCallException("Unable to find the given scheduler job with id '{$this->schedulerId}'.");
    }
}