Search in sources :

Example 1 with TaskEngine

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);
}
Also used : DummyTaskEngine(org.infinispan.tasks.DummyTaskEngine) TaskEngine(org.infinispan.tasks.spi.TaskEngine) TaskManager(org.infinispan.tasks.TaskManager) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) DummyTaskEngine(org.infinispan.tasks.DummyTaskEngine)

Example 2 with 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());
        });
    });
}
Also used : TaskEngine(org.infinispan.tasks.spi.TaskEngine) NonBlockingTaskEngine(org.infinispan.tasks.spi.NonBlockingTaskEngine) NonBlockingTaskEngine(org.infinispan.tasks.spi.NonBlockingTaskEngine) Address(org.infinispan.remoting.transport.Address) EventLogger(org.infinispan.util.logging.events.EventLogger) Subject(javax.security.auth.Subject)

Aggregations

TaskEngine (org.infinispan.tasks.spi.TaskEngine)2 Subject (javax.security.auth.Subject)1 GlobalComponentRegistry (org.infinispan.factories.GlobalComponentRegistry)1 Address (org.infinispan.remoting.transport.Address)1 DummyTaskEngine (org.infinispan.tasks.DummyTaskEngine)1 TaskManager (org.infinispan.tasks.TaskManager)1 NonBlockingTaskEngine (org.infinispan.tasks.spi.NonBlockingTaskEngine)1 EventLogger (org.infinispan.util.logging.events.EventLogger)1