Only run if you're the alpha
This project uses Zookeeper to ensure that only one process can run a task at a given time. It uses a Curator InterProcessSemaphoreMutex to ensure unicity.
running is as simple as this :
try (AlphaDog alphaDog = new AlphaDog("zoo.keep.er:2181")) {
// this will run only if alpha
alphaDog.runIfAlpha(() -> {System.err.println("Alpha wanabee");}, "lockName");
}
The last time a particular lock was acquired is saved, so you can optionnally provide a Duration to ensure a task will not run too often.
try (AlphaDog alphaDog = new AlphaDog("zoo.keep.er:2181")) {
// this will run only if alpha and if last run was at least 1 hour ago
alphaDog.runIfAlpha(() -> {System.err.println("Alpha wanabee");}, "lockName", Duration.of(1L, ChronoUnit.HOURS));
}