X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FDomainPingConfiguration.java;h=772b066f173db45cb0535d2d110b1a7faa8a86bc;hp=1146cca0c5c8b0b451c1f75c334cff0f71e87a91;hb=d7be034f96e06985f57d86d2779c434276b5bd4d;hpb=3e123160ad59a2e1162518923965562ff947b6d1 diff --git a/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java b/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java index 1146cca0..772b066f 100644 --- a/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java +++ b/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java @@ -1,35 +1,38 @@ package org.cacert.gigi.dbObjects; -import org.cacert.gigi.database.DatabaseConnection; +import java.util.Date; + +import org.cacert.gigi.Gigi; +import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.output.template.SprintfCommand; public class DomainPingConfiguration implements IdCachable { - public static enum PingType { - EMAIL, DNS, HTTP, SSL; - } + private static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000; private int id; private Domain target; - private PingType type; + private DomainPingType type; private String info; private DomainPingConfiguration(int id) { - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?"); - ps.setInt(1, id); - - GigiResultSet rs = ps.executeQuery(); - if ( !rs.next()) { - throw new IllegalArgumentException("Invalid pingconfig id " + id); + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) { + ps.setInt(1, id); + + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + throw new IllegalArgumentException("Invalid pingconfig id " + id); + } + this.id = rs.getInt("id"); + target = Domain.getById(rs.getInt("domainid")); + type = DomainPingType.valueOf(rs.getString("type").toUpperCase()); + info = rs.getString("info"); } - this.id = rs.getInt("id"); - target = Domain.getById(rs.getInt("domainid")); - type = PingType.valueOf(rs.getString("type").toUpperCase()); - info = rs.getString("info"); } @Override @@ -41,7 +44,7 @@ public class DomainPingConfiguration implements IdCachable { return target; } - public PingType getType() { + public DomainPingType getType() { return type; } @@ -59,10 +62,34 @@ public class DomainPingConfiguration implements IdCachable { return res; } - public void requestReping() { - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE pingconfig set reping='y' WHERE id=?"); - ps.setInt(1, id); - ps.execute(); + public Date getLastExecution() { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Date(rs.getTimestamp("stamp").getTime()); + } + return new Date(0); + } } + public Date getLastSuccess() { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Date(rs.getTimestamp("stamp").getTime()); + } + return new Date(0); + } + } + + public synchronized void requestReping() throws GigiApiException { + Date lastExecution = getLastExecution(); + if (lastExecution.getTime() + REPING_MINIMUM_DELAY < System.currentTimeMillis()) { + Gigi.notifyPinger(this); + return; + } + throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY))); + } }