-
Notifications
You must be signed in to change notification settings - Fork 61
issue-7-stop-job #146
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
issue-7-stop-job #146
Changes from all 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 |
|---|---|---|
|
|
@@ -32,10 +32,10 @@ import org.apache.sling.api.SlingHttpServletRequest | |
| import org.apache.sling.api.SlingHttpServletResponse | ||
| import org.apache.sling.api.servlets.SlingAllMethodsServlet | ||
| import org.springframework.batch.core.explore.JobExplorer | ||
| import org.springframework.batch.core.launch.support.SimpleJobOperator | ||
| import org.springframework.context.ConfigurableApplicationContext | ||
|
|
||
| import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST | ||
| import static javax.servlet.http.HttpServletResponse.SC_OK | ||
| import static javax.servlet.http.HttpServletResponse.* | ||
|
|
||
| /** | ||
| * This servlet is used to manage Grabbit jobs. | ||
|
|
@@ -49,7 +49,7 @@ import static javax.servlet.http.HttpServletResponse.SC_OK | |
| */ | ||
| @Slf4j | ||
| @CompileStatic | ||
| @SlingServlet(methods = ['GET', 'PUT'], resourceTypes = ['twcable:grabbit/job']) | ||
| @SlingServlet(methods = ['GET', 'PUT', 'DELETE'], resourceTypes = ['twcable:grabbit/job']) | ||
| class GrabbitJobServlet extends SlingAllMethodsServlet { | ||
|
|
||
| //A "special" meta-jobID that allows for the status of all jobs to be queried. | ||
|
|
@@ -147,6 +147,34 @@ class GrabbitJobServlet extends SlingAllMethodsServlet { | |
| response.writer.write(new JsonBuilder(jobExecutionIds).toString()) | ||
| } | ||
|
|
||
| @Override | ||
| protected void doDelete(SlingHttpServletRequest request, SlingHttpServletResponse response) { | ||
| String jobExecutionId = request.getParameter("jobId") ?: "" | ||
|
|
||
| if(jobExecutionId == ALL_JOBS_ID) { | ||
| response.setStatus(SC_BAD_REQUEST) | ||
| response.writer.write("Stopping 'all' jobs is not supported. Please specify single job id") | ||
| return | ||
| } | ||
| if(!jobExecutionId.isLong()) { | ||
| log.warn "Parameter ${jobExecutionId} 'jobId' is invalid" | ||
| response.status = SC_BAD_REQUEST | ||
| response.writer.write("Parameter 'jobId' is invalid") | ||
| return | ||
| } | ||
| try { | ||
|
Member
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. SimpleJobOperator jobOperator = configurableApplicationContext.getBean(SimpleJobOperator)
try {
jobOperator.stop(jobExecutionId.toLong())
}
catch(NoSuchJobExecutionException ex) {
response.status = SC_NOT_FOUND
response.writer.write("No such job exists with id ${jobExecutionId}")
}
catch(JobExecutionNotRunningException ex) {
response.status = SC_OK
response.writer.write("Job already complete for id ${jobExecutionId}. Nothing to stop")
}
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. @jbornemann : This is also something that I had earlier but changed it to single exception block... Reason I changed it:
Member
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. @viveksachdeva I'm not sure what you mean. All jobs, including ones being run are backed by the grabbit repository. I think we should have the distinction...and at the very least do away with catching "Exception" for reasons I've mentioned before. It will just save us from having to clean this up in the future, because the Checker framework will complain about it..
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. @jbornemann : Lets say I have lots of jobs in grabbit repo that were run some time in past and after that instance was restarted, then if I say stop job on one of those jobs, it will give throw NoSuchJobExecutionException instead of JobExecutionNotRunningException...
Member
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. Can you please confirm that is the case, because that is contrary to the documentation : http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/launch/JobOperator.html#stop-long- In your scenario, the jobExecutionId would still be present within the job repository; So when Spring Batch queries the repository, it should not return NoSuchJobExecutionException.. In your scenario, I would expect JobExecutionNotRunningException..
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. Checked it again with older jobs.. It throws NoSuchJobException |
||
| SimpleJobOperator jobOperator = configurableApplicationContext.getBean(SimpleJobOperator) | ||
| jobOperator.stop(jobExecutionId.toLong()) | ||
|
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 should also look to see whether any other cleanup of like the inputstream / session is needed after stopping the Job. Take a look at Also, this will only stop the client .. server will still be wasting time and resources sending his content over :-(
Member
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. Oh wow, yeah, good catch.
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. @sagarsane : afterJob is getting executed automatically on trigger of stop().. |
||
| response.status = SC_OK | ||
| response.writer.write("Job ${jobExecutionId} Successfully Stopped") | ||
| } | ||
| catch (Exception exception){ | ||
| response.status = SC_NOT_FOUND | ||
| response.writer.write("Could not find a running job with id ${jobExecutionId}") | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Will return the status of a job from the {@link org.springframework.batch.core.explore.JobExplorer} used in JSON format. | ||
| * @param jobId The jobID to get status. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm..for the case of "all", wondering if we should provide different messaging than saying it's invalid. Really it's valid, just not supported for DELETE/POST
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbornemann : did not get this point... job id parameter needs to be sent to stop that job.. what is 'all' in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Take a look at https://github.com/TWCable/grabbit/blob/master/src/main/groovy/com/twcable/grabbit/client/servlets/GrabbitJobServlet.groovy#L56
You can use "all" as a special meta-parameter to query all known jobs, e.g:
So what I'm saying here is - if someone tries to
we should not tell them that the jobID 'all' is invalid, because it's not. We should tell them deleting 'all' is unsupported or something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh... that one.. ok