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.output.CertificateValiditySelector;
11 public class Job implements IdCachable {
19 public static enum JobType implements DBEnum {
20 SIGN("sign"), REVOKE("revoke");
22 private final String name;
24 private JobType(String name) {
29 public String getDBName() {
34 public synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
35 CertificateValiditySelector.checkValidityLength(period);
36 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) {
37 ps.setInt(1, targetId.getId());
38 ps.setEnum(2, JobType.SIGN);
40 ps.setString(4, period);
42 return cache.put(new Job(ps.lastInsertId()));
46 public synchronized static Job revoke(Certificate targetId) {
48 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) {
49 ps.setInt(1, targetId.getId());
50 ps.setEnum(2, JobType.REVOKE);
52 return cache.put(new Job(ps.lastInsertId()));
56 public synchronized boolean waitFor(int max) {
57 long start = System.currentTimeMillis();
58 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=? AND state='open'")) {
60 GigiResultSet rs = ps.executeQuery();
63 if (max != 0 && System.currentTimeMillis() - start > max) {
67 this.wait((long) (2000 + Math.random() * 2000));
68 } catch (InterruptedException ie) {
69 // Ignore the interruption
72 rs = ps.executeQuery();
83 static ObjectCache<Job> cache = new ObjectCache<>();
85 public synchronized static Job getById(int id) {
86 Job i = cache.get(id);
90 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?")) {
92 GigiResultSet rs = ps.executeQuery();