1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.job;
17
18 import org.codelibs.core.lang.StringUtil;
19 import org.codelibs.fess.entity.PingResponse;
20 import org.codelibs.fess.es.client.FessEsClient;
21 import org.codelibs.fess.helper.SystemHelper;
22 import org.codelibs.fess.mylasta.direction.FessConfig;
23 import org.codelibs.fess.mylasta.mail.EsStatusPostcard;
24 import org.codelibs.fess.util.ComponentUtil;
25 import org.lastaflute.core.mail.Postbox;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class PingEsJob {
30
31 private static final Logger logger = LoggerFactory.getLogger(PingEsJob.class);
32
33 public String execute() {
34 final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
35 final FessConfig fessConfig = ComponentUtil.getFessConfig();
36 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
37
38 final StringBuilder resultBuf = new StringBuilder();
39
40 final String notificationTo = fessConfig.getNotificationTo();
41 final PingResponse ping = fessEsClient.ping();
42 final int status = ping.getStatus();
43 if (systemHelper.isChangedClusterState(status)) {
44 if (StringUtil.isNotBlank(notificationTo)) {
45 final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
46 try {
47 EsStatusPostcard.droppedInto(postbox, postcard -> {
48 postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
49 postcard.addReplyTo(fessConfig.getMailReturnPath());
50 postcard.addTo(notificationTo);
51 postcard.setHostname(systemHelper.getHostname());
52 postcard.setClustername(ping.getClusterName());
53 postcard.setClusterstatus(ping.getClusterStatus());
54 });
55 } catch (final Exception e) {
56 logger.warn("Failed to send a test mail.", e);
57 }
58 }
59 resultBuf.append("Status of ").append(ping.getClusterName()).append(" is changed to ").append(ping.getClusterStatus())
60 .append('.');
61 } else {
62 if (status == 0) {
63 resultBuf.append(ping.getClusterName()).append(" is alive.");
64 } else {
65 resultBuf.append(ping.getClusterName()).append(" is not available.");
66 }
67 }
68
69 return resultBuf.toString();
70 }
71
72 }