Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(992)

Unified Diff: lib/src/appengine_internal.dart

Issue 775043002: Add withAppEngineService() to package:appengine (Closed) Base URL: git@github.com:dart-lang/appengine.git@master
Patch Set: Rebased on HEAD Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/appengine.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/appengine_internal.dart
diff --git a/lib/src/appengine_internal.dart b/lib/src/appengine_internal.dart
index 3ed2cd7494bdff3f68f93c5c15367f93d936891e..c743cdf36cdfb618e7425a10b19a311c8fc1b85d 100644
--- a/lib/src/appengine_internal.dart
+++ b/lib/src/appengine_internal.dart
@@ -97,12 +97,17 @@ Future<ContextRegistry> initializeAppEngine() {
}
}
- var context = getDockerContext();
- var rpcService = initializeRPC();
-
- return getStorage(context).then((storage) {
- return new ContextRegistry(rpcService, storage, context);
- });
+ if (_contextRegistry != null) {
+ return new Future.value(_contextRegistry);
+ } else {
+ var context = getDockerContext();
+ var rpcService = initializeRPC();
+
+ return getStorage(context).then((storage) {
+ _contextRegistry = new ContextRegistry(rpcService, storage, context);
+ return _contextRegistry;
+ });
+ }
}
void initializeContext(Services services) {
@@ -127,50 +132,52 @@ void initializeRequestSpecificServices(Services services) {
users.registerUserService(services.users);
}
-Future runAppEngine(void handler(request, context), void onError(e, s)) {
+Future withAppEngineServices(Future callback()) {
return initializeAppEngine().then((ContextRegistry contextRegistry) {
- _contextRegistry = contextRegistry;
- var appengineServer = new AppEngineHttpServer(_contextRegistry);
- var backgroundServices = _contextRegistry.newBackgroundServices();
-
- ss.fork(() {
+ return ss.fork(() {
+ var backgroundServices = _contextRegistry.newBackgroundServices();
initializeContext(backgroundServices);
- appengineServer.run((request, context) {
- ss.fork(() {
- initializeRequestSpecificServices(context.services);
- handler(request, context);
- return request.response.done;
- }, onError: (error, stack) {
- var context = _contextRegistry.lookup(request);
- if (context != null) {
- try {
- context.services.logging.error(
- 'Uncaught error in request handler: $error\n$stack');
- } catch (e) {
- print('Error while logging uncaught error: $e');
- }
- } else {
- // TODO: We could log on the background ticket here.
- print('Unable to log error, since response has already been sent.');
- }
- onError('Uncaught error in request handler zone: $error', stack);
+ return callback();
+ });
+ });
+}
- // In many cases errors happen during request processing or response
- // preparation. In such cases we want to close the connection, since
- // user code might not be able to.
+Future runAppEngine(void handler(request, context), void onError(e, s)) {
+ return withAppEngineServices(() {
+ var appengineServer = new AppEngineHttpServer(_contextRegistry);
+ appengineServer.run((request, context) {
+ ss.fork(() {
+ initializeRequestSpecificServices(context.services);
+ handler(request, context);
+ return request.response.done;
+ }, onError: (error, stack) {
+ var context = _contextRegistry.lookup(request);
+ if (context != null) {
try {
- request.response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
- } on StateError catch (_) {}
- request.response.close().catchError((closeError, closeErrorStack) {
- onError('Forcefully closing response, due to error in request '
- 'handler zone, resulted in an error: $closeError',
- closeErrorStack);
- });
+ context.services.logging.error(
+ 'Uncaught error in request handler: $error\n$stack');
+ } catch (e) {
+ print('Error while logging uncaught error: $e');
+ }
+ } else {
+ // TODO: We could log on the background ticket here.
+ print('Unable to log error, since response has already been sent.');
+ }
+ onError('Uncaught error in request handler zone: $error', stack);
+
+ // In many cases errors happen during request processing or response
+ // preparation. In such cases we want to close the connection, since
+ // user code might not be able to.
+ try {
+ request.response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
+ } on StateError catch (_) {}
+ request.response.close().catchError((closeError, closeErrorStack) {
+ onError('Forcefully closing response, due to error in request '
+ 'handler zone, resulted in an error: $closeError',
+ closeErrorStack);
});
});
- return appengineServer.done;
});
-
- return new Future.value();
+ return appengineServer.done;
});
}
« no previous file with comments | « lib/appengine.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698