Skip to content

Commit 3a103e6

Browse files
author
Vivek
committed
issue-7 stop job client
1 parent 6535fc2 commit 3a103e6

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

docs/Running.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ image::../assets/monitor.png[Monitor Jobs]
3636

3737
See link:Monitoring.adoc[Monitoring / Validating the Content Sync] for information on evaluating the returned information.
3838

39+
=== Stopping a Job
40+
41+
From the main screen, enter "s" to stop an existing job. You will be prompted to enter a job id. Enter the job id that you wish to terminate.
42+
3943

4044
==== Configuration
4145

grabbit.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ function monitorStatus {
8888
done
8989
}
9090

91+
function stopJob {
92+
echo "Enter jobID to stop, or \"b\" to go back"
93+
read selection
94+
if [ "$selection" == "b" ]; then
95+
echo "*****************************************************"
96+
return
97+
fi
98+
statusJson=`curl -s -u $username:$password --request DELETE $client$GRABBIT_JOB"?jobId="$selection`
99+
echo
100+
echo "$statusJson"
101+
}
102+
103+
91104
clear
92105
echo $SET_BLUE
93106
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
@@ -105,12 +118,14 @@ echo "Client Password :"
105118
read -s password
106119

107120
while true; do
108-
echo "Enter \"n\" for new job request, \"m\" to monitor, or \"q\" to quit"
121+
echo "Enter \"n\" for new job request, \"m\" to monitor, \"s\" to stop a job, or \"q\" to quit"
109122
read selection
110123
if [ "$selection" == "n" ]; then
111124
newGrabbitRequest
112125
elif [ "$selection" == "m" ]; then
113126
monitorStatus
127+
elif [ "$selection" == "s" ]; then
128+
stopJob
114129
elif [ "$selection" == "q" ]; then
115130
exit 0;
116131
else

src/main/groovy/com/twcable/grabbit/client/servlets/GrabbitJobServlet.groovy

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ import org.apache.sling.api.SlingHttpServletRequest
3232
import org.apache.sling.api.SlingHttpServletResponse
3333
import org.apache.sling.api.servlets.SlingAllMethodsServlet
3434
import org.springframework.batch.core.explore.JobExplorer
35+
import org.springframework.batch.core.launch.JobExecutionNotRunningException
36+
import org.springframework.batch.core.launch.NoSuchJobExecutionException
37+
import org.springframework.batch.core.launch.support.SimpleJobOperator
3538
import org.springframework.context.ConfigurableApplicationContext
3639

3740
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST
3841
import static javax.servlet.http.HttpServletResponse.SC_OK
42+
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND
3943

4044
/**
4145
* This servlet is used to manage Grabbit jobs.
@@ -49,7 +53,7 @@ import static javax.servlet.http.HttpServletResponse.SC_OK
4953
*/
5054
@Slf4j
5155
@CompileStatic
52-
@SlingServlet(methods = ['GET', 'PUT'], resourceTypes = ['twcable:grabbit/job'])
56+
@SlingServlet(methods = ['GET', 'PUT', 'DELETE'], resourceTypes = ['twcable:grabbit/job'])
5357
class GrabbitJobServlet extends SlingAllMethodsServlet {
5458

5559
//A "special" meta-jobID that allows for the status of all jobs to be queried.
@@ -147,6 +151,38 @@ class GrabbitJobServlet extends SlingAllMethodsServlet {
147151
response.writer.write(new JsonBuilder(jobExecutionIds).toString())
148152
}
149153

154+
@Override
155+
protected void doDelete(SlingHttpServletRequest request, SlingHttpServletResponse response) {
156+
String jobExecutionId = request.getParameter("jobId") ?: ""
157+
158+
if(jobExecutionId == ALL_JOBS_ID) {
159+
response.setStatus(SC_BAD_REQUEST)
160+
response.writer.write("Stopping 'all' jobs is not supported. Please specify single job id")
161+
return
162+
}
163+
if(!jobExecutionId.isLong()) {
164+
log.warn "Parameter ${jobExecutionId} 'jobId' is invalid"
165+
response.status = SC_BAD_REQUEST
166+
response.writer.write("Parameter 'jobId' is invalid")
167+
return
168+
}
169+
try {
170+
SimpleJobOperator jobOperator = configurableApplicationContext.getBean(SimpleJobOperator)
171+
jobOperator.stop(jobExecutionId.toLong())
172+
response.status = SC_OK
173+
response.writer.write("Job ${jobExecutionId} Successfully Stopped")
174+
}
175+
catch (NoSuchJobExecutionException noSuchJobExc){
176+
response.status = SC_NOT_FOUND
177+
response.writer.write("No suhc job exists with id ${jobExecutionId}")
178+
}
179+
catch (JobExecutionNotRunningException jobNotRunningExc){
180+
response.status = SC_OK
181+
response.writer.write("Job already complete for id ${jobExecutionId}. Nothing to Stop")
182+
}
183+
184+
}
185+
150186
/**
151187
* Will return the status of a job from the {@link org.springframework.batch.core.explore.JobExplorer} used in JSON format.
152188
* @param jobId The jobID to get status.

src/test/groovy/com/twcable/grabbit/client/GrabbitJobServletSpec.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import org.apache.sling.api.resource.Resource
2727
import org.apache.sling.api.resource.ResourceMetadata
2828
import org.springframework.batch.core.JobParameters
2929
import org.springframework.batch.core.explore.JobExplorer
30+
import org.springframework.batch.core.launch.JobExecutionNotRunningException
31+
import org.springframework.batch.core.launch.NoSuchJobExecutionException
32+
import org.springframework.batch.core.launch.support.SimpleJobOperator
3033
import org.springframework.context.ConfigurableApplicationContext
3134
import spock.lang.Specification
3235
import spock.lang.Subject
@@ -37,6 +40,7 @@ import static com.twcable.grabbit.client.servlets.GrabbitJobServlet.ALL_JOBS_ID
3740
import static com.twcable.grabbit.testutil.StubInputStream.inputStream
3841
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST
3942
import static javax.servlet.http.HttpServletResponse.SC_OK
43+
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND
4044

4145
@Subject(GrabbitJobServlet)
4246
class GrabbitJobServletSpec extends Specification {
@@ -232,4 +236,38 @@ class GrabbitJobServletSpec extends Specification {
232236
inputStream << [inputStream(" "), inputStream("foo: 'foo'")]
233237
//One causes SnakeYAML to produce a null config map, and the other does not pass our validations (missing values)
234238
}
239+
240+
def "Stop job response check with differnt set of jobId parameter"() {
241+
given:
242+
SlingHttpServletRequest request = Mock(SlingHttpServletRequest) {
243+
getParameter("jobId") >> inputJobId
244+
}
245+
SlingHttpServletResponse response = Mock(SlingHttpServletResponse) {
246+
getWriter() >> Mock(PrintWriter)
247+
}
248+
GrabbitJobServlet servlet = new GrabbitJobServlet()
249+
final applicationContext = Mock(ConfigurableApplicationContext) {
250+
getBean(SimpleJobOperator) >> Mock(SimpleJobOperator) {
251+
stop(123L) >> true
252+
stop(222L) >> {throw new NoSuchJobExecutionException("Job does not exist")}
253+
stop(333L) >> {throw new JobExecutionNotRunningException("Job not running")}
254+
}
255+
}
256+
servlet.setConfigurableApplicationContext(applicationContext)
257+
258+
when:
259+
servlet.doDelete(request, response)
260+
261+
then:
262+
1 * response.setStatus(expectedResponse)
263+
264+
where:
265+
inputJobId | expectedResponse
266+
123L | SC_OK
267+
222L | SC_NOT_FOUND
268+
333L | SC_OK
269+
"aaa" | SC_BAD_REQUEST
270+
"" | SC_BAD_REQUEST
271+
}
272+
235273
}

0 commit comments

Comments
 (0)