Search in sources :

Example 1 with WatchMode

use of io.fabric8.maven.docker.config.WatchMode in project docker-maven-plugin by fabric8io.

the class WatchService method watch.

public synchronized void watch(WatchContext context, BuildService.BuildContext buildContext, List<ImageConfiguration> images) throws DockerAccessException, MojoExecutionException {
    // Important to be be a single threaded scheduler since watch jobs must run serialized
    ScheduledExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadScheduledExecutor();
        for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, images)) {
            final ImageConfiguration imageConfig = (ImageConfiguration) resolvable;
            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 " + imageConfig.getName() + (watchMode != null ? " using " + watchMode.getDescription() : ""));
            ArrayList<String> tasks = new ArrayList<>();
            if (imageConfig.getBuildConfiguration() != null && imageConfig.getBuildConfiguration().getAssemblyConfiguration() != null) {
                if (watcher.isCopy()) {
                    String containerBaseDir = imageConfig.getBuildConfiguration().getAssemblyConfiguration().getTargetDir();
                    schedule(executor, createCopyWatchTask(watcher, context.getMojoParameters(), containerBaseDir), interval);
                    tasks.add("copying artifacts");
                }
                if (watcher.isBuild()) {
                    schedule(executor, createBuildWatchTask(watcher, context.getMojoParameters(), watchMode == WatchMode.both, buildContext), interval);
                    tasks.add("rebuilding");
                }
            }
            if (watcher.isRun() && watcher.getContainerId() != null) {
                schedule(executor, createRestartWatchTask(watcher), interval);
                tasks.add("restarting");
            }
            if (tasks.size() > 0) {
                log.info("%s: Watch for %s", imageConfig.getDescription(), StringUtils.join(tasks.toArray(), " and "));
            }
        }
        log.info("Waiting ...");
        if (!context.isKeepRunning()) {
            runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
        }
        wait();
    } catch (InterruptedException e) {
        log.warn("Interrupted");
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StartOrderResolver(io.fabric8.maven.docker.util.StartOrderResolver) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) WatchImageConfiguration(io.fabric8.maven.docker.config.WatchImageConfiguration) WatchMode(io.fabric8.maven.docker.config.WatchMode) ArrayList(java.util.ArrayList)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1 WatchImageConfiguration (io.fabric8.maven.docker.config.WatchImageConfiguration)1 WatchMode (io.fabric8.maven.docker.config.WatchMode)1 StartOrderResolver (io.fabric8.maven.docker.util.StartOrderResolver)1 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1