davidjumani commented on code in PR #6755:
URL: https://github.com/apache/cloudstack/pull/6755#discussion_r1009029612


##########
plugins/shutdown/src/main/java/org/apache/cloudstack/shutdown/ShutdownManagerImpl.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.cloudstack.shutdown;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.cloudstack.api.command.CancelShutdownCmd;
+import org.apache.cloudstack.api.command.PrepareForShutdownCmd;
+import org.apache.cloudstack.api.command.ReadyForShutdownCmd;
+import org.apache.cloudstack.api.command.TriggerShutdownCmd;
+import org.apache.cloudstack.api.response.ReadyForShutdownResponse;
+import org.apache.cloudstack.framework.jobs.AsyncJobManager;
+import org.apache.log4j.Logger;
+
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.component.PluggableService;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class ShutdownManagerImpl extends ManagerBase implements 
ShutdownManager, PluggableService{
+
+    private static Logger s_logger = 
Logger.getLogger(ShutdownManagerImpl.class);
+
+    @Inject
+    private AsyncJobManager _jobMgr;
+
+    private boolean shutdownTriggered = false;
+
+    @Override
+    public boolean isShutdownTriggered() {
+        return shutdownTriggered;
+    }
+
+    protected ShutdownManagerImpl() {
+        super();
+    }
+
+    @Override
+    public long countPendingJobs() {
+        return _jobMgr.countPendingNonPseudoJobs();
+    }
+
+    @Override
+    public ReadyForShutdownResponse readyForShutdown() {
+        long pendingJobCount = countPendingJobs();
+        return new ReadyForShutdownResponse(!_jobMgr.isAllowAsyncJobs(), 
pendingJobCount == 0, pendingJobCount);
+    }
+
+    @Override
+    public ReadyForShutdownResponse prepareForShutdown() {
+        _jobMgr.disableAllowAsyncJobs();
+       return readyForShutdown();
+    }
+
+    @Override
+    public ReadyForShutdownResponse triggerShutdown(Boolean forced) {
+        this.shutdownTriggered = true;
+        if (forced != null && forced) {
+            System.exit(0);
+        }
+        _jobMgr.disableAllowAsyncJobs();
+        Thread shutdownTask = new ShutdownTask(this);
+        shutdownTask.start();
+        return readyForShutdown();
+    }
+
+    @Override
+    public ReadyForShutdownResponse cancelShutdown() {
+        this.shutdownTriggered = false;
+        if (_jobMgr.isAllowAsyncJobs()) {
+            throw new CloudRuntimeException("A shutdown has not been 
triggered");
+        }
+
+        _jobMgr.enableAllowAsyncJobs();
+       return readyForShutdown();
+    }
+
+    @Override
+    public List<Class<?>> getCommands() {
+        final List<Class<?>> cmdList = new ArrayList<>();
+        cmdList.add(CancelShutdownCmd.class);
+        cmdList.add(PrepareForShutdownCmd.class);
+        cmdList.add(ReadyForShutdownCmd.class);
+        cmdList.add(TriggerShutdownCmd.class);
+        return cmdList;
+    }
+
+    private final class ShutdownTask extends Thread {
+
+        private ShutdownManager shutdownManager;
+
+        private ShutdownTask(final ShutdownManager shutdownManager) {
+            this.shutdownManager = shutdownManager;
+        }
+
+        @Override
+        public void run() {
+            while(true) {
+                try {
+                    String msg = String.format("Checking for triggered 
shutdown... shutdownTriggered [%b] AllowAsyncJobs [%b] PendingJobCount [%d]",
+                        shutdownManager.isShutdownTriggered(), 
_jobMgr.isAllowAsyncJobs(), shutdownManager.countPendingJobs());
+                    s_logger.info(msg);
+                    if (!shutdownManager.isShutdownTriggered()) {
+                        return;
+                    }
+                    if (shutdownManager.countPendingJobs() == 0) {
+                        s_logger.info("Shutting down now");
+                        System.exit(0);

Review Comment:
   When `System.exit()` is called, all shutdown hooks are triggered and only 
when complete does the program exit
   
   https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exit(int)
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to