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;
17 public static enum JobType {
18 SIGN("sign"), REVOKE("revoke");
19 private final String name;
21 private JobType(String name) {
25 public String getName() {
30 public static Job submit(Certificate targetId, JobType type) throws SQLException {
31 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
32 ps.setInt(1, targetId.getId());
33 ps.setString(2, type.getName());
35 return new Job(DatabaseConnection.lastInsertId(ps));
38 public boolean waitFor(int max) throws SQLException, InterruptedException {
39 long start = System.currentTimeMillis();
40 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
41 "SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
43 ResultSet rs = ps.executeQuery();
46 if (max != 0 && System.currentTimeMillis() - start > max) {
49 Thread.sleep((long) (2000 + Math.random() * 2000));
50 rs = ps.executeQuery();