나는 Laravel 5.3 명령에 대한 다음 (단순) 잠금 코드가 있습니다정리 콘솔 명령
private $hash = null;
public final function handle() {
try {
$this->hash = md5(serialize([ static::class, $this->arguments(), $this->options() ]));
$this->info("Generated signature ".$this->hash,"v");
if (Redis::exists($this->hash)) {
$this->hash = null;
throw new \Exception("Method ".$this->signature." is already running");
}
Redis::set($this->hash, true);
$this->info("Running method","vv");
$this->runMutuallyExclusiveCommand(); //Actual command is not important here
$this->cleanup();
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
public function cleanup() {
if (is_string($this->hash)) {
Redis::del($this->hash);
}
}
명령이 일반적으로 그것의 실행주기를 통과 할 수있는 경우이 잘 작동 (처리 할 때 포함을 PHP 예외가 있습니다.) 그러나 명령이 다른 수단 (예 : CTRL-C 또는 터미널 창이 닫힌 경우)을 통해 중단되면 문제가 발생합니다. 이 경우 정리 코드가 실행되지 않고 명령이 여전히 "실행 중"으로 간주되어 캐시를 다시 시작하려면 캐시에서 항목을 수동으로 제거해야합니다. __destruct
함수에서 cleanup
코드를 실행하려고했지만 그 중 하나가 호출되지 않는 것 같습니다.
제 질문은 어떻게 종료되었는지에 관계없이 명령이 종료 될 때 실행할 코드를 설정하는 방법입니까?
작업이 종료되었거나 작업이 여전히 실행 중이기 때문에 캐시 항목이 있는지 여부를 알 수 없기 때문에 cron이 적절하다고 생각하지 않습니다. . 그러나 나는 그 대답이 정말로 '아니오'라는 것을 기꺼이 인정할 것이다. – apokryfos