package jobs;

import helpers.DnsUpdateHelper;

import java.util.List;
import java.util.concurrent.TimeUnit;

import models.Domain;
import play.Logger;
import play.libs.Akka;
import scala.concurrent.duration.Duration;

public class DnsUpdateJob implements Runnable {

	public DnsUpdateJob() {
		Logger.info("@" + System.currentTimeMillis() + " DnsUpdate scheduled");
	}

	public static void schedule() {
		DnsUpdateJob job = new DnsUpdateJob();
		Akka.system().scheduler().schedule(
				Duration.create(500, TimeUnit.MILLISECONDS), // initial delay
				Duration.create(1, TimeUnit.MINUTES), // run job every 1 minutes
				job, Akka.system().dispatcher());
	}

	@Override
	public void run() {
		Logger.info("@" + System.currentTimeMillis() + " DnsUpdate has started");
		List<Domain> domains = Domain.find.all();
		for (Domain domain : domains) {
			if (domain.findNeedsToChanged().size() > 0 || domain.forceUpdate) {
				Logger.info("@" + System.currentTimeMillis() + " updating domain " + domain.name + " "
						+ domain.findNeedsToChanged().size() + "/" + domain.dnsEntries.size());
				new DnsUpdateHelper(domain).update();
			}
			Logger.info("@" + System.currentTimeMillis() + " no update necessary for " + domain.name);
		}
		Logger.info("@" + System.currentTimeMillis() + " DnsUpdate has ended");
	}

}

@@ -475,21 +475,16 @@
 chedule(
-%0A%09%09%09%09
 Duration
@@ -530,16 +530,41 @@
  initial
+%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09//
  delay%0A%09
@@ -781,16 +781,25 @@
 rted%22);%0A
+%09%09try %7B%0A%09
 %09%09List%3CD
@@ -836,16 +836,17 @@
 ll();%0A%09%09
+%09
 for (Dom
@@ -869,16 +869,17 @@
 ains) %7B%0A
+%09
 %09%09%09if (d
@@ -937,24 +937,25 @@
 ceUpdate) %7B%0A
+%09
 %09%09%09%09Logger.i
@@ -1039,16 +1039,17 @@
 %22%0A%09%09%09%09%09%09
+%09
 + domain
@@ -1116,16 +1116,17 @@
 ));%0A%09%09%09%09
+%09
 new DnsU
@@ -1159,16 +1159,17 @@
 ();%0A
+%09
 %09%09%09%7D%0A%09%09%09
 Logg
@@ -1164,16 +1164,17 @@
 %09%09%09%7D%0A%09%09%09
+%09
 Logger.i
@@ -1256,16 +1256,94 @@
 .name);%0A
+%09%09%09%7D%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09Logger.warn(ex.getLocalizedMessage(), ex);%0A
 %09%09%7D%0A%09%09Lo

make sure, the update Job won’t crash at all costs…