-
Notifications
You must be signed in to change notification settings - Fork 45
DS-2036: Cancel job if client cancels connection. #1770
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| package com.here.xyz.jobs.service; | ||
|
|
||
| import static com.here.xyz.jobs.RuntimeInfo.State.CANCELLING; | ||
| import static com.here.xyz.jobs.RuntimeInfo.State.FAILED; | ||
| import static com.here.xyz.jobs.RuntimeInfo.State.NOT_READY; | ||
| import static com.here.xyz.jobs.RuntimeInfo.State.RUNNING; | ||
|
|
@@ -47,12 +48,15 @@ | |
| import com.here.xyz.util.service.BaseHttpServerVerticle.ValidationException; | ||
| import com.here.xyz.util.service.HttpException; | ||
| import com.here.xyz.util.service.errors.DetailedHttpException; | ||
| import io.netty.handler.codec.http.HttpResponseStatus; | ||
| import io.vertx.core.Future; | ||
| import io.vertx.ext.web.RoutingContext; | ||
| import io.vertx.ext.web.openapi.router.RouterBuilder; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.apache.commons.lang3.NotImplementedException; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
@@ -87,10 +91,27 @@ protected void postJob(final RoutingContext context) throws HttpException { | |
|
|
||
| protected Future<Job> createNewJob(RoutingContext context, Job job) { | ||
| logger.info(getMarker(context), "Received job creation request: {}", job.serialize(true)); | ||
| AtomicBoolean clientGone = new AtomicBoolean(false); | ||
|
|
||
| context.request().connection().closeHandler(v -> { | ||
| clientGone.set(true); | ||
| }); | ||
|
|
||
| return job.create().submit() | ||
| .compose(v -> abortIfClientGone(clientGone)) | ||
| .compose(v -> applyInputReferences(job)) | ||
| .compose(v -> abortIfClientGone(clientGone)) | ||
| .map(res -> job) | ||
| .recover(t -> { | ||
| if(t instanceof HttpException e && e.status == HttpResponseStatus.GONE) { | ||
| logger.warn(getMarker(context), e.getMessage()); | ||
| job.getStatus() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case we cant provide a proper errorMessage.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will not be able to provide the error message anyway, coz the client closed the connection already. |
||
| .withState(CANCELLING) | ||
| .withErrorMessage("Client disconnected.") | ||
|
roegi marked this conversation as resolved.
Outdated
|
||
| .withErrorCause(t.getMessage()); | ||
| job.store(); | ||
|
roegi marked this conversation as resolved.
Outdated
|
||
| return Future.failedFuture(e); | ||
| } | ||
| if (t instanceof CompilationError) | ||
| return Future.failedFuture(new DetailedHttpException("E319002", t)); | ||
| if (t instanceof ValidationException) | ||
|
|
@@ -104,6 +125,13 @@ protected Future<Job> createNewJob(RoutingContext context, Job job) { | |
| .onFailure(err -> sendErrorResponse(context, err)); | ||
| } | ||
|
|
||
| private <T> Future<T> abortIfClientGone(AtomicBoolean clientGone) { | ||
| if (clientGone.get()) { | ||
| return Future.failedFuture(new HttpException(HttpResponseStatus.GONE,"Client disconnected during job creation. Aborting job.")); | ||
| } | ||
| return Future.succeededFuture(); | ||
| } | ||
|
|
||
| protected Future<Void> applyInputReferences(Job job) { | ||
| if (job.getInputs() == null) | ||
| return Future.succeededFuture(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.