1 package org.cacert.gigi.util;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.Certificate;
8 import org.cacert.gigi.database.DatabaseConnection;
18 public static enum JobType {
19 SIGN("sign"), REVOKE("revoke");
21 private final String name;
23 private JobType(String name) {
27 public String getName() {
32 public static Job submit(Certificate targetId, JobType type) throws SQLException {
33 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
34 ps.setInt(1, targetId.getId());
35 ps.setString(2, type.getName());
37 return new Job(DatabaseConnection.lastInsertId(ps));
40 public boolean waitFor(int max) throws SQLException, InterruptedException {
41 long start = System.currentTimeMillis();
42 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
44 ResultSet rs = ps.executeQuery();
47 if (max != 0 && System.currentTimeMillis() - start > max) {
50 Thread.sleep((long) (2000 + Math.random() * 2000));
51 rs = ps.executeQuery();