]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Job.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / dbObjects / Job.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.sql.Date;
4
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;
10
11 public class Job implements IdCachable {
12
13     private int id;
14
15     private Job(int id) {
16         this.id = id;
17     }
18
19     public static enum JobType implements DBEnum {
20         SIGN("sign"), REVOKE("revoke");
21
22         private final String name;
23
24         private JobType(String name) {
25             this.name = name;
26         }
27
28         @Override
29         public String getDBName() {
30             return name;
31         }
32     }
33
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);
39             ps.setDate(3, start);
40             ps.setString(4, period);
41             ps.execute();
42             return cache.put(new Job(ps.lastInsertId()));
43         }
44     }
45
46     public synchronized static Job revoke(Certificate targetId) {
47
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);
51             ps.execute();
52             return cache.put(new Job(ps.lastInsertId()));
53         }
54     }
55
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'")) {
59             ps.setInt(1, id);
60             GigiResultSet rs = ps.executeQuery();
61             while (rs.next()) {
62                 rs.close();
63                 if (max != 0 && System.currentTimeMillis() - start > max) {
64                     return false;
65                 }
66                 try {
67                     this.wait((long) (2000 + Math.random() * 2000));
68                 } catch (InterruptedException ie) {
69                     // Ignore the interruption
70                     ie.printStackTrace();
71                 }
72                 rs = ps.executeQuery();
73             }
74         }
75         return true;
76     }
77
78     @Override
79     public int getId() {
80         return id;
81     }
82
83     static ObjectCache<Job> cache = new ObjectCache<>();
84
85     public synchronized static Job getById(int id) {
86         Job i = cache.get(id);
87         if (i != null) {
88             return i;
89         }
90         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?")) {
91             ps.setInt(1, id);
92             GigiResultSet rs = ps.executeQuery();
93             if (rs.next()) {
94                 Job j = new Job(id);
95                 cache.put(j);
96                 return j;
97             }
98             return null;
99         }
100
101     }
102 }