forked from I2P_Developers/i2p.i2p
allow the timestamper to be controlled by env properties (and, in turn, safe to always run)
if/when the property "timestamper.enabled" is set, the timestamper will query the sntp server(s) and update the clock accordingly if/when it is not set (or set to something other than "true"), it will pause with its standard delay before checking again in addition, it has a guard to help running the timestamper multiple times in the same JVM
This commit is contained in:
@@ -23,21 +23,25 @@ import net.i2p.util.Log;
|
|||||||
*/
|
*/
|
||||||
public class Timestamper implements Runnable {
|
public class Timestamper implements Runnable {
|
||||||
private static Log _log = new Log(Timestamper.class);
|
private static Log _log = new Log(Timestamper.class);
|
||||||
private String _targetURL;
|
private static String _targetURL;
|
||||||
private String _serverList[];
|
private static String _serverList[];
|
||||||
|
|
||||||
private int DELAY_MS = 5*60*1000;
|
private int DELAY_MS = 5*60*1000;
|
||||||
|
|
||||||
public Timestamper(String url, String serverNames[]) {
|
public Timestamper() {}
|
||||||
if (_log.shouldLog(Log.INFO))
|
|
||||||
_log.info("Creating new timestamper pointing at " + url);
|
|
||||||
_targetURL = url;
|
|
||||||
_serverList = serverNames;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startTimestamper() {
|
public void startTimestamper() {
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO))
|
||||||
_log.info("Starting timestamper pointing at " + _targetURL);
|
_log.info("Starting timestamper pointing at " + _targetURL);
|
||||||
|
synchronized (Timestamper.class) {
|
||||||
|
String enabled = System.getProperty("timestamper.enabled");
|
||||||
|
if (enabled != null) {
|
||||||
|
_log.warn("Timestamper already running");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.setProperty("timestamper.enabled", "true");
|
||||||
|
}
|
||||||
|
}
|
||||||
I2PThread t = new I2PThread(this, "Timestamper");
|
I2PThread t = new I2PThread(this, "Timestamper");
|
||||||
t.setPriority(I2PThread.MIN_PRIORITY);
|
t.setPriority(I2PThread.MIN_PRIORITY);
|
||||||
t.start();
|
t.start();
|
||||||
@@ -48,15 +52,21 @@ public class Timestamper implements Runnable {
|
|||||||
_log.info("Starting up timestamper");
|
_log.info("Starting up timestamper");
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
String enabled = System.getProperty("timestamper.enabled");
|
||||||
_log.debug("Querying servers " + _serverList);
|
if ( (enabled == null) || (!"true".equals(enabled)) ) {
|
||||||
try {
|
|
||||||
long now = NtpClient.currentTime(_serverList);
|
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("Stamp time");
|
_log.debug("Not stamping the time");
|
||||||
stampTime(now);
|
} else {
|
||||||
} catch (IllegalArgumentException iae) {
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.log(Log.CRIT, "Unable to reach any of the NTP servers - network disconnected?");
|
_log.debug("Querying servers " + _serverList);
|
||||||
|
try {
|
||||||
|
long now = NtpClient.currentTime(_serverList);
|
||||||
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
|
_log.debug("Stamp time");
|
||||||
|
stampTime(now);
|
||||||
|
} catch (IllegalArgumentException iae) {
|
||||||
|
_log.log(Log.CRIT, "Unable to reach any of the NTP servers - network disconnected?");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try { Thread.sleep(DELAY_MS); } catch (InterruptedException ie) {}
|
try { Thread.sleep(DELAY_MS); } catch (InterruptedException ie) {}
|
||||||
}
|
}
|
||||||
@@ -98,7 +108,9 @@ public class Timestamper implements Runnable {
|
|||||||
}
|
}
|
||||||
String servers[] = new String[args.length-1];
|
String servers[] = new String[args.length-1];
|
||||||
System.arraycopy(args, 1, servers, 0, servers.length);
|
System.arraycopy(args, 1, servers, 0, servers.length);
|
||||||
Timestamper ts = new Timestamper(args[0], servers);
|
_targetURL = args[0];
|
||||||
|
_serverList = servers;
|
||||||
|
Timestamper ts = new Timestamper();
|
||||||
ts.startTimestamper();
|
ts.startTimestamper();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user