io.github.sinri.keel.maids.watchman.KeelCronWatchman Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Keel Show documentation
Show all versions of Keel Show documentation
A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.
The newest version!
package io.github.sinri.keel.maids.watchman;
import io.github.sinri.keel.core.KeelCronExpression;
import io.github.sinri.keel.facade.async.KeelAsyncKit;
import io.github.sinri.keel.logger.event.KeelEventLogger;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.ThreadingModel;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.AsyncMap;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import static io.github.sinri.keel.facade.KeelInstance.Keel;
/**
* It is designed as KeelSundial, to perform crontab in cluster.
*
* @since 2.9.3
*/
public class KeelCronWatchman extends KeelWatchmanImpl {
private final KeelWatchmanEventHandler handler;
private final Function> cronTabUpdateStartup;
protected KeelCronWatchman(String watchmanName, Function> cronTabUpdateStartup) {
super(watchmanName);
this.handler = now -> {
Calendar calendar = new Calendar
.Builder()
.setInstant(now)
.build();
readAsyncMapForEventHandlers(calendar)
.onSuccess(list -> list.forEach(x -> x.handle(now)))
.onFailure(throwable -> getLogger().exception(throwable, r -> {
}));
};
this.cronTabUpdateStartup = cronTabUpdateStartup;
}
public static Future deploy(String watchmanName, Function> cronTabUpdateStartup) {
KeelCronWatchman keelCronWatchman = new KeelCronWatchman(watchmanName, cronTabUpdateStartup);
return Keel.getVertx().deployVerticle(keelCronWatchman, new DeploymentOptions()
.setThreadingModel(ThreadingModel.WORKER));
}
private static Future operateCronTab(String asyncMapName, Supplier> supplier) {
return Keel.getVertx().sharedData().getLock(asyncMapName)
.compose(lock -> supplier.get()
.andThen(ar -> lock.release()));
}
public static Future addCronJobToAsyncMap(
String asyncMapName,
KeelCronExpression keelCronExpression,
Class extends KeelWatchmanEventHandler> eventHandlerClass
) {
return addCronJobToAsyncMap(asyncMapName, keelCronExpression.getRawCronExpression(), eventHandlerClass.getName());
}
public static Future addCronJobToAsyncMap(
String asyncMapName,
String keelCronExpression,
String eventHandlerClassName
) {
return operateCronTab(
asyncMapName,
() -> Keel.getVertx().sharedData().getAsyncMap(asyncMapName)
.compose(asyncMap -> asyncMap.put(
keelCronExpression + "@" + eventHandlerClassName,
new JsonObject()
.put("cron", keelCronExpression)
.put("handler", eventHandlerClassName)
))
);
}
public static Future replaceAllCronJobToAsyncMap(String asyncMapName, Map> newMap) {
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy