Search in sources :

Example 1 with DevConsolePostHandler

use of io.quarkus.devconsole.runtime.spi.DevConsolePostHandler in project quarkus by quarkusio.

the class HibernateSearchDevConsoleRecorder method indexEntity.

public Handler<RoutingContext> indexEntity() {
    return new DevConsolePostHandler() {

        @Override
        protected void handlePostAsync(RoutingContext event, MultiMap form) throws Exception {
            if (form.isEmpty()) {
                return;
            }
            SearchMapping mapping = HibernateSearchSupplier.searchMapping();
            if (mapping == null) {
                flashMessage(event, "There are no indexed entity types.", FlashScopeUtil.FlashMessageStatus.ERROR);
                return;
            }
            mapping.scope(Object.class, mapping.allIndexedEntities().stream().map(SearchIndexedEntity::jpaName).filter(form::contains).collect(Collectors.toList())).massIndexer().startAndWait();
            flashMessage(event, "Entities successfully reindexed", Duration.ofSeconds(10));
        }
    };
}
Also used : DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) SearchMapping(org.hibernate.search.mapper.orm.mapping.SearchMapping)

Example 2 with DevConsolePostHandler

use of io.quarkus.devconsole.runtime.spi.DevConsolePostHandler in project quarkus by quarkusio.

the class CacheDevConsoleRecorder method clearCacheHandler.

public Handler<RoutingContext> clearCacheHandler() {
    return new DevConsolePostHandler() {

        @Override
        protected void handlePost(RoutingContext event, MultiMap form) {
            String cacheName = form.get("name");
            Optional<Cache> cache = CaffeineCacheSupplier.cacheManager().getCache(cacheName);
            if (cache.isPresent() && cache.get() instanceof CaffeineCache) {
                CaffeineCacheImpl caffeineCache = (CaffeineCacheImpl) cache.get();
                String action = form.get("action");
                if (action.equalsIgnoreCase("clearCache")) {
                    caffeineCache.invalidateAll().subscribe().with(ignored -> {
                        endResponse(event, OK, createResponseMessage(caffeineCache));
                    });
                } else if (action.equalsIgnoreCase("refresh")) {
                    endResponse(event, OK, createResponseMessage(caffeineCache));
                } else {
                    String errorMessage = "Invalid action: " + action;
                    endResponse(event, INTERNAL_SERVER_ERROR, createResponseError(cacheName, errorMessage));
                }
            } else {
                String errorMessage = "Cache for " + cacheName + " not found";
                endResponse(event, NOT_FOUND, createResponseError(cacheName, errorMessage));
            }
        }

        private void endResponse(RoutingContext event, HttpResponseStatus status, String message) {
            event.response().setStatusCode(status.code());
            event.response().end(message);
        }

        @Override
        protected void actionSuccess(RoutingContext event) {
        }

        private String createResponseMessage(CaffeineCacheImpl cache) {
            Json.JsonObjectBuilder object = Json.object();
            object.put("name", cache.getName());
            object.put("size", cache.getSize());
            return object.build();
        }

        private String createResponseError(String name, String error) {
            Json.JsonObjectBuilder object = Json.object();
            object.put("name", name);
            object.put("error", error);
            return object.build();
        }
    };
}
Also used : DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) CaffeineCache(io.quarkus.cache.CaffeineCache) CaffeineCacheImpl(io.quarkus.cache.runtime.caffeine.CaffeineCacheImpl) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Json(io.quarkus.vertx.http.runtime.devmode.Json) Cache(io.quarkus.cache.Cache) CaffeineCache(io.quarkus.cache.CaffeineCache)

Example 3 with DevConsolePostHandler

use of io.quarkus.devconsole.runtime.spi.DevConsolePostHandler in project quarkus by quarkusio.

the class FlywayDevConsoleRecorder method handler.

public Handler<RoutingContext> handler() {
    return new DevConsolePostHandler() {

        @Override
        protected void handlePost(RoutingContext event, MultiMap form) throws Exception {
            String datasource = form.get("datasource");
            String operation = form.get("operation");
            Collection<FlywayContainer> flywayContainers = new FlywayContainersSupplier().get();
            for (FlywayContainer flywayContainer : flywayContainers) {
                if (flywayContainer.getDataSourceName().equals(datasource)) {
                    Flyway flyway = flywayContainer.getFlyway();
                    if ("clean".equals(operation)) {
                        flyway.clean();
                        flashMessage(event, "Database cleaned");
                        return;
                    } else if ("migrate".equals(operation)) {
                        flyway.migrate();
                        flashMessage(event, "Database migrated");
                        return;
                    } else {
                        flashMessage(event, "Invalid operation: " + operation, FlashMessageStatus.ERROR);
                        return;
                    }
                }
            }
            flashMessage(event, "Datasource not found: " + datasource, FlashMessageStatus.ERROR);
        }
    };
}
Also used : DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) MultiMap(io.vertx.core.MultiMap) Flyway(org.flywaydb.core.Flyway) RoutingContext(io.vertx.ext.web.RoutingContext) FlywayContainer(io.quarkus.flyway.runtime.FlywayContainer) FlywayContainersSupplier(io.quarkus.flyway.runtime.FlywayContainersSupplier)

Example 4 with DevConsolePostHandler

use of io.quarkus.devconsole.runtime.spi.DevConsolePostHandler in project quarkus by quarkusio.

the class LiquibaseDevConsoleRecorder method handler.

public Handler<RoutingContext> handler() {
    return new DevConsolePostHandler() {

        @Override
        protected void handlePost(RoutingContext event, MultiMap form) throws Exception {
            String datasource = form.get("datasource");
            String operation = form.get("operation");
            InjectableInstance<LiquibaseFactory> liquibaseFactoryInstance = Arc.container().select(LiquibaseFactory.class);
            if (liquibaseFactoryInstance.isUnsatisfied()) {
                return;
            }
            Annotation qualifier;
            if (DataSourceUtil.isDefault(datasource)) {
                qualifier = Default.Literal.INSTANCE;
            } else {
                qualifier = LiquibaseDataSourceLiteral.of(datasource);
            }
            InstanceHandle<LiquibaseFactory> liquibaseFactoryHandle = Arc.container().instance(LiquibaseFactory.class, qualifier);
            if (!liquibaseFactoryHandle.isAvailable()) {
                flashMessage(event, "Unknown datasource: " + datasource, FlashMessageStatus.ERROR);
            }
            LiquibaseFactory liquibaseFactory = liquibaseFactoryHandle.get();
            if ("clean".equals(operation)) {
                try (Liquibase liquibase = liquibaseFactory.createLiquibase()) {
                    liquibase.dropAll();
                }
                flashMessage(event, "Datasource " + datasource + " cleaned");
                return;
            } else if ("migrate".equals(operation)) {
                try (Liquibase liquibase = liquibaseFactory.createLiquibase()) {
                    liquibase.update(liquibaseFactory.createContexts(), liquibaseFactory.createLabels());
                }
                flashMessage(event, "Datasource " + datasource + " migrated");
                return;
            } else {
                flashMessage(event, "Invalid operation: " + operation, FlashMessageStatus.ERROR);
                return;
            }
        }
    };
}
Also used : DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) Liquibase(liquibase.Liquibase) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) LiquibaseFactory(io.quarkus.liquibase.LiquibaseFactory) Annotation(java.lang.annotation.Annotation)

Example 5 with DevConsolePostHandler

use of io.quarkus.devconsole.runtime.spi.DevConsolePostHandler in project quarkus by quarkusio.

the class SchedulerDevConsoleRecorder method invokeHandler.

public Handler<RoutingContext> invokeHandler() {
    // the usual issue of Vert.x hanging on to the first TCCL and setting it on all its threads
    final ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
    return new DevConsolePostHandler() {

        @Override
        protected void handlePost(RoutingContext ctx, MultiMap form) throws Exception {
            String action = form.get("action");
            if ("pause".equals(action)) {
                Scheduler scheduler = Arc.container().instance(Scheduler.class).get();
                if (scheduler.isRunning()) {
                    scheduler.pause();
                    LOG.info("Scheduler paused via Dev UI");
                    flashMessage(ctx, "Scheduler paused");
                }
            } else if ("resume".equals(action)) {
                Scheduler scheduler = Arc.container().instance(Scheduler.class).get();
                if (!scheduler.isRunning()) {
                    scheduler.resume();
                    LOG.info("Scheduler resumed via Dev UI");
                    flashMessage(ctx, "Scheduler resumed");
                }
            } else if ("pauseJob".equals(action)) {
                Scheduler scheduler = Arc.container().instance(Scheduler.class).get();
                String identity = form.get("identity");
                if (identity != null && !scheduler.isPaused(identity)) {
                    scheduler.pause(identity);
                    LOG.infof("Scheduler paused job with identity '%s' via Dev UI", identity);
                    flashMessage(ctx, "Job with identity " + identity + " paused");
                }
            } else if ("resumeJob".equals(action)) {
                Scheduler scheduler = Arc.container().instance(Scheduler.class).get();
                String identity = form.get("identity");
                if (identity != null && scheduler.isPaused(identity)) {
                    scheduler.resume(identity);
                    LOG.infof("Scheduler resumed job with identity '%s'via Dev UI", identity);
                    flashMessage(ctx, "Job with identity " + identity + " resumed");
                }
            } else {
                String name = form.get("name");
                SchedulerContext context = Arc.container().instance(SchedulerContext.class).get();
                for (ScheduledMethodMetadata metadata : context.getScheduledMethods()) {
                    if (metadata.getMethodDescription().equals(name)) {
                        context.getExecutor().execute(new Runnable() {

                            @Override
                            public void run() {
                                final ClassLoader previousCl = Thread.currentThread().getContextClassLoader();
                                try {
                                    Thread.currentThread().setContextClassLoader(currentCl);
                                    ScheduledInvoker invoker = context.createInvoker(metadata.getInvokerClassName());
                                    if (invoker.isBlocking()) {
                                        invoker.invoke(new DevModeScheduledExecution());
                                    } else {
                                        Vertx vertx = Arc.container().instance(Vertx.class).get();
                                        VertxContext.getOrCreateDuplicatedContext(vertx).runOnContext(new Handler<Void>() {

                                            @Override
                                            public void handle(Void event) {
                                                try {
                                                    invoker.invoke(new DevModeScheduledExecution());
                                                } catch (Exception ignored) {
                                                }
                                            }
                                        });
                                    }
                                    LOG.infof("Invoked scheduled method %s via Dev UI", name);
                                } catch (Exception e) {
                                    LOG.error("Unable to invoke a @Scheduled method: " + metadata.getMethodDescription(), e);
                                } finally {
                                    Thread.currentThread().setContextClassLoader(previousCl);
                                }
                            }
                        });
                        flashMessage(ctx, "Action invoked");
                        return;
                    }
                }
                flashMessage(ctx, "Action not found: " + name, FlashMessageStatus.ERROR);
            }
        }
    };
}
Also used : ScheduledMethodMetadata(io.quarkus.scheduler.common.runtime.ScheduledMethodMetadata) Scheduler(io.quarkus.scheduler.Scheduler) Vertx(io.vertx.core.Vertx) DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) ScheduledInvoker(io.quarkus.scheduler.common.runtime.ScheduledInvoker) SchedulerContext(io.quarkus.scheduler.common.runtime.SchedulerContext)

Aggregations

DevConsolePostHandler (io.quarkus.devconsole.runtime.spi.DevConsolePostHandler)6 MultiMap (io.vertx.core.MultiMap)6 RoutingContext (io.vertx.ext.web.RoutingContext)6 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Cache (io.quarkus.cache.Cache)1 CaffeineCache (io.quarkus.cache.CaffeineCache)1 CaffeineCacheImpl (io.quarkus.cache.runtime.caffeine.CaffeineCacheImpl)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 Record (io.quarkus.deployment.annotations.Record)1 ConfigDescriptionBuildItem (io.quarkus.deployment.builditem.ConfigDescriptionBuildItem)1 ConsoleCommandBuildItem (io.quarkus.deployment.builditem.ConsoleCommandBuildItem)1 DevConsoleRouteBuildItem (io.quarkus.devconsole.spi.DevConsoleRouteBuildItem)1 DevConsoleRuntimeTemplateInfoBuildItem (io.quarkus.devconsole.spi.DevConsoleRuntimeTemplateInfoBuildItem)1 FlywayContainer (io.quarkus.flyway.runtime.FlywayContainer)1 FlywayContainersSupplier (io.quarkus.flyway.runtime.FlywayContainersSupplier)1 LiquibaseFactory (io.quarkus.liquibase.LiquibaseFactory)1 Scheduler (io.quarkus.scheduler.Scheduler)1 ScheduledInvoker (io.quarkus.scheduler.common.runtime.ScheduledInvoker)1 ScheduledMethodMetadata (io.quarkus.scheduler.common.runtime.ScheduledMethodMetadata)1 SchedulerContext (io.quarkus.scheduler.common.runtime.SchedulerContext)1