use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.
the class WatchServiceTest method testRestartContainerAndCallPostGoalRestartEnabled.
@Test
public void testRestartContainerAndCallPostGoalRestartEnabled() throws Exception {
// Given
AtomicBoolean restarted = new AtomicBoolean(false);
WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.both).containerRestarter(// Override Restart task to set this value to true
i -> restarted.set(true)).build();
WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
// When
watchService.restartContainerAndCallPostGoal(imageWatcher, true);
// Then
assertTrue(restarted.get());
}
use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.
the class WatchService method watch.
public synchronized void watch(WatchContext context, JKubeConfiguration buildContext, List<ImageConfiguration> images) throws IOException {
// Important to be be a single threaded scheduler since watch jobs must run serialized
ScheduledExecutorService executor = null;
try {
executor = Executors.newSingleThreadScheduledExecutor();
for (ImageConfiguration imageConfig : runService.getImagesConfigsInOrder(queryService, images)) {
String imageId = queryService.getImageId(imageConfig.getName());
String containerId = runService.lookupContainer(imageConfig.getName());
ImageWatcher watcher = new ImageWatcher(imageConfig, context, imageId, containerId);
long interval = watcher.getInterval();
WatchMode watchMode = watcher.getWatchMode(imageConfig);
log.info("Watching %s %s", imageConfig.getName(), (watchMode != null ? " using " + watchMode.getDescription() : ""));
ArrayList<String> tasks = new ArrayList<>();
if (imageConfig.getBuildConfiguration() != null && imageConfig.getBuildConfiguration().getAssembly() != null) {
if (watcher.isCopy()) {
schedule(executor, createCopyWatchTask(watcher, context.getBuildContext()), interval);
tasks.add("copying artifacts");
}
if (watcher.isBuild()) {
schedule(executor, createBuildWatchTask(watcher, context.getBuildContext(), watchMode == WatchMode.both, buildContext), interval);
tasks.add("rebuilding");
}
}
if (watcher.isRun() && watcher.getContainerId() != null) {
schedule(executor, createRestartWatchTask(watcher), interval);
tasks.add("restarting");
}
if (!tasks.isEmpty()) {
log.info("%s: Watch for %s", imageConfig.getDescription(), String.join(" and ", tasks));
}
}
log.info("Waiting ...");
if (!context.isKeepRunning()) {
runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
}
wait();
} catch (InterruptedException e) {
log.warn("Interrupted");
Thread.currentThread().interrupt();
} finally {
if (executor != null) {
executor.shutdownNow();
}
}
}
use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.
the class WatchServiceTest method testCopyFilesToContainer.
@Test
public void testCopyFilesToContainer() throws Exception {
// Given
AtomicBoolean fileCopied = new AtomicBoolean(false);
WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.copy).containerCopyTask(f -> fileCopied.compareAndSet(false, true)).build();
File fileToCopy = Files.createTempFile("test-changed-files", "tar").toFile();
WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
// When
watchService.copyFilesToContainer(fileToCopy, imageWatcher);
// Then
assertTrue(fileCopied.get());
}
use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.
the class WatchServiceTest method testCallPostExec.
@Test
public void testCallPostExec() throws Exception {
// Given
AtomicBoolean postExecCommandExecuted = new AtomicBoolean(false);
WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.copy).containerCommandExecutor(imageWatcher -> {
postExecCommandExecuted.set(true);
return "Some Output";
}).build();
WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
// When
watchService.callPostExec(imageWatcher);
// Then
assertTrue(postExecCommandExecuted.get());
}
Aggregations