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; + } +}