View Javadoc
1   /*
2    * Copyright 2012-2017 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 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  }