2019-12-24 13:54:32 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @link https://www.kuvalda.ru/
|
|
|
|
* @copyright
|
|
|
|
* @license
|
|
|
|
*/
|
|
|
|
|
2019-12-24 17:04:30 +03:00
|
|
|
namespace dominion\cron\console;
|
2019-12-24 13:54:32 +03:00
|
|
|
|
2019-12-24 17:04:30 +03:00
|
|
|
use Yii;
|
2019-12-24 13:54:32 +03:00
|
|
|
use yii\console\Controller;
|
|
|
|
use yii\console\ExitCode;
|
|
|
|
use dominion\cron\models\MoleTask;
|
2019-12-24 17:04:30 +03:00
|
|
|
use yii\base\InlineAction;
|
2019-12-24 13:54:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Запуск агента по расписанию
|
|
|
|
*
|
|
|
|
* @author Rybkin Sasha <ribkin@dominion.ru>
|
|
|
|
* @since 0.1
|
|
|
|
*/
|
|
|
|
class MoleController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Запуск всех агентов для проекта yii из mole_task
|
|
|
|
* @return int Exit code
|
|
|
|
*/
|
|
|
|
public function actionIndex()
|
|
|
|
{
|
2019-12-24 17:04:30 +03:00
|
|
|
$filePatch = Yii::getAlias('@app/runtime/lock.lock');
|
2019-12-24 13:54:32 +03:00
|
|
|
if(!file_exists($filePatch))
|
|
|
|
{
|
|
|
|
$fp = fopen($filePatch, "w");
|
|
|
|
fwrite($fp, "");
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
$file = fopen($filePatch, 'r+');
|
|
|
|
|
|
|
|
if (flock($file, LOCK_EX | LOCK_NB))
|
|
|
|
{
|
|
|
|
$model = new MoleTask;
|
|
|
|
$tasks = $model->getAllTask();
|
|
|
|
foreach ($tasks as $task)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$task->dateStart = date('Y-m-d H:i:s');
|
|
|
|
$task->save();
|
|
|
|
$class = 'app\commands\\' . $task->controller;
|
|
|
|
if (class_exists($class))
|
|
|
|
{
|
|
|
|
$control = new $class($task->controller, 'product');
|
|
|
|
$control->actionIndex(unserialize($task->params));
|
|
|
|
}
|
|
|
|
$task->dateEnd = date('Y-m-d H:i:s');
|
|
|
|
$task->completed = 1;
|
|
|
|
$task->save();
|
|
|
|
} catch (\yii\elasticsearch\Exception $ex)
|
|
|
|
{
|
|
|
|
$task->dateStart = null;
|
|
|
|
$task->save();
|
|
|
|
print_r($ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ExitCode::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|