bonditka/yii2-task

任务系统。

安装次数: 10

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

v0.1 2018-04-10 16:39 UTC

This package is auto-updated.

Last update: 2024-09-20 22:01:22 UTC


README

添加创建和执行任意任务的功能。作为任务,应指定实现接口 \bonditka\task\components\TaskInstance 的类。

在此扩展中,任务是多步骤操作。任务步骤在 TaskInstance 的实现中通过 arStep 属性描述。

任务可以通过 module 属性被各种模块标记,这允许灵活地根据需要配置任务系统。

安装

推荐通过 composer 安装。

要安装,请执行以下命令

php composer.phar require --prefer-dist bonditka/yii2-task "*"

或者将以下内容

"bonditka/yii2-task": "*"

添加到你的 composer.json 文件的 require 块中。

之后,需要应用迁移

php yii migrate

使用

以下是一个使用 crontab 运行未执行任务的示例

//crontask
$tasks = Task::getTaskToRun();
foreach($tasks as $task){
	do {
		$arResult = $task->run();

		if (!is_array($arResult) && helpers\ArrayHelper::keyExists('errors', $arResult)) {
			if(is_array($arResult['errors'])){
				$error = implode('. ', $arResult['errors']);
			}
			else{
				$error = $arResult['errors'];
			}
			throw new yii\base\ErrorException('Задача выполнилась с ошибками: '.$error);
		}

		if(!helpers\ArrayHelper::keyExists('progress', $arResult) || !helpers\ArrayHelper::keyExists('nextStep', $arResult)){
			throw new yii\base\ErrorException('Задача не вернула сигнала о завершении или переходе на следующий шаг');
		}

		$step = helpers\ArrayHelper::getValue($arResult, 'nextStep');
		$task->setStep($step)->taskSave();

	} while (!empty($arResult['nextStep']));
	$task->complete()->taskSave();
}
//TaskInstance
use yii\base as yiiBase;
use yii\helpers;

class TaskInstance extends yiiBase\Model implements \bonditka\task\components\TaskInstance
{
    public $step;

    public function rules()
    {
        return [
            [['step'], 'required'],
            [['step'], 'string'],
        ];
    }

    /** @var array of task step */
    public $arStep = [
        'first' => [
			'methodName' => 'myFirstAwesomeMethod',
			'message' => 'myAwesomeMethod is done. Next step is second',
		],
        'second' => [
			'methodName' => 'mySecondAwesomeMethod',
			'message' => 'myAwesomeMethod is done. Next step is third',
		],
        'third' => [
			'methodName' => 'myThirdAwesomeMethod',
			'message' => 'myAwesomeMethod is done. Task is done.',
		]
    ];


    /** @param array $arStep contains the steps of the task.
     *  Every step must contain
     *      'methodName' — the name of the method of the current instance of the class that is currently running,
     *      optional* 'message' — description of actions of the current step for the user
     */

    public function setSteps(array $arStep)
    {
        $this->arStep = $arStep;
    }

    public function getSteps(){
        return $this->arStep;
    }


    /**
     * @return integer between 0 and 100 of progress by current task
     */
    public function getProgress()
    {
        $stepPosition = array_search($this->step, array_keys($this->arStep));
        return ceil((($stepPosition + 1) / count($this->arStep)) * 100);
    }

    protected function getNextStep()
    {
        $keys = array_keys($this->arStep);
        return isset($keys[array_search($this->step, $keys) + 1]) ? $keys[array_search($this->step, $keys) + 1] : null;
    }

    /**
     * @param $param
     */
    protected function myFirstAwesomeMethod($param)
    {
        //do something...
        return true;
    }

    /**
     * @param $param
     */
    protected function mySecondAwesomeMethod($param)
    {
        //do something...
        return true;
    }

    /**
     * @param $param
     */
    protected function myThirdAwesomeMethod($param)
    {
        //do something...
        return true;
    }

    /**
     * @param $param
     * @return array $arResult
     * @throws yiiBase\ErrorException
     * @throws yiiBase\InvalidConfigException
	 * Основной метод пошагового выполнения задания
     */
    public function executeStep($param)
    {
        if (empty($this->arStep)) {
            throw new yiiBase\InvalidConfigException;
        }

        if (empty($this->step)) {
            reset($this->arStep);
            $this->step = key($this->arStep);
        }

        $methodResult = $this->{$this->arStep[$this->step]['methodName']}($param);

        if($methodResult === false){
            throw new yiiBase\ErrorException;
        }

        return [
            'progress' => $this->getProgress(),
            'nextStep' => $this->getNextStep(),
            'result' => $methodResult
        ];
    }
}

测试

要启动测试,需要在 .env 文件中指定测试数据库的连接。示例文件

# ---------
YII_DEBUG   = true
YII_ENV     = dev

# Databases
# ---------
TEST_DB_DSN           = mysql:host=localhost;port=3306;dbname=testdb
TEST_DB_USERNAME      = testdbu
TEST_DB_PASSWORD      = testdbpsw
TEST_DB_TABLE_PREFIX  =

然后,需要应用迁移

php tests/bin/yii migrate

之后,可以运行测试

vendor/bin/codecept build
vendor/bin/codecept run