From 452a5fb6e0cd7b4694b08a7c14db32ea3662e2e6 Mon Sep 17 00:00:00 2001 From: Stephen Bolton Date: Mon, 6 Jan 2025 18:10:21 +0000 Subject: [PATCH] Add shutdown-on-startup --- .../api/web/RequestThreadLocalListener.java | 6 +++- .../listeners/ContextLifecycleListener.java | 3 +- .../servlets/FinalStartupServlet.java | 34 +++++++++++++++++++ .../dotmarketing/servlets/InitServlet.java | 3 ++ .../liferay/portal/servlet/InitServlet.java | 2 ++ .../liferay/portal/servlet/MainServlet.java | 25 +++++++++----- dotCMS/src/main/webapp/WEB-INF/web.xml | 6 ++++ 7 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 dotCMS/src/main/java/com/dotmarketing/servlets/FinalStartupServlet.java diff --git a/dotCMS/src/main/java/com/dotcms/api/web/RequestThreadLocalListener.java b/dotCMS/src/main/java/com/dotcms/api/web/RequestThreadLocalListener.java index 6ad074f911c5..2e6dec81eb0f 100644 --- a/dotCMS/src/main/java/com/dotcms/api/web/RequestThreadLocalListener.java +++ b/dotCMS/src/main/java/com/dotcms/api/web/RequestThreadLocalListener.java @@ -1,5 +1,6 @@ package com.dotcms.api.web; +import javax.servlet.ServletContextEvent; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; @@ -13,9 +14,12 @@ public class RequestThreadLocalListener implements ServletRequestListener { public RequestThreadLocalListener() { - Logger.info(this.getClass(), "Starting RequestThreadLocalListener"); } + public void contextInitialized(ServletContextEvent sce) { + Logger.info(this.getClass(), "Starting RequestThreadLocalListener"); + } + public void requestDestroyed(ServletRequestEvent requestEvent) { HttpServletRequestThreadLocal.INSTANCE.setRequest(null); DotVelocitySecretAppConfigThreadLocal.INSTANCE.clearConfig(); diff --git a/dotCMS/src/main/java/com/dotmarketing/listeners/ContextLifecycleListener.java b/dotCMS/src/main/java/com/dotmarketing/listeners/ContextLifecycleListener.java index e3624c919066..a7ee350243b2 100644 --- a/dotCMS/src/main/java/com/dotmarketing/listeners/ContextLifecycleListener.java +++ b/dotCMS/src/main/java/com/dotmarketing/listeners/ContextLifecycleListener.java @@ -58,12 +58,13 @@ public void contextDestroyed(ServletContextEvent arg0) { } public void contextInitialized(ServletContextEvent arg0) { - + Logger.info(this,"ContextLifecycleListener contextInitialized called"); ByteBuddyFactory.init(); Config.setMyApp(arg0.getServletContext()); installWebSocket(arg0.getServletContext()); + Logger.info(this,"ContextLifecycleListener contextInitialized completed"); } private void installWebSocket(final ServletContext serverContext) { diff --git a/dotCMS/src/main/java/com/dotmarketing/servlets/FinalStartupServlet.java b/dotCMS/src/main/java/com/dotmarketing/servlets/FinalStartupServlet.java new file mode 100644 index 000000000000..33a9d14e17b6 --- /dev/null +++ b/dotCMS/src/main/java/com/dotmarketing/servlets/FinalStartupServlet.java @@ -0,0 +1,34 @@ +package com.dotmarketing.servlets; + +import com.dotmarketing.util.Config; +import com.dotmarketing.util.Logger; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +//import javax.servlet.annotation.WebServlet; + +/** + * This servlet is used to check if the server should be shutdown on startup. + * If the property shutdown-on-startup is set to true, the server will be shutdown + * The loadOnStartup is set to the highest value to ensure it is run after all other servlets that are not + * lazy loaded + */ +public class FinalStartupServlet extends HttpServlet { + + // init method + @Override + public void init(ServletConfig cfg) throws ServletException { + super.init(cfg); + Logger.info(this, "Starting FinalStartupServlet"); + if (Config.getBooleanProperty("shutdown-on-startup", false)) { + Logger.info(this, "shutdown-on-startup is true, shutting down the server"); + System.exit(0); + } + } + + @Override + public void destroy() { + Logger.info(this, "FinalStartupServlet destroyed"); + } + +} diff --git a/dotCMS/src/main/java/com/dotmarketing/servlets/InitServlet.java b/dotCMS/src/main/java/com/dotmarketing/servlets/InitServlet.java index 3b3362d2424a..3e192cc155d1 100644 --- a/dotCMS/src/main/java/com/dotmarketing/servlets/InitServlet.java +++ b/dotCMS/src/main/java/com/dotmarketing/servlets/InitServlet.java @@ -95,6 +95,7 @@ public void destroy() { * @throws DotDataException */ public void init(ServletConfig config) throws ServletException { + Logger.info(this,"InitServlet init Started"); startupDate = new java.util.Date(); new StartupLogger().log(); @@ -259,6 +260,8 @@ public void init(ServletConfig config) throws ServletException { Logger.warn(this.getClass(), "Unable to record startup time :" + e); } + Logger.info(this,"InitServlet init Completed"); + } protected void deleteFiles(java.io.File directory) { diff --git a/dotCMS/src/main/java/com/liferay/portal/servlet/InitServlet.java b/dotCMS/src/main/java/com/liferay/portal/servlet/InitServlet.java index 64562ad2e961..dcca1fff1154 100644 --- a/dotCMS/src/main/java/com/liferay/portal/servlet/InitServlet.java +++ b/dotCMS/src/main/java/com/liferay/portal/servlet/InitServlet.java @@ -45,6 +45,7 @@ public class InitServlet extends HttpServlet { public void init() throws ServletException { synchronized (InitServlet.class) { + Logger.info(this, "Portal InitServlet init method called"); // Initialize @@ -75,6 +76,7 @@ public void init() throws ServletException { Logger.error(this,e.getMessage(),e); } } + Logger.info(this, "Portal InitServlet init method completed"); } @Override diff --git a/dotCMS/src/main/java/com/liferay/portal/servlet/MainServlet.java b/dotCMS/src/main/java/com/liferay/portal/servlet/MainServlet.java index 41352da19e90..a6f7b6f5066f 100644 --- a/dotCMS/src/main/java/com/liferay/portal/servlet/MainServlet.java +++ b/dotCMS/src/main/java/com/liferay/portal/servlet/MainServlet.java @@ -92,9 +92,12 @@ */ public class MainServlet extends ActionServlet { + public static final String SKIP_UPGRADE = "skip-upgrade"; + @CloseDBIfOpened // Note if ByteBuddyFactory not initialized before this class this may not be wrapped public void init(ServletConfig config) throws ServletException { synchronized (MainServlet.class) { + Logger.info(this, "MainServlet init started"); ByteBuddyFactory.init(); super.init(config); Config.initializeConfig(); @@ -115,10 +118,11 @@ public void init(ServletConfig config) throws ServletException { try { // Checking for execute upgrades - StartupTasksExecutor.getInstance().executeStartUpTasks(); - StartupTasksExecutor.getInstance().executeSchemaUpgrades(); - StartupTasksExecutor.getInstance().executeBackportedTasks(); - + if (!Config.getBooleanProperty(SKIP_UPGRADE, false)) { + StartupTasksExecutor.getInstance().executeStartUpTasks(); + StartupTasksExecutor.getInstance().executeSchemaUpgrades(); + StartupTasksExecutor.getInstance().executeBackportedTasks(); + } final Task00030ClusterInitialize clusterInitializeTask = new Task00030ClusterInitialize(); if(clusterInitializeTask.forceRun()){ clusterInitializeTask.executeUpgrade(); @@ -207,13 +211,16 @@ public void init(ServletConfig config) throws ServletException { // Init other dotCMS services. DotInitializationService.getInstance().initialize(); - try { - // Now that everything is up we can check if we need to execute data upgrade tasks - StartupTasksExecutor.getInstance().executeDataUpgrades(); - } catch (Exception e) { - throw new DotRuntimeException("Error executing data upgrade tasks", e); + if (!Config.getBooleanProperty(SKIP_UPGRADE, false)) { + try { + // Now that everything is up we can check if we need to execute data upgrade tasks + StartupTasksExecutor.getInstance().executeDataUpgrades(); + } catch (Exception e) { + throw new DotRuntimeException("Error executing data upgrade tasks", e); + } } } + Logger.info(this, "MainServlet init completed"); } public void callParentService(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { diff --git a/dotCMS/src/main/webapp/WEB-INF/web.xml b/dotCMS/src/main/webapp/WEB-INF/web.xml index cac6f9bfd85a..429a4cec0376 100644 --- a/dotCMS/src/main/webapp/WEB-INF/web.xml +++ b/dotCMS/src/main/webapp/WEB-INF/web.xml @@ -247,6 +247,12 @@ com.dotmarketing.servlets.InitServlet 8 + + + FinalStartupServlet + com.dotmarketing.servlets.FinalStartupServlet + 99999 + VelocityServlet