if (!commandLineArguments.isNoPidFile()) {
savePidFile(commandLineArguments.getPidFile());
}
// Le server object. This is where all the magic happens.
final ServerStatus serverStatus = injector.getInstance(ServerStatus.class);
serverStatus.initialize();
ActivityWriter activityWriter = null;
ServiceManager serviceManager = null;
try {
activityWriter = injector.getInstance(ActivityWriter.class);
serviceManager = injector.getInstance(ServiceManager.class);
} catch (ProvisionException e) {
for (Message message : e.getErrorMessages()) {
if (message.getCause() instanceof MongoException) {
LOG.error(UI.wallString("Unable to connect to MongoDB. Is it running and the configuration correct?"));
System.exit(-1);
}
}
LOG.error("Guice error", e);
System.exit(-1);
} catch (Exception e) {
LOG.error("Unexpected exception", e);
System.exit(-1);
}
final ActivityWriter finalActivityWriter = activityWriter;
final ServiceManager finalServiceManager = serviceManager;
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
String msg = "SIGNAL received. Shutting down.";
LOG.info(msg);
finalActivityWriter.write(new Activity(msg, Main.class));
GracefulShutdown shutdown = injector.getInstance(GracefulShutdown.class);
shutdown.runWithoutExit();
finalServiceManager.stopAsync().awaitStopped();
}
});
// Register this node.
final NodeService nodeService = injector.getInstance(NodeService.class);
nodeService.registerServer(serverStatus.getNodeId().toString(), configuration.isMaster(), configuration.getRestTransportUri());
if (configuration.isMaster() && !nodeService.isOnlyMaster(serverStatus.getNodeId())) {
LOG.warn("Detected another master in the cluster. Retrying in {} seconds to make sure it is not "
+ "an old stale instance.", configuration.getStaleMasterTimeout());
try {
Thread.sleep(configuration.getStaleMasterTimeout());
} catch (InterruptedException e) { /* nope */ }
if (!nodeService.isOnlyMaster(serverStatus.getNodeId())) {
// All devils here.
String what = "Detected other master node in the cluster! Starting as non-master! "
+ "This is a mis-configuration you should fix.";
LOG.warn(what);
activityWriter.write(new Activity(what, Main.class));
// Write a notification.
final NotificationService notificationService = injector.getInstance(NotificationService.class);
Notification notification = notificationService.buildNow()
.addType(Notification.Type.MULTI_MASTER)
.addSeverity(Notification.Severity.URGENT);
notificationService.publishIfFirst(notification);
configuration.setIsMaster(false);
} else {
LOG.warn("Stale master has gone. Starting as master.");
}
}
// Enable local mode?
if (commandLineArguments.isLocal() || commandLineArguments.isDebug()) {
// In local mode, systemstats are sent to localhost for example.
LOG.info("Running in local mode");
serverStatus.setLocalMode(true);
}
// Are we in stats mode?
if (commandLineArguments.isStats()) {
LOG.info("Printing system utilization information.");
serverStatus.setStatsMode(true);
}
if (!commandLineArguments.performRetention()) {
configuration.setPerformRetention(false);