This is a cache of https://fess.codelibs.org/xref/org/codelibs/fess/job/PingEsJob.html. It is a snapshot of the page at 2020-10-25T01:24:55.878+0000.
PingEsJob xref
View Javadoc
1   /*
2    * Copyright 2012-2020 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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.FessEsClient;
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 PingEsJob {
34  
35      private static final Logger logger = LogManager.getLogger(PingEsJob.class);
36  
37      public String execute() {
38          final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
39          final FessConfig fessConfig = ComponentUtil.getFessConfig();
40          final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
41  
42          final StringBuilder resultBuf = new StringBuilder();
43  
44          final PingResponse ping = fessEsClient.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 {
83              if (status == 0) {
84                  resultBuf.append(ping.getClusterName()).append(" is alive.");
85              } else {
86                  resultBuf.append(ping.getClusterName()).append(" is not available.");
87              }
88          }
89  
90          return resultBuf.toString();
91      }
92  
93  }