karuturi commented on a change in pull request #1832: CLOUDSTACK-9652 Job 
framework - Cancelling async jobs
URL: https://github.com/apache/cloudstack/pull/1832#discussion_r115916534
 
 

 ##########
 File path: 
framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java
 ##########
 @@ -1075,4 +1135,65 @@ private void publishOnEventBus(AsyncJob job, String 
jobEvent) {
     public List<AsyncJobVO> findFailureAsyncJobs(String... cmds) {
         return _jobDao.getFailureJobsSinceLastMsStart(getMsid(), cmds);
     }
+
+    @Override
+    public String cancelAsyncJob(long jobId, String reason) {
+        final AsyncJobVO job = _jobDao.findById(jobId);
+        String errString;
+
+        if (job == null) {
+            errString="Cannot cancel. job-" + jobId + " no longer exists.";
+            if (s_logger.isDebugEnabled()) {
+                s_logger.debug(errString);
+            }
+            // still purge item from queue to avoid any blocking
+            _queueMgr.purgeAsyncJobQueueItemId(jobId);
+            return errString;
+        }
+        try {
+            Class<?> cmdClass = Class.forName(job.getCmd());
+            if (! CancellableCmd.class.isAssignableFrom(cmdClass)) {
+                errString="Cannot cancel. job-" + jobId + " as it is not 
cancellable.";
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug(errString);
+                }
+                return errString;
+            }
+        }catch (ClassNotFoundException e){
+            errString="Command- " + job.getCmd() + " of jobid- " + jobId + " 
not found.";
+            s_logger.error(errString, e);
+            return errString;
+        }
+        if (job.getStatus() == Status.IN_PROGRESS) {
+            if (s_logger.isDebugEnabled()) {
+                s_logger.debug("cancelling job-" + jobId + " which is in 
IN_PROGRESS state.");
+            }
+            try {
+                completeAsyncJob(jobId, JobInfo.Status.CANCELLED, 0, "Job is 
cancelled due to " + reason);
+                _jobMonitor.unregisterByJobId(jobId);
+
+                // purge the item and resume queue processing
+                _queueMgr.purgeItem(jobId);
+
+                return "";
 
 Review comment:
   Can be anything based on the method contract. empty just avoids null points 
incase its not checked properly by the caller.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to