【发布时间】:2018-02-16 13:49:03
【问题描述】:
我们的团队一直在开发这个 RESTful API,使用 Slim PHP 作为路由器,使用 MySQL Propel ORM,并使用这个 app.yaml 配置将其部署为 Google App Engine 中的服务
service: api
runtime: php55
api_version: 1
threadsafe: yes
instance_class: F1
automatic_scaling:
min_idle_instances: automatic
max_idle_instances: automatic
min_pending_latency: automatic
max_pending_latency: automatic
skip_files:
- ^vendor/(.*/)+[Tt]ests/.*$
- ^\.(.*)
handlers:
- url: .*
script: app.php
被 Ember.js Web 应用程序使用,通过我们收到的奇怪无模式服务器崩溃 500 秒的所有开发,更准确地说:
500 Server Error Error: Server Error 服务器遇到错误 并且无法完成您的请求。请在 30 秒后重试。
使用 App Engine 日志。
处理此请求的进程遇到问题, 导致它退出。这很可能会导致使用新的进程 为您的应用程序的下一个请求。 (错误代码 204)
在随机端点中,否则 99% 的时间都可以正常工作,我们当然不想在这些随机崩溃的情况下投入生产。
我们尝试过的:
- 检查是否已达到 MySQL max_connections,因为我们每次请求都会打开和关闭一个连接。
- 将我们的实例从 F1 升级到 F4_1G 以解决内存不足的问题。
- 在 localhost 中使用 dev_appserver.py 进行压力测试(我们在这里没有遇到任何崩溃)
- 尝试捕获整个 Slim 应用程序进行调试(它实际上从未捕获到异常,因此我们认为它确实与 Google App Engine 有关)
这是正常请求流程的一些代码。
app.php
/*
* Create SLIM application
*/
$app = new \Slim\App([
"settings" => [
"determineRouteBeforeAppMiddleware" => true,
]
]);
//Custom Authentication middleware
$app->add(new \OAuth2Server\SlimAuthenticationMiddleware());
//CORS and Headers Middleware
$app->add(function($request, $response, $next) {
$response = $next($request, $response);
$response = $response->withHeader("Access-Control-Allow-Origin", "*");
$response = $response->withHeader("Access-Control-Allow-Headers", "Content-Type, authorization");
$response = $response->withHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, OPTIONS");
$response = $response->withHeader("content-type", "application/json; charset=utf8");
return $response;
});
require_once("router.php");
$app->run();
路由器.php
$app->get($apiVersionPath.'/visits/{id}','\Controllers\Visits:get')
->add(new \OAuth2Server\ValidateRequestUser(array("doctor", "nurse","superuser","admin")));
访问控制器 GET/ID 相关代码。
/**
* @param Request $request
* @param Response $response
* @param []$args
* @return Response
*/
public function get($request, $response, $args) {
$id = $request->getAttribute("route")->getArgument("id");
$serializer = new Serializer();
if(!is_numeric($id) || $id == 0){
throw new InvalidArgumentException("Invalid Argument");
}
$visit = \VisitQuery::create()
->useUserQuery()
->filterByClientId($request->getAttribute("user")->getClientId())
->endUse();
$visit = $visit->findPk($id);
if(!isset($visit) || !($visit instanceof \Visit)){
throw new EntityNotFoundException("Visit not found");
}
$resource = $visit->toResource(false);
$serializer->addResource($resource);
$body = $response->getBody();
$body->write($serializer->serialize());
return $response;
}
【问题讨论】:
-
根据this issue 204s 通常表示内存问题。由于碰撞实例类型没有帮助并且没有观察到任何模式,我只能建议添加您的应用程序代码,以防有人注意到可疑或可能的解释。
-
谢谢@DanCornilescu,我刚刚添加了一些请求流程的代码
标签: php mysql google-app-engine slim propel