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.es.client.SearchEngineClient;
25 import org.codelibs.fess.helper.NotificationHelper;
26 import org.codelibs.fess.helper.SystemHelper;
27 import org.codelibs.fess.mylasta.direction.FessConfig;
28 import org.codelibs.fess.mylasta.mail.EsStatusPostcard;
29 import org.codelibs.fess.util.ComponentUtil;
30 import org.dbflute.mail.send.hook.SMailCallbackContext;
31 import org.lastaflute.core.mail.Postbox;
32
33 public class PingSearchEngineJob {
34
35 private static final Logger logger = LogManager.getLogger(PingSearchEngineJob.class);
36
37 public String execute() {
38 final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
39 final FessConfig fessConfig = ComponentUtil.getFessConfig();
40 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
41
42 final StringBuilder resultBuf = new StringBuilder();
43
44 final PingResponse ping = searchEngineClient.ping();
45 final int status = ping.getStatus();
46 if (systemHelper.isChangedClusterState(status)) {
47 if (fessConfig.hasNotification()) {
48 final String toStrs = fessConfig.getNotificationTo();
49 final String[] toAddresses;
50 if (StringUtil.isNotBlank(toStrs)) {
51 toAddresses = toStrs.split(",");
52 } else {
53 toAddresses = StringUtil.EMPTY_STRINGS;
54 }
55 final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
56 try {
57 final NotificationHelper notificationHelper = ComponentUtil.getNotificationHelper();
58 SMailCallbackContext.setPreparedMessageHookOnThread(notificationHelper::send);
59 EsStatusPostcard.droppedInto(postbox, postcard -> {
60 postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
61 postcard.addReplyTo(fessConfig.getMailReturnPath());
62 if (toAddresses.length > 0) {
63 stream(toAddresses).of(stream -> stream.map(String::trim).forEach(address -> {
64 postcard.addTo(address);
65 }));
66 } else {
67 postcard.addTo(fessConfig.getMailFromAddress());
68 postcard.dryrun();
69 }
70 postcard.setHostname(systemHelper.getHostname());
71 postcard.setClustername(ping.getClusterName());
72 postcard.setClusterstatus(ping.getClusterStatus());
73 });
74 } catch (final Exception e) {
75 logger.warn("Failed to send a test mail.", e);
76 } finally {
77 SMailCallbackContext.clearPreparedMessageHookOnThread();
78 }
79 }
80 resultBuf.append("Status of ").append(ping.getClusterName()).append(" is changed to ").append(ping.getClusterStatus())
81 .append('.');
82 } else if (status == 0) {
83 resultBuf.append(ping.getClusterName()).append(" is alive.");
84 } else {
85 resultBuf.append(ping.getClusterName()).append(" is not available.");
86 }
87
88 return resultBuf.toString();
89 }
90
91 }