]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Переход на таблицу
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 2 Jul 2025 12:33:50 +0000 (15:33 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 2 Jul 2025 12:33:50 +0000 (15:33 +0300)
erp24/commands/CronController.php
erp24/controllers/CategoryPlanController.php

index f37c189c7d945288754604107cc627c7a915a08b..efb4f83ac8e1c14510f3ceddd040b53a369bd43d 100644 (file)
@@ -1742,30 +1742,22 @@ class CronController extends Controller
 
     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;
         }
 
@@ -1779,32 +1771,18 @@ class CronController extends Controller
                 $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'];
@@ -1922,20 +1900,20 @@ class CronController extends Controller
 
                 $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;
 
@@ -1943,19 +1921,17 @@ class CronController extends Controller
 
                 $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;
             }
index 65492e808d7d5c64772ffa9b8e37f14e7fa97131..a88d549c2921a84c4484f9372a8e121a3ddc5b2a 100644 (file)
@@ -23,6 +23,7 @@ use yii_app\records\StoreDynamic;
 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 {
@@ -254,7 +255,7 @@ class CategoryPlanController extends Controller {
                     if ( $categoryPlanNew->save()) {
                         $recalcFlag = Yii::$app->cache->get('needRecalc');
                         if ($recalcFlag) {
-                            $taskName = "apRecalculateTask";
+                            $taskName = "taskApRecalculate";
 
                             $cacheValue = [
                                 'taskName'  => $taskName,
@@ -266,7 +267,22 @@ class CategoryPlanController extends Controller {
                                 '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 {
@@ -590,8 +606,9 @@ class CategoryPlanController extends Controller {
         $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)
@@ -599,8 +616,6 @@ class CategoryPlanController extends Controller {
         if ($scriptLauncherLog && $scriptLauncherLog['active'] == 1 && $scriptLauncherLog['status'] == 1) {
             return $this->asJson(['status' => 'running' ]);
         } else {
-            $taskName = "apRecalculateTask";
-
             $cacheValue = [
                 'taskName'  => $taskName,
                 'year'      => $year,
@@ -611,47 +626,46 @@ class CategoryPlanController extends Controller {
                 '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,
         ]);
+
     }
 
 }