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));
}
};
}
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();
}
};
}
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);
}
};
}
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;
}
}
};
}
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);
}
}
};
}
Aggregations