Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for CompletionListener for the DevAppServer #357

Merged
merged 2 commits into from
Mar 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -621,86 +621,86 @@ public void doScope(
HttpServletResponse response)
throws IOException, ServletException {

org.eclipse.jetty.server.Request.addCompletionListener(
baseRequest.getCoreRequest(),
t -> {
try {
// a special hook with direct access to the container instance
// we invoke this only after the normal request processing,
// in order to generate a valid response
if (request.getRequestURI().startsWith(AH_URL_RELOAD)) {
try {
reloadWebApp();
log.info("Reloaded the webapp context: " + request.getParameter("info"));
} catch (Exception ex) {
log.log(Level.WARNING, "Failed to reload the current webapp context.", ex);
}
}
} finally {

LocalEnvironment env =
(LocalEnvironment) request.getAttribute(LocalEnvironment.class.getName());
if (env != null) {
environments.remove(env);

// Acquire all of the semaphores back, which will block if any are outstanding.
Semaphore semaphore =
(Semaphore) env.getAttributes().get(LocalEnvironment.API_CALL_SEMAPHORE);
try {
semaphore.acquire(MAX_SIMULTANEOUS_API_CALLS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
log.log(
Level.WARNING, "Interrupted while waiting for API calls to complete:", ex);
}

try {
ApiProxy.setEnvironmentForCurrentThread(env);

// Invoke all of the registered RequestEndListeners.
env.callRequestEndListeners();

if (apiProxyDelegate instanceof ApiProxyLocal) {
// If apiProxyDelegate is not instanceof ApiProxyLocal, we are presumably
// running in
// the devappserver2 environment, where the master web server in Python will
// take care
// of logging requests.
ApiProxyLocal apiProxyLocal = (ApiProxyLocal) apiProxyDelegate;
String appId = env.getAppId();
String versionId = env.getVersionId();
String requestId = DevLogHandler.getRequestId();

LocalLogService logService =
(LocalLogService) apiProxyLocal.getService(LocalLogService.PACKAGE);

@SuppressWarnings("NowMillis")
long nowMillis = System.currentTimeMillis();
logService.addRequestInfo(
appId,
versionId,
requestId,
request.getRemoteAddr(),
request.getRemoteUser(),
baseRequest.getTimeStamp() * 1000,
nowMillis * 1000,
request.getMethod(),
request.getRequestURI(),
request.getProtocol(),
request.getHeader("User-Agent"),
true,
response.getStatus(),
request.getHeader("Referrer"));
logService.clearResponseSize();
if (baseRequest.getDispatcherType() == DispatcherType.REQUEST) {
org.eclipse.jetty.server.Request.addCompletionListener(
baseRequest.getCoreRequest(),
t -> {
try {
// a special hook with direct access to the container instance
// we invoke this only after the normal request processing,
// in order to generate a valid response
if (request.getRequestURI().startsWith(AH_URL_RELOAD)) {
try {
reloadWebApp();
log.info("Reloaded the webapp context: " + request.getParameter("info"));
} catch (Exception ex) {
log.log(Level.WARNING, "Failed to reload the current webapp context.", ex);
}
}
} finally {

LocalEnvironment env =
(LocalEnvironment) request.getAttribute(LocalEnvironment.class.getName());
if (env != null) {
environments.remove(env);

// Acquire all of the semaphores back, which will block if any are outstanding.
Semaphore semaphore =
(Semaphore) env.getAttributes().get(LocalEnvironment.API_CALL_SEMAPHORE);
try {
semaphore.acquire(MAX_SIMULTANEOUS_API_CALLS);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
log.log(
Level.WARNING, "Interrupted while waiting for API calls to complete:", ex);
}

try {
ApiProxy.setEnvironmentForCurrentThread(env);

// Invoke all of the registered RequestEndListeners.
env.callRequestEndListeners();

if (apiProxyDelegate instanceof ApiProxyLocal) {
// If apiProxyDelegate is not instanceof ApiProxyLocal, we are presumably
// running in
// the devappserver2 environment, where the master web server in Python will
// take care
// of logging requests.
ApiProxyLocal apiProxyLocal = (ApiProxyLocal) apiProxyDelegate;
String appId = env.getAppId();
String versionId = env.getVersionId();
String requestId = DevLogHandler.getRequestId();

LocalLogService logService =
(LocalLogService) apiProxyLocal.getService(LocalLogService.PACKAGE);

@SuppressWarnings("NowMillis")
long nowMillis = System.currentTimeMillis();
logService.addRequestInfo(
appId,
versionId,
requestId,
request.getRemoteAddr(),
request.getRemoteUser(),
baseRequest.getTimeStamp() * 1000,
nowMillis * 1000,
request.getMethod(),
request.getRequestURI(),
request.getProtocol(),
request.getHeader("User-Agent"),
true,
response.getStatus(),
request.getHeader("Referrer"));
logService.clearResponseSize();
}
} finally {
ApiProxy.clearEnvironmentForCurrentThread();
}
}
}
} finally {
ApiProxy.clearEnvironmentForCurrentThread();
}
}
}
});
});

if (baseRequest.getDispatcherType() == DispatcherType.REQUEST) {
Semaphore semaphore = new Semaphore(MAX_SIMULTANEOUS_API_CALLS);

LocalEnvironment env =
Expand Down
Loading