71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @link https://www.kuvalda.ru/
|
|
* @copyright
|
|
* @license
|
|
*/
|
|
|
|
namespace dominion\cron\console;
|
|
|
|
use Yii;
|
|
use yii\console\Controller;
|
|
use yii\console\ExitCode;
|
|
use dominion\cron\models\MoleTask;
|
|
use yii\base\InlineAction;
|
|
|
|
/**
|
|
* Запуск агента по расписанию
|
|
*
|
|
* @author Rybkin Sasha <ribkin@dominion.ru>
|
|
* @since 0.1
|
|
*/
|
|
class MoleController extends Controller
|
|
{
|
|
/**
|
|
* Запуск всех агентов для проекта yii из mole_task
|
|
* @return int Exit code
|
|
*/
|
|
public function actionIndex()
|
|
{
|
|
$filePatch = Yii::getAlias('@app/runtime/lock.lock');
|
|
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;
|
|
}
|
|
|
|
}
|