Skip to content

Commit

Permalink
完成异常日志捕获记录,优化项目目录结构,优化应用执行流程提高效率
Browse files Browse the repository at this point in the history
  • Loading branch information
anoxia committed Mar 4, 2019
1 parent 66e505e commit b39287b
Show file tree
Hide file tree
Showing 43 changed files with 1,816 additions and 741 deletions.
1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/skeleton.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

762 changes: 345 additions & 417 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/Cli/Worker.php → app/Cli/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use Bee\Di\Container;

/**
* Worker
* Job
*
* @package Star\Cli
*/
class Worker extends Command
class Job extends Command
{
/**
* 命令名称
Expand Down
14 changes: 13 additions & 1 deletion app/Http/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
* @package Star\Http
*/
class Demo extends Http
{}
{
public function say()
{
throw new \Exception('hello word');

return 'hello word';
}

public function upload()
{
return 'upload';
}
}
20 changes: 20 additions & 0 deletions app/Job/Demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Star\Job;

use Swoole\Process;
use Bee\Process\Worker;

/**
* Demo Job
* @package Star\Job
*/
class Demo extends Worker
{
/**
* @param Process $process
* @param int $ppid
*/
public function handle(Process $process, $ppid)
{
}
}
4 changes: 2 additions & 2 deletions app/Worker/Master.php → app/Job/Master.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Star\Worker;
namespace Star\Job;

use Bee\Di\Container as Di;

/**
* 多进程管理器
*
* @package Star\Worker
* @package Star\Job
*/
class Master extends \Bee\Process\Master
{
Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Star\Middleware;

use Bee\Http\Context;
use Star\Util\Application;
use Star\Util\Middleware;
use Bee\Http\Application;
use Bee\Http\Middleware;

/**
* 身份鉴权中间件
Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/CORS.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Star\Middleware;

use Bee\Http\Context;
use Star\Util\Application;
use Star\Util\Middleware;
use Bee\Http\Application;
use Bee\Http\Middleware;

/**
* 跨域处理中间件
Expand Down
30 changes: 8 additions & 22 deletions app/Middleware/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
namespace Star\Middleware;

use Bee\Exception;
use Star\Util\Middleware;
use Star\Util\Application;
use Bee\Http\Application;
use Bee\Http\Middleware;
use Bee\Http\Context;
use Star\Util\ThrowExceptionHandler;

/**
* 路由请求派发中间件
Expand All @@ -29,33 +30,18 @@ public function call(Application $application, Context $context, $parameters = n
try {
// 执行业务
$returnValue = $routeHandler->callMethod($application, $parameters);

// 返回数据为数组,拼接成约定格式
if (is_array($returnValue)) {
$returnValue = [
'result' => true,
'info' => $returnValue,
];
// 检查是否需要 json 序列化
if ($context->isOutputJson()) {
$returnValue = json_encode($returnValue);
}
} catch (Exception $e) {
// 拼接一场时返回的数据
$returnValue = [
'result' => false,
'code' => $e->getCode(),
'msg' => $e->getMessage(),
];
// 收集异常信息并记录日志
$returnValue = ThrowExceptionHandler::http($e, $context);

// 收集运行异常信息
$context->setLog($e->toArray());
// 终止请求往下传递
$continue = false;
}

// 检查是否需要 json 序列化
if ($context->isOutputJson()) {
$returnValue = json_encode($returnValue);
}

// 将返回值注入上下文
$context->setContent($returnValue);

Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use Bee\Di\Container;
use Bee\Http\Context;
use Bee\Http\Application;
use Bee\Http\Middleware;
use Bee\Router\Handler as RouteHandler;
use Star\Util\Middleware;
use Star\Util\Application;
use Star\Util\ThrowException;

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Model/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class Demo extends Model
/**
* @var string
*/
protected $table = 'api_error_log';
protected $table = 'table_name';
}
1 change: 1 addition & 0 deletions app/Util/Http.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Star\Util;

use Bee\Http\Application;
use Bee\Http\Context;
use Bee\Http\Request;
use Bee\Http\Response;
Expand Down
28 changes: 4 additions & 24 deletions app/Util/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Star\Util;

use Bee\Di\Container as Di;
use Bee\Error\Notice;
use Bee\Http\Application;
use Bee\Http\Server;
use Bee\Router\Router;
use Swoole\Http\Request;
Expand Down Expand Up @@ -40,23 +42,11 @@ public function onWorkerStart(SwooleHttpServer $server, $workerId)
swoole_set_process_name($this->name . ':worker');
}

// 注册错误处理方法
register_shutdown_function(function () {
// PR('register_shutdown_function');
});

$di = Di::getDefault();

// 错误处理方法
set_error_handler(function ($code, $message, $file, $line, $callStack) use ($di) {
// PR('set_error_handler');
PR(func_get_args());
// PR(debug_backtrace());

$notice = new Notice();

$di->getShared('service.logger')->error($message);

ThrowExceptionHandler::error(new Notice($message, $code, $line, $file));
}, E_ALL);

$router = new Router();
Expand All @@ -76,8 +66,6 @@ public function onWorkerStart(SwooleHttpServer $server, $workerId)
*/
public function onWorkerStop(SwooleHttpServer $server, $workerId)
{
// PR('onWorkerStop');
// PR($server);
}

/**
Expand All @@ -96,7 +84,7 @@ public function onRequest(Request $request, Response $response)
->handle()
;
} catch (\Throwable $e) {
$response->end($e->getMessage());
$response->end(ThrowExceptionHandler::uncaught($e));
}
}

Expand Down Expand Up @@ -127,13 +115,9 @@ public function onTask(SwooleHttpServer $server, SwooleTask $task)
* @param integer $workerPid
* @param integer $exitCode
* @param integer $signal
*
* @return mixed
*/
public function onWorkerError(SwooleHttpServer $server, $workerId, $workerPid, $exitCode, $signal)
{
// PR('onWorkerError');
// PR(func_get_args());
}

/**
Expand All @@ -143,22 +127,18 @@ public function onWorkerError(SwooleHttpServer $server, $workerId, $workerPid, $
* - onManagerStart回调时,Task和Worker进程已创建
*
* @param SwooleHttpServer $server
* @return mixed
*/
public function onManagerStart(SwooleHttpServer $server)
{
// TODO: Implement onManagerStart() method.
}

/**
* 管理进程结束时回调该方法
* - onManagerStop触发时,说明Task和Worker进程已结束运行,已被Manager进程回收。
*
* @param SwooleHttpServer $server
* @return mixed
*/
public function onManagerStop(SwooleHttpServer $server)
{
// TODO: Implement onManagerStop() method.
}
}
2 changes: 1 addition & 1 deletion app/Util/ThrowException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @package Eye\Util
*/
class ThrowException extends \Bee\RuntimeException
class ThrowException extends \Bee\Exception
{
/**
* 服务不可用
Expand Down
Loading

0 comments on commit b39287b

Please sign in to comment.