/* * Copyright (C) 2015 SoftIndex LLC. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.datakernel.cube.service; import io.datakernel.async.CompletionCallback; import io.datakernel.cube.Cube; import io.datakernel.eventloop.Eventloop; import io.datakernel.eventloop.EventloopService; import io.datakernel.eventloop.ScheduledRunnable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class CubeAutoReloadingChunks implements EventloopService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Cube cube; private final Eventloop eventloop; private final long refreshPeriodMillis; private ScheduledRunnable scheduledRefreshChunksTask; private final Runnable refreshChunkTask = createRefreshChunksTask(); private CubeAutoReloadingChunks(Cube cube, Eventloop eventloop, long refreshPeriodMillis) { this.cube = cube; this.eventloop = eventloop; this.refreshPeriodMillis = refreshPeriodMillis; } public static CubeAutoReloadingChunks create(Cube cube, Eventloop eventloop, long refreshPeriodMillis) { return new CubeAutoReloadingChunks(cube, eventloop, refreshPeriodMillis); } @Override public Eventloop getEventloop() { return eventloop; } private void scheduleChunksRefresh() { if (scheduledRefreshChunksTask != null && scheduledRefreshChunksTask.isCancelled()) return; scheduledRefreshChunksTask = eventloop.scheduleBackground(eventloop.currentTimeMillis() + refreshPeriodMillis, refreshChunkTask); } private Runnable createRefreshChunksTask() { return new Runnable() { @Override public void run() { cube.loadChunks(new CompletionCallback() { @Override protected void onComplete() { scheduleChunksRefresh(); } @Override protected void onException(Exception exception) { logger.error("Refreshing chunks failed."); scheduleChunksRefresh(); } }); } }; } @Override public void start(final CompletionCallback callback) { cube.loadChunks(new CompletionCallback() { @Override protected void onComplete() { callback.setComplete(); scheduleChunksRefresh(); } @Override protected void onException(Exception e) { callback.setException(e); } }); } @Override public void stop(CompletionCallback callback) { if (scheduledRefreshChunksTask != null) scheduledRefreshChunksTask.cancel(); callback.setComplete(); } }