1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.job;
17
18 import static org.codelibs.core.stream.StreamUtil.stream;
19
20 import org.apache.logging.log4j.LogManager;
21 import org.apache.logging.log4j.Logger;
22 import org.codelibs.core.lang.StringUtil;
23 import org.codelibs.fess.entity.PingResponse;
24 import org.codelibs.fess.helper.NotificationHelper;
25 import org.codelibs.fess.helper.SystemHelper;
26 import org.codelibs.fess.mylasta.direction.FessConfig;
27 import org.codelibs.fess.mylasta.mail.EsStatusPostcard;
28 import org.codelibs.fess.opensearch.client.SearchEngineClient;
29 import org.codelibs.fess.util.ComponentUtil;
30 import org.dbflute.mail.send.hook.SMailCallbackContext;
31 import org.lastaflute.core.mail.Postbox;
32
33
34
35
36 public class PingSearchEngineJob {
37
38
39
40
41 public PingSearchEngineJob() {
42
43 }
44
45 private static final Logger logger = LogManager.getLogger(PingSearchEngineJob.class);
46
47
48
49
50
51
52 public String execute() {
53 final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
54 final FessConfig fessConfig = ComponentUtil.getFessConfig();
55 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
56
57 final StringBuilder resultBuf = new StringBuilder();
58
59 final PingResponse ping = searchEngineClient.ping();
60 final int status = ping.getStatus();
61 if (systemHelper.isChangedClusterState(status)) {
62 if (fessConfig.hasNotification()) {
63 final String toStrs = fessConfig.getNotificationTo();
64 final String[] toAddresses;
65 if (StringUtil.isNotBlank(toStrs)) {
66 toAddresses = toStrs.split(",");
67 } else {
68 toAddresses = StringUtil.EMPTY_STRINGS;
69 }
70 final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
71 try {
72 final NotificationHelper notificationHelper = ComponentUtil.getNotificationHelper();
73 SMailCallbackContext.setPreparedMessageHookOnThread(notificationHelper::send);
74 EsStatusPostcard.droppedInto(postbox, postcard -> {
75 postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
76 postcard.addReplyTo(fessConfig.getMailReturnPath());
77 if (toAddresses.length > 0) {
78 stream(toAddresses).of(stream -> stream.map(String::trim).forEach(address -> {
79 postcard.addTo(address);
80 }));
81 } else {
82 postcard.addTo(fessConfig.getMailFromAddress());
83 postcard.dryrun();
84 }
85 postcard.setHostname(systemHelper.getHostname());
86 postcard.setClustername(ping.getClusterName());
87 postcard.setClusterstatus(ping.getClusterStatus());
88 });
89 } catch (final Exception e) {
90 logger.warn("Failed to send a test mail.", e);
91 } finally {
92 SMailCallbackContext.clearPreparedMessageHookOnThread();
93 }
94 }
95 resultBuf.append("Status of ")
96 .append(ping.getClusterName())
97 .append(" is changed to ")
98 .append(ping.getClusterStatus())
99 .append('.');
100 } else if (status == 0) {
101 resultBuf.append(ping.getClusterName()).append(" is alive.");
102 } else {
103 resultBuf.append(ping.getClusterName()).append(" is not available.");
104 }
105
106 return resultBuf.toString();
107 }
108
109 }