public function actionAutoplannogrammaRecalculate(): int
{
- $cacheKey = 'apRecalculateTask';
- $cache = Yii::$app->cache;
- $task = $cache->get($cacheKey);
- if ($task) {
- $this->stdout("Задача время {$task['startTime']}\n");
- }
-
$scriptLauncherLog = ScriptLauncherLog::find()
->andWhere(['name' => "taskApRecalculate"])
->orderBy(['created_at' => SORT_DESC])
- ->asArray()
->limit(1)
->one();
- if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1 && $scriptLauncherLog['status'] == 1) {
- //$task = json_decode($scriptLauncherLog['context'], true );
- $cache->delete($cacheKey);
- $this->stdout("Задача уже запущена {$task['startTime']}\n");
+
+ if ($scriptLauncherLog && $scriptLauncherLog->active == 1 && $scriptLauncherLog->status == 1) {
+ $task = json_decode($scriptLauncherLog->context, true );
+ } else {
return ExitCode::OK;
}
//$this->stdout(json_encode($task, JSON_UNESCAPED_UNICODE));
- if (!is_array($task) || empty($task['taskName']) || $task['taskName'] !== 'apRecalculateTask') {
- $this->stdout("Нет задания apRecalculateTask\n");
+ if (!is_array($task) || empty($task['taskName']) || $task['taskName'] !== 'taskApRecalculate') {
+ $this->stdout("Нет задания taskApRecalculate\n");
return ExitCode::OK;
}
$this->stdout("Недопустимые парамеры: отстутствует {$key}\n");
$task['status'] = 'error';
$task['error'] = "Отстутствует {$key}";
- $cache->set($cacheKey, $task);
return ExitCode::UNSPECIFIED_ERROR;
}
}
if (isset($task['status']) && $task['status'] === 'pending') {
$task['status'] = 'running';
- $ok = $cache->set($cacheKey, $task);
-
-
- $log = new ScriptLauncherLog();
- $log->source = 'CronController';
- $log->category = 'autoplannogramma';
- $log->prefix = 'actionAutoplannogrammaRecalculate';
- $log->name = 'taskApRecalculate';
- $log->context = json_encode($task, JSON_UNESCAPED_UNICODE);
- $log->year = (int)$task['year'];
- $log->month = (int)$task['month'];
- $log->active = 1;
- $log->progress = 0;
- $log->status = 1;
- $log->date_start = date('Y-m-d H:i:s');
- if (!$log->save()) {
- Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
- LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
+ $scriptLauncherLog->context = json_encode($task, JSON_UNESCAPED_UNICODE);
+
+ if (!$scriptLauncherLog->save()) {
+ Yii::error(json_encode($scriptLauncherLog->getErrors(), JSON_UNESCAPED_UNICODE));
+ LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $scriptLauncherLog->getErrors()], JSON_UNESCAPED_UNICODE));
}
- $ok = $cache->delete($cacheKey);
+
$service = new AutoPlannogrammaService();
$year = (int)$task['year'];
$task['status'] = 'done';
$task['progress'] = 100;
- $cache->set($cacheKey, $task);
-
- $log->context = json_encode($task, JSON_UNESCAPED_UNICODE);
- $log->message = 'Finished successfully';
- $log->progress = 100;
- $log->active = 0;
- $log->date_finish = date('Y-m-d H:i:s');
- $log->status = 2;
- if (!$log->save()) {
- Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
- $this->stdout(json_encode($log->getErrors()));
- LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
+
+
+ $scriptLauncherLog->context = json_encode($task, JSON_UNESCAPED_UNICODE);
+ $scriptLauncherLog->message = 'Finished successfully';
+ $scriptLauncherLog->progress = 100;
+ $scriptLauncherLog->active = 0;
+ $scriptLauncherLog->date_finish = date('Y-m-d H:i:s');
+ $scriptLauncherLog->status = 2;
+ if (!$scriptLauncherLog->save()) {
+ Yii::error(json_encode($scriptLauncherLog->getErrors(), JSON_UNESCAPED_UNICODE));
+ $this->stdout(json_encode($scriptLauncherLog->getErrors()));
+ LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $scriptLauncherLog->getErrors()], JSON_UNESCAPED_UNICODE));
}
- $cache->delete($cacheKey);
+
$this->stdout("Расчет для магазина {$storeId} закончен\n");
return ExitCode::OK;
$task['status'] = 'error';
$task['error'] = $e->getMessage();
- $cache->set($cacheKey, $task);
-
- $log->message = 'Error: ' . $e->getMessage();
- $log->error_count = 1;
- $log->error_message = $e->getMessage();
- $log->active = 0;
- $log->date_finish = date('Y-m-d H:i:s');
- $log->status = 2;
- if (!$log->save()) {
- Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
- LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
+
+ $scriptLauncherLog->message = 'Error: ' . $e->getMessage();
+ $scriptLauncherLog->error_count = 1;
+ $scriptLauncherLog->error_message = $e->getMessage();
+ $scriptLauncherLog->active = 0;
+ $scriptLauncherLog->date_finish = date('Y-m-d H:i:s');
+ $scriptLauncherLog->status = 2;
+ if (!$scriptLauncherLog->save()) {
+ Yii::error(json_encode($scriptLauncherLog->getErrors(), JSON_UNESCAPED_UNICODE));
+ LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $scriptLauncherLog->getErrors()], JSON_UNESCAPED_UNICODE));
}
- $cache->delete($cacheKey);
$this->stderr("Ошибка в ходе расчета: {$e->getMessage()}\n");
return ExitCode::UNSPECIFIED_ERROR;
}
use yii_app\records\WriteOffs;
use yii_app\records\WriteOffsErp;
use yii_app\services\AutoPlannogrammaService;
+use yii_app\services\LogService;
use yii_app\services\StorePlanService;
class CategoryPlanController extends Controller {
if ( $categoryPlanNew->save()) {
$recalcFlag = Yii::$app->cache->get('needRecalc');
if ($recalcFlag) {
- $taskName = "apRecalculateTask";
+ $taskName = "taskApRecalculate";
$cacheValue = [
'taskName' => $taskName,
'progress' => 0,
'error' => null
];
- Yii::$app->cache->set($taskName, $cacheValue, 3600);
+ $log = new ScriptLauncherLog();
+ $log->source = 'CronController';
+ $log->category = 'autoplannogramma';
+ $log->prefix = 'actionAutoplannogrammaRecalculate';
+ $log->name = $taskName;
+ $log->context = json_encode($cacheValue, JSON_UNESCAPED_UNICODE);
+ $log->year = (int)$cacheValue['year'];
+ $log->month = (int)$cacheValue['month'];
+ $log->active = 1;
+ $log->progress = 0;
+ $log->status = 1;
+ $log->date_start = date('Y-m-d H:i:s');
+ if (!$log->save()) {
+ Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
+ LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
+ }
Yii::$app->cache->delete('needRecalc');
}
} else {
$year = (int)Yii::$app->request->get('year');
$month = (int)Yii::$app->request->get('month');
$store = (int)Yii::$app->request->get('store_id');
+ $taskName = "taskApRecalculate";
$scriptLauncherLog = ScriptLauncherLog::find()
- ->andWhere(['name' => "taskApRecalculate"])
+ ->andWhere(['name' => $taskName])
->orderBy(['created_at' => SORT_DESC])
->asArray()
->limit(1)
if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1 && $scriptLauncherLog['status'] == 1) {
return $this->asJson(['status' => 'running' ]);
} else {
- $taskName = "apRecalculateTask";
-
$cacheValue = [
'taskName' => $taskName,
'year' => $year,
'progress' => 0,
'error' => null
];
- Yii::$app->cache->set($taskName, $cacheValue, 3600);
-
- return $this->asJson(['status' => 'started', 'data' => Yii::$app->cache->get('apRecalculateTask') ]);
+ $log = new ScriptLauncherLog();
+ $log->source = 'CronController';
+ $log->category = 'autoplannogramma';
+ $log->prefix = 'actionAutoplannogrammaRecalculate';
+ $log->name = $taskName;
+ $log->context = json_encode($cacheValue, JSON_UNESCAPED_UNICODE);
+ $log->year = (int)$cacheValue['year'];
+ $log->month = (int)$cacheValue['month'];
+ $log->active = 1;
+ $log->progress = 0;
+ $log->status = 1;
+ $log->date_start = date('Y-m-d H:i:s');
+ if (!$log->save()) {
+ Yii::error(json_encode($log->getErrors(), JSON_UNESCAPED_UNICODE));
+ LogService::apiErrorLog(json_encode(["error_id" => 8, "error" => $log->getErrors()], JSON_UNESCAPED_UNICODE));
+ }
+ return $this->asJson(['status' => 'started', 'data' => $cacheValue ]);
}
}
public function actionCheckTask()
{
- $task = Yii::$app->cache->get('apRecalculateTask');
+ $scriptLauncherLog = ScriptLauncherLog::find()
+ ->andWhere(['name' => "taskApRecalculate"])
+ ->orderBy(['created_at' => SORT_DESC])
+ ->limit(1)
+ ->one();
+ $task = json_decode($scriptLauncherLog->context, true );
if (!$task) {
- $scriptLauncherLog = ScriptLauncherLog::find()
- ->andWhere(['name' => "taskApRecalculate"])
- ->orderBy(['created_at' => SORT_DESC])
- ->asArray()
- ->limit(1)
- ->one();
-
- if (!$scriptLauncherLog) {
- return $this->asJson(['status' => 'not_found']);
- }
-
- $dateFinish = $scriptLauncherLog['date_finish'];
- $ageSeconds = $dateFinish
- ? time() - strtotime($dateFinish)
- : null;
-
-
- if ($dateFinish === null || $ageSeconds <= 5) {
- $task = json_decode($scriptLauncherLog['context'], true);
- } else {
- return $this->asJson(['status' => 'not_found']);
- }
+ return $this->asJson(['status' => 'not_found']);
}
- Yii::$app->cache->delete('apRecalculateTask');
+
return $this->asJson([
'status' => $task['status'],
- 'progress' => $task['progress'] ?? 0,
- 'error' => $task['error'] ?? null,
- 'start' => $task['startTime'],
+ 'progress' => $task['progress'],
+ 'error' => $task['error'],
+ 'start' => $scriptLauncherLog->date_start,
]);
+
}
}