IDEMPIERE-4211 Schedulers launched twice - problem with RunOnlyOnIP null running on load balancer (#345)

- log errors from background init.
- add explicit Env Context init for AdempiereMonitor thread and
Background init thread.
This commit is contained in:
hengsin 2020-11-02 18:23:24 +08:00 committed by GitHub
parent c9a766c806
commit 03d668bf51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 40 deletions

View File

@ -148,6 +148,11 @@ public class WebEnv
return true;
}
Properties ctx = new Properties();
Env.setContext(ctx, Env.AD_CLIENT_ID, 0);
Env.setContext(ctx, Env.AD_USER_ID, 0);
ServerContext.setCurrentInstance(ctx);
// Load Environment Variables (serverApps/src/web/WEB-INF/web.xml)
Enumeration<String> en = context.getInitParameterNames();
StringBuilder info = new StringBuilder("Servlet Context Init Parameters: ")
@ -179,18 +184,12 @@ public class WebEnv
// Logging now initiated
if (log.isLoggable(Level.INFO)) log.info(info.toString());
//
Properties ctx = new Properties();
try {
ServerContext.setCurrentInstance(ctx);
MClient client = MClient.get(Env.getCtx(), 0);
MSystem system = MSystem.get(Env.getCtx());
client.sendEMail(client.getRequestEMail(),
"Server started: " + system.getName() + " (" + WebUtil.getServerName() + ")",
"ServerInfo: " + context.getServerInfo(), null);
} finally {
ServerContext.dispose();
}
//
MClient client = MClient.get(Env.getCtx(), 0);
MSystem system = MSystem.get(Env.getCtx());
client.sendEMail(client.getRequestEMail(),
"Server started: " + system.getName() + " (" + WebUtil.getServerName() + ")",
"ServerInfo: " + context.getServerInfo(), null);
return s_initOK;
} // initWeb

View File

@ -1282,46 +1282,62 @@ public class AdempiereMonitor extends HttpServlet
// initial Wait (default to 10 seconds) to give cluster service time to start first
final int initialWaitSeconds = MSysConfig.getIntValue(MSysConfig.MONITOR_INITIAL_WAIT_FOR_CLUSTER_IN_SECONDS, 10);
serverMgrFuture = Adempiere.getThreadPoolExecutor().schedule(() -> {
int maxSecondsToWait = MSysConfig.getIntValue(MSysConfig.MONITOR_MAX_WAIT_FOR_CLUSTER_IN_SECONDS, 180);
int totalWaitSeconds = initialWaitSeconds;
//check every 5 seconds (until maxSecondsToWait)
int waitSeconds = 5;
while (ClusterServerMgr.getClusterService() == null)
{
try {
Thread.sleep(waitSeconds * 1000);
} catch (InterruptedException e) {
break;
serverMgrFuture = Adempiere.getThreadPoolExecutor().schedule(() -> {
try {
Properties ctx = new Properties();
Env.setContext(ctx, Env.AD_CLIENT_ID, 0);
Env.setContext(ctx, Env.AD_USER_ID, 0);
ServerContext.setCurrentInstance(ctx);
int maxSecondsToWait = MSysConfig.getIntValue(MSysConfig.MONITOR_MAX_WAIT_FOR_CLUSTER_IN_SECONDS, 180);
int totalWaitSeconds = initialWaitSeconds;
//check every 5 seconds (until maxSecondsToWait)
int waitSeconds = 5;
while (ClusterServerMgr.getClusterService() == null)
{
try {
Thread.sleep(waitSeconds * 1000);
} catch (InterruptedException e) {
break;
}
if (Thread.interrupted())
break;
totalWaitSeconds += waitSeconds;
if (totalWaitSeconds >= maxSecondsToWait) {
log.warning("Cluster Service did not start after " + totalWaitSeconds + " seconds");
break;
}
}
if (Thread.interrupted())
break;
totalWaitSeconds += waitSeconds;
if (totalWaitSeconds >= maxSecondsToWait) {
log.warning("Cluster Service did not start after " + totalWaitSeconds + " seconds");
break;
//always create the local server manager instance
m_serverMgr = AdempiereServerMgr.get();
//switch to cluster manager if cluster service is available
if (ClusterServerMgr.getClusterService() != null)
m_serverMgr = ClusterServerMgr.getInstance();
} catch (Throwable e) {
if (e.getCause() != null) {
log.log(Level.SEVERE, e.getCause().getMessage(), e.getCause());
} else {
log.log(Level.SEVERE, e.getMessage(), e);
}
} finally {
ServerContext.dispose();
}
//always create the local server manager instance
m_serverMgr = AdempiereServerMgr.get();
//switch to cluster manager if cluster service is available
if (ClusterServerMgr.getClusterService() != null)
m_serverMgr = ClusterServerMgr.getInstance();
}, initialWaitSeconds, TimeUnit.SECONDS);
m_dirAccessList = getDirAcessList();
} // init
private IServerManager getServerManager()
private synchronized IServerManager getServerManager()
{
if (!serverMgrFuture.isDone() && !serverMgrFuture.isCancelled())
if (serverMgrFuture != null && !serverMgrFuture.isDone() && !serverMgrFuture.isCancelled())
{
try {
serverMgrFuture.get();
} catch (Exception e) {}
}
} catch (Exception e) {
}
}
return m_serverMgr;
}