1 package org.cacert.gigi.dbObjects;
5 import org.cacert.gigi.GigiApiException;
6 import org.cacert.gigi.database.DatabaseConnection;
7 import org.cacert.gigi.database.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9 import org.cacert.gigi.output.CertificateValiditySelector;
11 public class Job implements IdCachable {
19 public static enum JobType {
20 SIGN("sign"), REVOKE("revoke");
22 private final String name;
24 private JobType(String name) {
28 public String getName() {
33 public synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
34 CertificateValiditySelector.checkValidityLength(period);
35 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?");
36 ps.setInt(1, targetId.getId());
37 ps.setString(2, JobType.SIGN.getName());
39 ps.setString(4, period);
41 return cache.put(new Job(ps.lastInsertId()));
44 public synchronized static Job revoke(Certificate targetId) {
46 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`");
47 ps.setInt(1, targetId.getId());
48 ps.setString(2, JobType.REVOKE.getName());
50 return cache.put(new Job(ps.lastInsertId()));
53 public synchronized boolean waitFor(int max) throws InterruptedException {
54 long start = System.currentTimeMillis();
55 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
57 GigiResultSet rs = ps.executeQuery();
60 if (max != 0 && System.currentTimeMillis() - start > max) {
63 Thread.sleep((long) (2000 + Math.random() * 2000));
64 rs = ps.executeQuery();
75 static ObjectCache<Job> cache = new ObjectCache<>();
77 public synchronized static Job getById(int id) {
78 Job i = cache.get(id);
82 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=?'");
84 GigiResultSet rs = ps.executeQuery();