X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Futil%2FJob.java;h=13e6c7e0c1c5c768f0888ee7c3c977b4a016a8e4;hp=70c9d569714cd091788cbea36f9d8b8bcabd0c08;hb=943d8e7ed0ea5a9d56e7e694a3cbd849c52bad16;hpb=05e8bb67c3407cbecbb528db16b1608d8534907e diff --git a/src/org/cacert/gigi/util/Job.java b/src/org/cacert/gigi/util/Job.java index 70c9d569..13e6c7e0 100644 --- a/src/org/cacert/gigi/util/Job.java +++ b/src/org/cacert/gigi/util/Job.java @@ -8,48 +8,49 @@ import org.cacert.gigi.Certificate; import org.cacert.gigi.database.DatabaseConnection; public class Job { - int id; - - private Job(int id) { - this.id = id; - } - - public static enum JobType { - SIGN("sign"), REVOKE("revoke"); - private final String name; - - private JobType(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - - public static Job submit(Certificate targetId, JobType type) throws SQLException { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?"); - ps.setInt(1, targetId.getId()); - ps.setString(2, type.getName()); - ps.execute(); - return new Job(DatabaseConnection.lastInsertId(ps)); - } - - public boolean waitFor(int max) throws SQLException, InterruptedException { - long start = System.currentTimeMillis(); - PreparedStatement ps = DatabaseConnection.getInstance().prepare( - "SELECT 1 FROM `jobs` WHERE id=? AND state='open'"); - ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - rs.close(); - if (max != 0 && System.currentTimeMillis() - start > max) { - return false; - } - Thread.sleep((long) (2000 + Math.random() * 2000)); - rs = ps.executeQuery(); - } - rs.close(); - return true; - } + + int id; + + private Job(int id) { + this.id = id; + } + + public static enum JobType { + SIGN("sign"), REVOKE("revoke"); + + private final String name; + + private JobType(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public static Job submit(Certificate targetId, JobType type) throws SQLException { + PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?"); + ps.setInt(1, targetId.getId()); + ps.setString(2, type.getName()); + ps.execute(); + return new Job(DatabaseConnection.lastInsertId(ps)); + } + + public boolean waitFor(int max) throws SQLException, InterruptedException { + long start = System.currentTimeMillis(); + PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'"); + ps.setInt(1, id); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + rs.close(); + if (max != 0 && System.currentTimeMillis() - start > max) { + return false; + } + Thread.sleep((long) (2000 + Math.random() * 2000)); + rs = ps.executeQuery(); + } + rs.close(); + return true; + } }