Improve Argeo Init shutdown sequence.
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 4 Mar 2022 09:23:04 +0000 (10:23 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 4 Mar 2022 09:23:04 +0000 (10:23 +0100)
org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java
org.argeo.init/src/org/argeo/init/RuntimeContext.java
org.argeo.init/src/org/argeo/init/Service.java
org.argeo.init/src/org/argeo/init/logging/ThinLogging.java
org.argeo.init/src/org/argeo/init/osgi/OsgiRuntimeContext.java

index b493c08ef3cb923d286444d96e96b8e1c1fdc4ab..a0c4b0c0b8a445346e34921d3d3c3a29b7fdab81 100644 (file)
@@ -9,7 +9,6 @@ import javax.security.auth.login.Configuration;
 import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsState;
 import org.argeo.cms.auth.ident.IdentClient;
 import org.argeo.api.cms.CmsLog;
 import org.argeo.api.cms.CmsState;
 import org.argeo.cms.auth.ident.IdentClient;
-import org.argeo.cms.internal.osgi.CmsShutdown;
 import org.osgi.framework.Constants;
 
 /**
 import org.osgi.framework.Constants;
 
 /**
@@ -17,32 +16,20 @@ import org.osgi.framework.Constants;
  */
 public class CmsStateImpl implements CmsState {
        private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class);
  */
 public class CmsStateImpl implements CmsState {
        private final static CmsLog log = CmsLog.getLog(CmsStateImpl.class);
-//     private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
-
-//     private static CmsStateImpl instance;
-
-//     private ExecutorService internalExecutorService;
 
        // REFERENCES
        private Long availableSince;
 
 
        // REFERENCES
        private Long availableSince;
 
-//     private ThreadGroup threadGroup = new ThreadGroup("CMS");
-//     private List<Runnable> stopHooks = new ArrayList<>();
-
        private String stateUuid;
 //     private final boolean cleanState;
        private String hostname;
 
        public void start() {
        private String stateUuid;
 //     private final boolean cleanState;
        private String hostname;
 
        public void start() {
-//             instance = this;
-
-               Runtime.getRuntime().addShutdownHook(new CmsShutdown());
-//             this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
+//             Runtime.getRuntime().addShutdownHook(new CmsShutdown());
 
                try {
                        initSecurity();
 //                     initArgeoLogger();
 
                try {
                        initSecurity();
 //                     initArgeoLogger();
-//                     initNode();
 
                        if (log.isTraceEnabled())
                                log.trace("CMS State started");
 
                        if (log.isTraceEnabled())
                                log.trace("CMS State started");
@@ -81,139 +68,14 @@ public class CmsStateImpl implements CmsState {
                Configuration.getConfiguration();
        }
 
                Configuration.getConfiguration();
        }
 
-//     private void initI18n() {
-//             Object defaultLocaleValue = KernelUtils.getFrameworkProp(CmsConstants.I18N_DEFAULT_LOCALE);
-//             defaultLocale = defaultLocaleValue != null ? new Locale(defaultLocaleValue.toString())
-//                             : new Locale(ENGLISH.getLanguage());
-//             locales = LocaleUtils.asLocaleList(KernelUtils.getFrameworkProp(CmsConstants.I18N_LOCALES));
-//     }
-
-       private void initServices() {
-               // JTA
-//             String tmType = KernelUtils.getFrameworkProp(CmsConstants.TRANSACTION_MANAGER,
-//                             CmsConstants.TRANSACTION_MANAGER_SIMPLE);
-//             if (CmsConstants.TRANSACTION_MANAGER_SIMPLE.equals(tmType)) {
-//                     initSimpleTransactionManager();
-//             } else if (CmsConstants.TRANSACTION_MANAGER_BITRONIX.equals(tmType)) {
-////                   initBitronixTransactionManager();
-//                     throw new UnsupportedOperationException(
-//                                     "Bitronix is not supported anymore, but could be again if there is enough interest.");
-//             } else {
-//                     throw new IllegalArgumentException("Usupported transaction manager type " + tmType);
-//             }
-
-               // POI
-//             POIXMLTypeLoader.setClassLoader(CTConnection.class.getClassLoader());
-
-               // Tika
-//             OpenDocumentParser odfParser = new OpenDocumentParser();
-//             bc.registerService(Parser.class, odfParser, new Hashtable());
-//             PDFParser pdfParser = new PDFParser();
-//             bc.registerService(Parser.class, pdfParser, new Hashtable());
-//             OOXMLParser ooxmlParser = new OOXMLParser();
-//             bc.registerService(Parser.class, ooxmlParser, new Hashtable());
-//             TesseractOCRParser ocrParser = new TesseractOCRParser();
-//             ocrParser.setLanguage("ara");
-//             bc.registerService(Parser.class, ocrParser, new Hashtable());
-
-//             // JCR
-//             RepositoryServiceFactory repositoryServiceFactory = new RepositoryServiceFactory();
-//             stopHooks.add(() -> repositoryServiceFactory.shutdown());
-//             Activator.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
-//                             LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
-//
-//             NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
-//             Activator.registerService(RepositoryFactory.class, repositoryFactory, null);
-
-               // Security
-//             NodeUserAdmin userAdmin = new NodeUserAdmin(CmsConstants.ROLES_BASEDN, CmsConstants.TOKENS_BASEDN);
-//             stopHooks.add(() -> userAdmin.destroy());
-//             Activator.registerService(ManagedServiceFactory.class, userAdmin,
-//                             LangUtils.dict(Constants.SERVICE_PID, CmsConstants.NODE_USER_ADMIN_PID));
-
-       }
-
-//     private void initSimpleTransactionManager() {
-//             SimpleTransactionManager transactionManager = new SimpleTransactionManager();
-//             Activator.registerService(WorkControl.class, transactionManager, null);
-//             Activator.registerService(WorkTransaction.class, transactionManager, null);
-////           Activator.registerService(TransactionManager.class, transactionManager, null);
-////           Activator.registerService(UserTransaction.class, transactionManager, null);
-//             // TODO TransactionSynchronizationRegistry
-//     }
-
-//     private void initBitronixTransactionManager() {
-//             // TODO manage it in a managed service, as startup could be long
-//             ServiceReference<TransactionManager> existingTm = bc.getServiceReference(TransactionManager.class);
-//             if (existingTm != null) {
-//                     if (log.isDebugEnabled())
-//                             log.debug("Using provided transaction manager " + existingTm);
-//                     return;
-//             }
-//
-//             if (!TransactionManagerServices.isTransactionManagerRunning()) {
-//                     bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
-//                     tmConf.setServerId(UUID.randomUUID().toString());
-//
-//                     Bundle bitronixBundle = FrameworkUtil.getBundle(bitronix.tm.Configuration.class);
-//                     File tmBaseDir = bitronixBundle.getDataFile(KernelConstants.DIR_TRANSACTIONS);
-//                     File tmDir1 = new File(tmBaseDir, "btm1");
-//                     tmDir1.mkdirs();
-//                     tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog").getAbsolutePath());
-//                     File tmDir2 = new File(tmBaseDir, "btm2");
-//                     tmDir2.mkdirs();
-//                     tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog").getAbsolutePath());
-//             }
-//             BitronixTransactionManager transactionManager = getTransactionManager();
-//             stopHooks.add(() -> transactionManager.shutdown());
-//             BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
-//             // register
-//             bc.registerService(TransactionManager.class, transactionManager, null);
-//             bc.registerService(UserTransaction.class, transactionManager, null);
-//             bc.registerService(TransactionSynchronizationRegistry.class, transactionSynchronizationRegistry, null);
-//             if (log.isDebugEnabled())
-//                     log.debug("Initialised default Bitronix transaction manager");
-//     }
-
        public void stop() {
                if (log.isDebugEnabled())
                        log.debug("CMS stopping...  (" + this.stateUuid + ")");
        public void stop() {
                if (log.isDebugEnabled())
                        log.debug("CMS stopping...  (" + this.stateUuid + ")");
-//             new GogoShellKiller().start();
-
-               // In a different thread in order to avoid interruptions
-//             Thread stopHookThread = new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks");
-//             stopHookThread.start();
-//             try {
-//                     stopHookThread.join(10 * 60 * 1000);
-//             } catch (InterruptedException e) {
-//                     // silent
-//             }
-
-//             internalExecutorService.shutdown();
 
                long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
                log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
        }
 
 
                long duration = ((System.currentTimeMillis() - availableSince) / 1000) / 60;
                log.info("## ARGEO CMS STOPPED after " + (duration / 60) + "h " + (duration % 60) + "min uptime ##");
        }
 
-       /** Apply shutdown hoos in reverse order. */
-//     private void applyStopHooks() {
-////           for (int i = stopHooks.size() - 1; i >= 0; i--) {
-////                   try {
-////                           stopHooks.get(i).run();
-////                   } catch (Exception e) {
-////                           log.error("Could not run shutdown hook #" + i);
-////                   }
-////           }
-//             // Clean hanging Gogo shell thread
-//             new GogoShellKiller().start();
-//
-////           instance = null;
-//     }
-
-//     @Override
-//     public boolean isClean() {
-//             return cleanState;
-//     }
 
        @Override
        public Long getAvailableSince() {
 
        @Override
        public Long getAvailableSince() {
index 7dd8e6c0c65832ed499c097d8e4508d74520bcbe..1d3c743ca765b85b564a8cf9f8ff213158463a79 100644 (file)
@@ -1,5 +1,7 @@
 package org.argeo.init;
 
 package org.argeo.init;
 
-public interface RuntimeContext extends Runnable, AutoCloseable {
+public interface RuntimeContext extends Runnable {
        void waitForStop(long timeout) throws InterruptedException;
        void waitForStop(long timeout) throws InterruptedException;
+
+       void close() throws Exception;
 }
 }
index 03e2f12732941952f90302854d9218e9f020344a..9dcea49d2d6199cad72b9696191de85ecbd69f53 100644 (file)
@@ -31,6 +31,7 @@ public class Service implements Runnable, AutoCloseable {
                Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                        try {
                                if (Service.runtimeContext != null) {
                Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                        try {
                                if (Service.runtimeContext != null) {
+//                                     System.out.println("Argeo Init stopping with PID " + pid);
                                        Service.runtimeContext.close();
                                        Service.runtimeContext.waitForStop(0);
                                }
                                        Service.runtimeContext.close();
                                        Service.runtimeContext.waitForStop(0);
                                }
@@ -47,22 +48,26 @@ public class Service implements Runnable, AutoCloseable {
 //                     log.log(Logger.Level.DEBUG, key + "=" + System.getProperty(key.toString()));
 //             }
                try {
 //                     log.log(Logger.Level.DEBUG, key + "=" + System.getProperty(key.toString()));
 //             }
                try {
-                       try (OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext((Map<String, String>) config)) {
+                       try {
+                               OsgiRuntimeContext osgiRuntimeContext = new OsgiRuntimeContext((Map<String, String>) config);
                                osgiRuntimeContext.run();
                                Service.runtimeContext = osgiRuntimeContext;
                                Service.runtimeContext.waitForStop(0);
                        } catch (NoClassDefFoundError e) {
                                osgiRuntimeContext.run();
                                Service.runtimeContext = osgiRuntimeContext;
                                Service.runtimeContext.waitForStop(0);
                        } catch (NoClassDefFoundError e) {
-                               try (StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map<String, String>) config)) {
-                                       staticRuntimeContext.run();
-                                       Service.runtimeContext = staticRuntimeContext;
-                                       Service.runtimeContext.waitForStop(0);
-                               }
+                               StaticRuntimeContext staticRuntimeContext = new StaticRuntimeContext((Map<String, String>) config);
+                               staticRuntimeContext.run();
+                               Service.runtimeContext = staticRuntimeContext;
+                               Service.runtimeContext.waitForStop(0);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        System.exit(1);
                }
-               log.log(Logger.Level.DEBUG, "Argeo Init stopping with PID " + pid);
+               log.log(Logger.Level.DEBUG, "Argeo Init stopped with PID " + pid);
        }
 
        }
 
+       
+       public static RuntimeContext getRuntimeContext() {
+               return runtimeContext;
+       }
 }
 }
index e02dae0b4af53282017ef368844b78884db7db01..e21899394dda63abdef24514b9bad69516974472 100644 (file)
@@ -26,6 +26,9 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
 
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Consumer;
 
+import org.argeo.init.RuntimeContext;
+import org.argeo.init.Service;
+
 /**
  * A thin logging system based on the {@link Logger} framework. It is a
  * {@link Consumer} of configuration, and can be registered as such.
 /**
  * A thin logging system based on the {@link Logger} framework. It is a
  * {@link Consumer} of configuration, and can be registered as such.
@@ -115,6 +118,15 @@ class ThinLogging implements Consumer<Map<String, Object>> {
        }
 
        private void close() {
        }
 
        private void close() {
+               RuntimeContext runtimeContext = Service.getRuntimeContext();
+               if (runtimeContext != null) {
+                       try {
+                               runtimeContext.waitForStop(0);
+                       } catch (InterruptedException e) {
+                               // silent
+                       }
+               }
+
                publisher.close();
                try {
                        // we ait a bit in order to make sure all messages are flushed
                publisher.close();
                try {
                        // we ait a bit in order to make sure all messages are flushed
index 6c3e40ce57eadf8189f9aa8c8687c4c3d2c1c27e..373e3d6719115030884a2ed770b7dbf6667724d9 100644 (file)
@@ -11,6 +11,7 @@ import java.util.function.Consumer;
 
 import org.argeo.init.RuntimeContext;
 import org.argeo.init.logging.ThinLoggerFinder;
 
 import org.argeo.init.RuntimeContext;
 import org.argeo.init.logging.ThinLoggerFinder;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
@@ -91,14 +92,25 @@ public class OsgiRuntimeContext implements RuntimeContext {
        public void waitForStop(long timeout) throws InterruptedException {
                if (framework == null)
                        throw new IllegalStateException("Framework is not initialised");
        public void waitForStop(long timeout) throws InterruptedException {
                if (framework == null)
                        throw new IllegalStateException("Framework is not initialised");
+
                framework.waitForStop(timeout);
        }
 
                framework.waitForStop(timeout);
        }
 
-       @Override
        public void close() throws Exception {
        public void close() throws Exception {
+               // TODO make shutdown of dynamic service more robust
+               Bundle scrBundle = osgiBoot.getBundlesBySymbolicName().get("org.apache.felix.scr");
+               if (scrBundle != null) {
+                       scrBundle.stop();
+                       while (!(scrBundle.getState() <= Bundle.RESOLVED)) {
+                               Thread.sleep(500);
+                       }
+                       Thread.sleep(1000);
+               }
+
                stop(framework.getBundleContext());
                if (framework != null)
                        framework.stop();
                stop(framework.getBundleContext());
                if (framework != null)
                        framework.stop();
+
        }
 
 }
        }
 
 }