use of org.infinispan.tasks.spi.TaskEngine in project infinispan by infinispan.
the class TasksResourceTest method defineCaches.
@Override
protected void defineCaches(EmbeddedCacheManager cm) {
cm.defineConfiguration("default", getDefaultCacheBuilder().build());
GlobalComponentRegistry gcr = cm.getGlobalComponentRegistry();
TaskManager taskManager = gcr.getComponent(TaskManager.class);
TaskEngine taskEngine = new DummyTaskEngine();
taskManager.registerTaskEngine(taskEngine);
}
use of org.infinispan.tasks.spi.TaskEngine in project infinispan by infinispan.
the class TaskManagerImpl method runTask.
@Override
public <T> CompletionStage<T> runTask(String name, TaskContext context) {
// This finds an engine that can accept the task
CompletionStage<TaskEngine> engineStage = Flowable.fromIterable(engines).concatMapMaybe(engine -> {
if (engine instanceof NonBlockingTaskEngine) {
return Maybe.fromCompletionStage(((NonBlockingTaskEngine) engine).handlesAsync(name)).concatMap(canHandle -> canHandle ? Maybe.just(engine) : Maybe.empty());
}
return engine.handles(name) ? Maybe.just(engine) : Maybe.empty();
}).firstElement().toCompletionStage(null);
// Performs the actual task if an engine was found
return engineStage.thenCompose(engine -> {
if (engine == null) {
throw log.unknownTask(name);
}
context.cacheManager(cacheManager);
Address address = cacheManager.getAddress();
Subject subject = context.getSubject().orElseGet(() -> {
if (useSecurity) {
return Security.getSubject();
} else {
return null;
}
});
Optional<String> who = Optional.ofNullable(subject == null ? null : Security.getSubjectUserPrincipal(subject).getName());
TaskExecutionImpl exec = new TaskExecutionImpl(name, address == null ? "local" : address.toString(), who, context);
exec.setStart(timeService.instant());
runningTasks.put(exec.getUUID(), exec);
CompletionStage<T> task = engine.runTask(name, context, blockingManager);
return task.whenComplete((r, e) -> {
if (context.isLogEvent()) {
EventLogger eventLog = eventLogManager.getEventLogger().scope(cacheManager.getAddress());
who.ifPresent(eventLog::who);
context.getCache().ifPresent(eventLog::context);
if (e != null) {
eventLog.detail(e).error(EventLogCategory.TASKS, MESSAGES.taskFailure(name));
} else {
eventLog.detail(String.valueOf(r)).info(EventLogCategory.TASKS, MESSAGES.taskSuccess(name));
}
}
runningTasks.remove(exec.getUUID());
});
});
}
Aggregations