/*******************************************************************************
*
* Copyright (c) 2004-2009 Oracle Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
* Kohsuke Kawaguchi
*
*
*******************************************************************************/
package hudson.util;
import hudson.model.Hudson;
import org.eclipse.hudson.WebAppController;
import hudson.triggers.SafeTimerTask;
import hudson.triggers.Trigger;
import org.apache.commons.io.FileUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
/**
* Makes sure that no other Hudson uses our <tt>HUDSON_HOME</tt> directory, to
* forestall the problem of running multiple instances of Hudson that point to
* the same data directory.
*
* <p> This set up error occasionally happens especialy when the user is trying
* to reassign the context path of the app, and it results in a hard-to-diagnose
* error, so we actively check this.
*
* <p> The mechanism is simple. This class occasionally updates a known file
* inside the hudson home directory, and whenever it does so, it monitors the
* timestamp of the file to make sure no one else is updating this file. In this
* way, while we cannot detect the problem right away, within a reasonable time
* frame we can detect the collision.
*
* <p> More traditional way of doing this is to use a lock file with PID in it,
* but unfortunately in Java, there's no reliabe way to obtain PID.
*
* @author Kohsuke Kawaguchi
* @since 1.178
*/
public class DoubleLaunchChecker {
/**
* The timestamp of the owner file when we updated it for the last time. 0
* to indicate that there was no update before.
*/
private long lastWriteTime = 0L;
/**
* Once the error is reported, the user can choose to ignore and proceed
* anyway, in which case the flag is set to true.
*/
private boolean ignore = false;
private final Random random = new Random();
public final File home;
/**
* ID string of the other Hudson that we are colliding with. Can be null.
*/
private String collidingId;
public DoubleLaunchChecker() {
home = Hudson.getInstance().getRootDir();
}
protected void execute() {
File timestampFile = new File(home, ".owner");
long t = timestampFile.lastModified();
if (t != 0 && lastWriteTime != 0 && t != lastWriteTime && !ignore) {
try {
collidingId = FileUtils.readFileToString(timestampFile);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to read collision file", e);
}
// we noticed that someone else have updated this file.
// switch GUI to display this error.
WebAppController.get().install(this);
LOGGER.severe("Collision detected. timestamp=" + t + ", expected=" + lastWriteTime);
// we need to continue updating this file, so that the other Hudson would notice the problem, too.
}
try {
FileUtils.writeStringToFile(timestampFile, getId());
lastWriteTime = timestampFile.lastModified();
} catch (IOException e) {
// if failed to write, err on the safe side and assume things are OK.
lastWriteTime = 0;
}
schedule();
}
/**
* Figures out a string that identifies this instance of Hudson.
*/
public String getId() {
Hudson h = Hudson.getInstance();
// in servlet 2.5, we can get the context path
String contextPath = "";
try {
Method m = ServletContext.class.getMethod("getContextPath");
contextPath = " contextPath=\"" + m.invoke(h.servletContext) + "\"";
} catch (Exception e) {
// maybe running with Servlet 2.4
}
return h.hashCode() + contextPath + " at " + ManagementFactory.getRuntimeMXBean().getName();
}
public String getCollidingId() {
return collidingId;
}
/**
* Schedules the next execution.
*/
public void schedule() {
// randomize the scheduling so that multiple Hudson instances will write at the file at different time
long MINUTE = 1000 * 60;
Trigger.timer.schedule(new SafeTimerTask() {
protected void doRun() {
execute();
}
}, (random.nextInt(30) + 60) * MINUTE);
}
/**
* Serve all URLs with the index view.
*/
public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
rsp.setStatus(SC_INTERNAL_SERVER_ERROR);
req.getView(this, "index.jelly").forward(req, rsp);
}
/**
* Ignore the problem and go back to using Hudson.
*/
public void doIgnore(StaplerRequest req, StaplerResponse rsp) throws IOException {
ignore = true;
WebAppController.get().install(Hudson.getInstance());
rsp.sendRedirect2(req.getContextPath() + '/');
}
private static final Logger LOGGER = Logger.getLogger(DoubleLaunchChecker.class.getName());
}