/*******************************************************************************
 * Copyright 2013 Michael Marconi
 * 
 * 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 oncue.worker;

import static akka.pattern.Patterns.ask;

import java.util.concurrent.TimeoutException;

import oncue.common.messages.CleanupJobs;
import oncue.common.messages.Job;

import org.joda.time.Duration;

import scala.concurrent.Await;
import akka.util.Timeout;

/**
 * This job will clean up complete and failed runs from the backing store.
 * You'll probably want to run this as a timed job on a schedule.
 */
public class MaintenanceWorker extends AbstractWorker {

	private static final String EXPIRATION_AGE = "expiration-age";
	private static final String INCLUDE_FAILED_JOBS = "include-failed-jobs";

	@Override
	public void doWork(Job job) throws Exception {
		processJob();
	}

	private void processJob() throws Exception {

		boolean includeFailedJobs = false;
		if (job.getParams().containsKey(INCLUDE_FAILED_JOBS))
			includeFailedJobs = new Boolean(job.getParams().get(INCLUDE_FAILED_JOBS));

		if (!job.getParams().containsKey(EXPIRATION_AGE))
			throw new IllegalArgumentException(
					"You must define the 'expiration-age' parameter in the configuration of a maintenance worker");

		Duration expirationAge = Duration.millis(scala.concurrent.duration.Duration.create(
				job.getParams().get(EXPIRATION_AGE)).toMillis());
		CleanupJobs cleanupJobs = new CleanupJobs(includeFailedJobs, expirationAge);

		try {
			Await.result(
					ask(getContext().actorFor(settings.SCHEDULER_PATH), cleanupJobs, new Timeout(
							settings.SCHEDULER_TIMEOUT)), settings.SCHEDULER_TIMEOUT);
		} catch (TimeoutException e) {
			throw new RuntimeException("Timeout waiting for scheduler to clean up jobs");
		}
	}

	@Override
	protected void redoWork(Job job) throws Exception {
		processJob();
	}
}

@@ -1000,16 +1000,77 @@
 .Await;%0A
+import akka.event.Logging;%0Aimport akka.event.LoggingAdapter;%0A
 import a
@@ -1431,16 +1431,94 @@
 d-jobs%22;
+%0A%09private LoggingAdapter log = Logging.getLogger(getContext().system(), this);
 %0A%0A%09@Over
@@ -2233,16 +2233,32 @@
 ry %7B%0A%09%09%09
+Object object = 
 Await.re
@@ -2412,16 +2412,48 @@
 MEOUT);%0A
+%09%09%09log.info(object.toString());%0A
 %09%09%7D catc

Add rudimentary logging to see when maintenance worker has finished