Ver código fonte

基本ジョブの追加

develop
sosuke.iwabuchi 1 ano atrás
pai
commit
a257e94d3d
2 arquivos alterados com 165 adições e 0 exclusões
  1. +105
    -0
      app/Console/Commands/BaseCommand.php
  2. +60
    -0
      app/Console/Commands/HeartBeat.php

+ 105
- 0
app/Console/Commands/BaseCommand.php Ver arquivo

@@ -0,0 +1,105 @@
<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

abstract class BaseCommand extends Command
{

const RESULTCODE_SUCCESS = 0;
const RESULTCODE_WARN = 1;
const RESULTCODE_FAILED = 2;


/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}


abstract protected function service(): int;
protected bool $outputInfoForBase = true;

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$ret = 0;

$this->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 "";
}
}

+ 60
- 0
app/Console/Commands/HeartBeat.php Ver arquivo

@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands;


class HeartBeat extends BaseCommand
{

const COMMAND = "heartbeat {--maintenance}";

protected bool $outputInfoForBase = false;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = self::COMMAND;

/**
* The console command description.
*
* @var string
*/
protected $description = 'ハートビート';

static public function getCommand()
{
return self::COMMAND;
}


/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function service(): int
{
if ($this->option('maintenance')) {
$isMaintenanceMode = app()->isDownForMaintenance();
if ($isMaintenanceMode) {
$this->outputWarn("down for maintenance");
}
} else {
$this->outputInfo("heart beat");
}
return self::RESULTCODE_SUCCESS;
}
}

Carregando…
Cancelar
Salvar