2021-03-02 19:16:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Snowflake\Error;
|
|
|
|
|
|
|
|
|
|
|
2021-03-02 19:22:29 +08:00
|
|
|
use Exception;
|
2021-04-26 17:37:53 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-03-02 19:16:20 +08:00
|
|
|
use Snowflake\Core\Json;
|
|
|
|
|
use Snowflake\Exception\ComponentException;
|
|
|
|
|
use Snowflake\Process\Process;
|
|
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class LoggerProcess
|
|
|
|
|
* @package Snowflake\Error
|
|
|
|
|
*/
|
|
|
|
|
class LoggerProcess extends Process
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-04-27 10:34:50 +08:00
|
|
|
/**
|
|
|
|
|
* @param \Swoole\Process $process
|
|
|
|
|
*/
|
2021-04-26 17:34:33 +08:00
|
|
|
public function before(\Swoole\Process $process): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement before() method.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-26 17:37:53 +08:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function getProcessName(): string
|
2021-04-26 17:34:33 +08:00
|
|
|
{
|
|
|
|
|
// TODO: Implement getProcessName() method.
|
2021-04-26 17:37:53 +08:00
|
|
|
return get_called_class();
|
2021-04-26 17:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-02 19:16:20 +08:00
|
|
|
/**
|
|
|
|
|
* @param \Swoole\Process $process
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
*/
|
|
|
|
|
public function onHandler(\Swoole\Process $process): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement onHandler() method.
|
|
|
|
|
$this->message($process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Swoole\Process $process
|
|
|
|
|
* @throws ComponentException
|
2021-03-02 19:22:29 +08:00
|
|
|
* @throws Exception
|
2021-03-02 19:16:20 +08:00
|
|
|
*/
|
|
|
|
|
public function message(\Swoole\Process $process)
|
|
|
|
|
{
|
|
|
|
|
$message = Json::decode($process->read());
|
|
|
|
|
if (!empty($message)) {
|
2021-03-02 19:22:29 +08:00
|
|
|
Snowflake::writeFile($this->getDirName($message), $message[0], FILE_APPEND);
|
|
|
|
|
|
|
|
|
|
$this->checkLogFile($message[1]);
|
2021-03-02 19:16:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Coroutine\System::sleep(1);
|
|
|
|
|
|
|
|
|
|
$this->message($process);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 19:22:29 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function getDirName($message): string
|
|
|
|
|
{
|
|
|
|
|
return storage('server-' . date('Y-m-d') . '.log', $message[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $dirName
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function checkLogFile($dirName)
|
|
|
|
|
{
|
2021-03-02 19:25:53 +08:00
|
|
|
$files = new \DirectoryIterator(storage(null, $dirName));
|
2021-03-02 19:22:29 +08:00
|
|
|
if ($files->getSize() < 15) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Coroutine\System::exec('find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;');
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 19:16:20 +08:00
|
|
|
}
|