Merge "Suggestions to enhance the SQL call pattern."
[gigi.git] / src / org / cacert / gigi / ping / PingerDaemon.java
1 package org.cacert.gigi.ping;
2
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.database.DatabaseConnection.Link;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
14 import org.cacert.gigi.dbObjects.DomainPingType;
15 import org.cacert.gigi.util.RandomToken;
16
17 public class PingerDaemon extends Thread {
18
19     HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
20
21     private GigiPreparedStatement searchNeededPings;
22
23     private KeyStore truststore;
24
25     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
26
27     public PingerDaemon(KeyStore truststore) {
28         this.truststore = truststore;
29     }
30
31     @Override
32     public void run() {
33         try (Link l = DatabaseConnection.newLink(false)) {
34             runWithConnection();
35         } catch (InterruptedException e) {
36             e.printStackTrace();
37         }
38     }
39
40     public void runWithConnection() {
41         searchNeededPings = new GigiPreparedStatement("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL OR `domainPinglog`.`when` < CURRENT_TIMESTAMP - interval '6 mons') AND `domains`.`deleted` IS NULL AND `pingconfig`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
42         pingers.put(DomainPingType.EMAIL, new EmailPinger());
43         pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
44         pingers.put(DomainPingType.HTTP, new HTTPFetch());
45         pingers.put(DomainPingType.DNS, new DNSPinger());
46
47         while (true) {
48             try {
49                 boolean worked = false;
50                 synchronized (this) {
51                     DomainPingConfiguration conf;
52                     while ((conf = toExecute.peek()) != null) {
53                         worked = true;
54                         handle(conf);
55                         toExecute.remove();
56                     }
57                     notifyAll();
58                 }
59
60                 GigiResultSet rs = searchNeededPings.executeQuery();
61                 while (rs.next()) {
62                     worked = true;
63                     handle(DomainPingConfiguration.getById(rs.getInt("id")));
64                 }
65                 try {
66                     if ( !worked) {
67                         Thread.sleep(5000);
68                     }
69                 } catch (InterruptedException e) {
70                 }
71             } catch (Throwable t) {
72                 t.printStackTrace();
73             }
74         }
75     }
76
77     private void handle(DomainPingConfiguration conf) {
78         DomainPingType type = conf.getType();
79         String config = conf.getInfo();
80         DomainPinger dp = pingers.get(type);
81         if (dp != null) {
82             if (dp instanceof EmailPinger) {
83                 String token = null;
84                 token = RandomToken.generateToken(16);
85                 config = config + ":" + token;
86             }
87             Domain target = conf.getTarget();
88             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
89             try {
90                 dp.ping(target, config, target.getOwner(), conf.getId());
91             } catch (Throwable t) {
92                 t.printStackTrace();
93                 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
94             }
95             System.err.println("done (" + System.currentTimeMillis() + ")");
96         }
97     }
98
99     public synchronized void queue(DomainPingConfiguration toReping) {
100         interrupt();
101         toExecute.add(toReping);
102         while (toExecute.size() > 0) {
103             try {
104                 wait();
105             } catch (InterruptedException e) {
106                 e.printStackTrace();
107             }
108         }
109     }
110 }