From a257e94d3d02cdd44388e76275dc60e5ea1f1942 Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Sat, 23 Mar 2024 09:11:08 +0900 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E3=82=B8=E3=83=A7=E3=83=96?= =?UTF-8?q?=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/BaseCommand.php | 105 +++++++++++++++++++++++++++ app/Console/Commands/HeartBeat.php | 60 +++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 app/Console/Commands/BaseCommand.php create mode 100644 app/Console/Commands/HeartBeat.php diff --git a/app/Console/Commands/BaseCommand.php b/app/Console/Commands/BaseCommand.php new file mode 100644 index 0000000..5b571fe --- /dev/null +++ b/app/Console/Commands/BaseCommand.php @@ -0,0 +1,105 @@ +boot(); + try { + $ret = $this->service(); + } catch (Exception $e) { + $message = sprintf("例外発生:%s:%s:%d", $e->getMessage(), $e->getFile(), $e->getLine()); + $this->outputError($message, $e->getTrace()); + $ret = self::RESULTCODE_FAILED; + } + + + if ($ret === self::RESULTCODE_SUCCESS) { + $this->outputInfoForBase("成功しました。"); + } else if ($ret === self::RESULTCODE_WARN) { + $this->outputWarn("一部失敗があります。"); + } else if ($ret === self::RESULTCODE_FAILED) { + $this->outputError("失敗しました"); + } else { + $this->outputError(sprintf("未定義のエラーコード:%d", $ret)); + } + + return $ret; + } + + private function boot() + { + Log::setDefaultDriver("batch"); + Log::withContext([ + '__scheduleId' => strval(Str::uuid()), + ...$this->arguments(), + ]); + $this->outputInfoForBase(sprintf("バッチ起動 %s", $this->getCommandName())); + } + + protected function outputInfo(string $message, array $context = []) + { + Log::info($message, $this->getContext($context)); + $this->info($message); + } + private function outputInfoForBase(string $message, array $context = []) + { + if ($this->outputInfoForBase) { + Log::info($message, $this->getContext($context)); + } + $this->info($message); + } + protected function outputWarn(string $message, array $context = []) + { + Log::warning($message, $this->getContext($context)); + $this->warn($message); + } + protected function outputError(string $message, array $context = []) + { + Log::error($message, $this->getContext($context)); + $this->error($message); + } + private function getContext(array $context = []) + { + return array_merge($context, ["context" => $this->arguments()]); + } + + protected function getCommandName(): string + { + return ""; + } +} diff --git a/app/Console/Commands/HeartBeat.php b/app/Console/Commands/HeartBeat.php new file mode 100644 index 0000000..b706414 --- /dev/null +++ b/app/Console/Commands/HeartBeat.php @@ -0,0 +1,60 @@ +option('maintenance')) { + $isMaintenanceMode = app()->isDownForMaintenance(); + if ($isMaintenanceMode) { + $this->outputWarn("down for maintenance"); + } + } else { + $this->outputInfo("heart beat"); + } + return self::RESULTCODE_SUCCESS; + } +}