Search in sources :

Example 1 with NotificationHelper

use of org.codelibs.fess.helper.NotificationHelper in project fess by codelibs.

the class PingSearchEngineJob method execute.

public String execute() {
    final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    final StringBuilder resultBuf = new StringBuilder();
    final PingResponse ping = searchEngineClient.ping();
    final int status = ping.getStatus();
    if (systemHelper.isChangedClusterState(status)) {
        if (fessConfig.hasNotification()) {
            final String toStrs = fessConfig.getNotificationTo();
            final String[] toAddresses;
            if (StringUtil.isNotBlank(toStrs)) {
                toAddresses = toStrs.split(",");
            } else {
                toAddresses = StringUtil.EMPTY_STRINGS;
            }
            final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
            try {
                final NotificationHelper notificationHelper = ComponentUtil.getNotificationHelper();
                SMailCallbackContext.setPreparedMessageHookOnThread(notificationHelper::send);
                EsStatusPostcard.droppedInto(postbox, postcard -> {
                    postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
                    postcard.addReplyTo(fessConfig.getMailReturnPath());
                    if (toAddresses.length > 0) {
                        stream(toAddresses).of(stream -> stream.map(String::trim).forEach(address -> {
                            postcard.addTo(address);
                        }));
                    } else {
                        postcard.addTo(fessConfig.getMailFromAddress());
                        postcard.dryrun();
                    }
                    postcard.setHostname(systemHelper.getHostname());
                    postcard.setClustername(ping.getClusterName());
                    postcard.setClusterstatus(ping.getClusterStatus());
                });
            } catch (final Exception e) {
                logger.warn("Failed to send a test mail.", e);
            } finally {
                SMailCallbackContext.clearPreparedMessageHookOnThread();
            }
        }
        resultBuf.append("Status of ").append(ping.getClusterName()).append(" is changed to ").append(ping.getClusterStatus()).append('.');
    } else if (status == 0) {
        resultBuf.append(ping.getClusterName()).append(" is alive.");
    } else {
        resultBuf.append(ping.getClusterName()).append(" is not available.");
    }
    return resultBuf.toString();
}
Also used : StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) SMailCallbackContext(org.dbflute.mail.send.hook.SMailCallbackContext) NotificationHelper(org.codelibs.fess.helper.NotificationHelper) StringUtil(org.codelibs.core.lang.StringUtil) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) EsStatusPostcard(org.codelibs.fess.mylasta.mail.EsStatusPostcard) Postbox(org.lastaflute.core.mail.Postbox) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Logger(org.apache.logging.log4j.Logger) PingResponse(org.codelibs.fess.entity.PingResponse) ComponentUtil(org.codelibs.fess.util.ComponentUtil) SystemHelper(org.codelibs.fess.helper.SystemHelper) LogManager(org.apache.logging.log4j.LogManager) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) PingResponse(org.codelibs.fess.entity.PingResponse) Postbox(org.lastaflute.core.mail.Postbox) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SystemHelper(org.codelibs.fess.helper.SystemHelper) NotificationHelper(org.codelibs.fess.helper.NotificationHelper)

Example 2 with NotificationHelper

use of org.codelibs.fess.helper.NotificationHelper in project fess by codelibs.

the class Crawler method sendMail.

protected void sendMail(final Map<String, String> infoMap) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    if (fessConfig.hasNotification()) {
        final Map<String, String> dataMap = new HashMap<>();
        for (final Map.Entry<String, String> entry : infoMap.entrySet()) {
            dataMap.put(StringUtil.decapitalize(entry.getKey()), entry.getValue());
        }
        String hostname = fessConfig.getMailHostname();
        if (StringUtil.isBlank(hostname)) {
            hostname = ComponentUtil.getSystemHelper().getHostname();
        }
        dataMap.put("hostname", hostname);
        logger.debug("\ninfoMap: {}\ndataMap: {}", infoMap, dataMap);
        final DynamicProperties systemProperties = ComponentUtil.getSystemProperties();
        final String toStrs = fessConfig.getNotificationTo();
        final Postbox postbox = ComponentUtil.getComponent(Postbox.class);
        try {
            final String[] toAddresses;
            if (StringUtil.isNotBlank(toStrs)) {
                toAddresses = toStrs.split(",");
            } else {
                toAddresses = StringUtil.EMPTY_STRINGS;
            }
            final NotificationHelper notificationHelper = ComponentUtil.getNotificationHelper();
            SMailCallbackContext.setPreparedMessageHookOnThread(notificationHelper::send);
            CrawlerPostcard.droppedInto(postbox, postcard -> {
                postcard.setFrom(fessConfig.getMailFromAddress(), fessConfig.getMailFromName());
                postcard.addReplyTo(fessConfig.getMailReturnPath());
                if (toAddresses.length > 0) {
                    stream(toAddresses).of(stream -> stream.map(String::trim).forEach(address -> {
                        postcard.addTo(address);
                    }));
                } else {
                    postcard.addTo(fessConfig.getMailFromAddress());
                    postcard.dryrun();
                }
                postcard.setCrawlerEndTime(getValueFromMap(dataMap, "crawlerEndTime", StringUtil.EMPTY));
                postcard.setCrawlerExecTime(getValueFromMap(dataMap, "crawlerExecTime", "0"));
                postcard.setCrawlerStartTime(getValueFromMap(dataMap, "crawlerStartTime", StringUtil.EMPTY));
                postcard.setDataCrawlEndTime(getValueFromMap(dataMap, "dataCrawlEndTime", StringUtil.EMPTY));
                postcard.setDataCrawlExecTime(getValueFromMap(dataMap, "dataCrawlExecTime", "0"));
                postcard.setDataCrawlStartTime(getValueFromMap(dataMap, "dataCrawlStartTime", StringUtil.EMPTY));
                postcard.setDataIndexSize(getValueFromMap(dataMap, "dataIndexSize", "0"));
                postcard.setDataIndexExecTime(getValueFromMap(dataMap, "dataIndexExecTime", "0"));
                postcard.setHostname(getValueFromMap(dataMap, "hostname", StringUtil.EMPTY));
                postcard.setWebFsCrawlEndTime(getValueFromMap(dataMap, "webFsCrawlEndTime", StringUtil.EMPTY));
                postcard.setWebFsCrawlExecTime(getValueFromMap(dataMap, "webFsCrawlExecTime", "0"));
                postcard.setWebFsCrawlStartTime(getValueFromMap(dataMap, "webFsCrawlStartTime", StringUtil.EMPTY));
                postcard.setWebFsIndexExecTime(getValueFromMap(dataMap, "webFsIndexExecTime", "0"));
                postcard.setWebFsIndexSize(getValueFromMap(dataMap, "webFsIndexSize", "0"));
                if (Constants.TRUE.equalsIgnoreCase(infoMap.get(Constants.CRAWLER_STATUS))) {
                    postcard.setStatus(Constants.OK);
                } else {
                    postcard.setStatus(Constants.FAIL);
                }
                postcard.setJobname(systemProperties.getProperty("job.runtime.name", StringUtil.EMPTY));
            });
        } finally {
            SMailCallbackContext.clearPreparedMessageHookOnThread();
        }
    }
}
Also used : ThreadUtil(org.codelibs.core.lang.ThreadUtil) Constants(org.codelibs.fess.Constants) SMailCallbackContext(org.dbflute.mail.send.hook.SMailCallbackContext) Date(java.util.Date) SearchEngineClient(org.codelibs.fess.es.client.SearchEngineClient) SystemMonitorTarget(org.codelibs.fess.timer.SystemMonitorTarget) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) CrawlerPostcard(org.codelibs.fess.mylasta.mail.CrawlerPostcard) Map(java.util.Map) JvmInfo(org.opensearch.monitor.jvm.JvmInfo) CmdLineParser(org.kohsuke.args4j.CmdLineParser) GenericExternalContext(org.lastaflute.di.core.external.GenericExternalContext) Resource(javax.annotation.Resource) Option(org.kohsuke.args4j.Option) CoreLibConstants(org.codelibs.core.CoreLibConstants) Collectors(java.util.stream.Collectors) CrawlingInfoService(org.codelibs.fess.app.service.CrawlingInfoService) CmdLineException(org.kohsuke.args4j.CmdLineException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) OsProbe(org.opensearch.monitor.os.OsProbe) ComponentUtil(org.codelibs.fess.util.ComponentUtil) CrawlingInfoHelper(org.codelibs.fess.helper.CrawlingInfoHelper) ThreadDumpUtil(org.codelibs.fess.util.ThreadDumpUtil) PathMappingService(org.codelibs.fess.app.service.PathMappingService) Queue(java.util.Queue) DataIndexHelper(org.codelibs.fess.helper.DataIndexHelper) TimeoutTask(org.codelibs.core.timer.TimeoutTask) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) WebFsIndexHelper(org.codelibs.fess.helper.WebFsIndexHelper) NotificationHelper(org.codelibs.fess.helper.NotificationHelper) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SingletonLaContainerFactory(org.lastaflute.di.core.factory.SingletonLaContainerFactory) ArrayList(java.util.ArrayList) Postbox(org.lastaflute.core.mail.Postbox) InterruptedRuntimeException(org.codelibs.core.exception.InterruptedRuntimeException) FesenClient(org.codelibs.fess.crawler.client.FesenClient) ManagementFactory(java.lang.management.ManagementFactory) StreamUtil.stream(org.codelibs.core.stream.StreamUtil.stream) DynamicProperties(org.codelibs.core.misc.DynamicProperties) ProcessProbe(org.opensearch.monitor.process.ProcessProbe) ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) StringUtil(org.codelibs.core.lang.StringUtil) IOException(java.io.IOException) DuplicateHostHelper(org.codelibs.fess.helper.DuplicateHostHelper) InputStreamReader(java.io.InputStreamReader) File(java.io.File) PathMappingHelper(org.codelibs.fess.helper.PathMappingHelper) GenericExternalContextComponentDefRegister(org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister) TimeoutManager(org.codelibs.core.timer.TimeoutManager) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) HashMap(java.util.HashMap) NotificationHelper(org.codelibs.fess.helper.NotificationHelper) DynamicProperties(org.codelibs.core.misc.DynamicProperties) Postbox(org.lastaflute.core.mail.Postbox) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 StringUtil (org.codelibs.core.lang.StringUtil)2 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)2 SearchEngineClient (org.codelibs.fess.es.client.SearchEngineClient)2 NotificationHelper (org.codelibs.fess.helper.NotificationHelper)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 ComponentUtil (org.codelibs.fess.util.ComponentUtil)2 SMailCallbackContext (org.dbflute.mail.send.hook.SMailCallbackContext)2 Postbox (org.lastaflute.core.mail.Postbox)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ManagementFactory (java.lang.management.ManagementFactory)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1