1 package club.wpia.gigi.dbObjects;
5 import club.wpia.gigi.GigiApiException;
6 import club.wpia.gigi.database.DBEnum;
7 import club.wpia.gigi.database.GigiPreparedStatement;
8 import club.wpia.gigi.database.GigiResultSet;
9 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
10 import club.wpia.gigi.output.CertificateValiditySelector;
12 public class Job implements IdCachable {
20 public static enum JobType implements DBEnum {
21 SIGN("sign"), REVOKE("revoke");
23 private final String name;
25 private JobType(String name) {
30 public String getDBName() {
35 protected synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
36 CertificateValiditySelector.checkValidityLength(period);
37 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) {
38 ps.setInt(1, targetId.getId());
39 ps.setEnum(2, JobType.SIGN);
41 ps.setString(4, period);
43 return cache.put(new Job(ps.lastInsertId()));
47 protected synchronized static Job revoke(Certificate targetId, RevocationType type) {
48 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `certsRevoked` SET id=?, type=?::`revocationType`")) {
49 ps.setInt(1, targetId.getId());
54 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) {
55 ps.setInt(1, targetId.getId());
56 ps.setEnum(2, JobType.REVOKE);
58 return cache.put(new Job(ps.lastInsertId()));
62 public synchronized boolean waitFor(int max) {
63 long start = System.currentTimeMillis();
64 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=? AND state='open'")) {
66 GigiResultSet rs = ps.executeQuery();
69 if (max != 0 && System.currentTimeMillis() - start > max) {
73 this.wait((long) (2000 + Math.random() * 2000));
74 } catch (InterruptedException ie) {
75 // Ignore the interruption
78 rs = ps.executeQuery();
89 static ObjectCache<Job> cache = new ObjectCache<>();
91 public synchronized static Job getById(int id) {
92 Job i = cache.get(id);
96 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?")) {
98 GigiResultSet rs = ps.executeQuery();