Files
kiri-core/HttpServer/Abstracts/Callback.php
T

136 lines
3.9 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2020-09-04 01:05:33 +08:00
namespace HttpServer\Abstracts;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-02 17:33:48 +08:00
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 01:27:08 +08:00
use Snowflake\Error\Logger;
use Snowflake\Event;
2020-09-02 19:09:32 +08:00
use Snowflake\Exception\ConfigException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2020-10-13 14:11:59 +08:00
use Swoole\Coroutine\Server;
2020-08-31 01:27:08 +08:00
use Swoole\Timer;
2021-02-20 15:23:43 +08:00
/**
* Class Callback
* @package HttpServer\Abstracts
*/
2021-02-20 17:33:28 +08:00
abstract class Callback extends HttpService
2020-08-31 01:27:08 +08:00
{
/**
* @param $server
* @param $worker_id
* @param $message
* @throws Exception
*/
protected function clear($server, $worker_id, $message)
{
2021-03-02 14:28:17 +08:00
try {
2021-03-02 14:49:20 +08:00
fire(Event::SYSTEM_RESOURCE_CLEAN);
2021-03-02 14:48:10 +08:00
2021-03-02 18:16:34 +08:00
\logger()->insert();
2021-03-02 14:28:17 +08:00
Snowflake::clearProcessId($server->worker_pid);
$logger = Snowflake::app()->getLogger();
$logger->write($this->_MESSAGE[$message] . $worker_id);
$logger->clear();
$this->eventNotify($message);
} catch (\Throwable $exception) {
$this->addError($exception);
}
2020-08-31 01:27:08 +08:00
}
const EVENT_ERROR = 'WORKER:ERROR';
const EVENT_STOP = 'WORKER:STOP';
const EVENT_EXIT = 'WORKER:EXIT';
2020-10-29 18:17:25 +08:00
private array $_MESSAGE = [
2020-08-31 01:27:08 +08:00
self::EVENT_ERROR => 'The server error. at No.',
self::EVENT_STOP => 'The server stop. at No.',
self::EVENT_EXIT => 'The server exit. at No.',
];
/**
* @param $message
2020-09-18 21:17:44 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-03-01 15:56:57 +08:00
private function eventNotify($message)
2020-08-31 01:27:08 +08:00
{
switch ($message) {
case self::EVENT_ERROR:
2021-03-01 15:56:57 +08:00
fire(Event::SERVER_WORKER_ERROR);
2020-08-31 01:27:08 +08:00
break;
case self::EVENT_EXIT:
2021-03-01 15:56:57 +08:00
fire(Event::SERVER_WORKER_EXIT);
2020-08-31 01:27:08 +08:00
break;
case self::EVENT_STOP:
2021-03-01 15:56:57 +08:00
fire(Event::SERVER_WORKER_STOP);
2020-08-31 01:27:08 +08:00
break;
}
}
2020-09-02 17:33:48 +08:00
/**
2020-09-02 19:09:32 +08:00
* @return PHPMailer
* @throws \PHPMailer\PHPMailer\Exception
* @throws ConfigException
*/
2020-12-17 14:09:14 +08:00
private function createEmail(): PHPMailer
2020-09-02 19:09:32 +08:00
{
$mail = new PHPMailer(true);
2020-09-03 16:51:12 +08:00
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = Config::get('email.host'); // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Debugoutput = false; // Enable SMTP authentication
$mail->CharSet = "UTF8"; // Enable SMTP authentication
$mail->Username = Config::get('email.username'); // SMTP username
$mail->Password = Config::get('email.password'); // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = Config::get('email.port'); // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
2020-09-02 19:09:32 +08:00
$mail->setFrom(Config::get('email.send.address'), Config::get('email.send.nickname'));
return $mail;
}
/**
2020-09-02 17:33:48 +08:00
* @param $message
* @throws
*/
2020-09-02 19:09:32 +08:00
protected function system_mail($message)
2020-09-02 17:33:48 +08:00
{
try {
2020-09-09 19:44:40 +08:00
if (!Config::get('email.enable', false, false)) {
return;
}
2020-09-02 19:09:32 +08:00
$mail = $this->createEmail();
2020-09-03 01:44:16 +08:00
$receives = Config::get('email.receive');
2020-09-02 19:09:32 +08:00
if (empty($receives) || !is_array($receives)) {
throw new Exception('接收人信息错误');
}
foreach ($receives as $receive) {
$mail->addAddress($receive['address'], $receive['nickname']); // Add a recipient
}
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'service error';
2020-09-02 17:33:48 +08:00
$mail->Body = $message;
$mail->AltBody = $message;
$mail->send();
2020-11-06 16:47:17 +08:00
} catch (\Throwable $e) {
2021-02-20 15:45:48 +08:00
$this->addError($e, 'email');
2020-09-02 17:33:48 +08:00
}
}
2020-08-31 01:27:08 +08:00
}