Search in sources :

Example 11 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class GenerateThumbnailJob method execute.

@Override
public String execute() {
    final StringBuilder resultBuf = new StringBuilder();
    if (sessionId == null) {
        // create session id
        sessionId = RandomStringUtils.randomAlphabetic(15);
    }
    resultBuf.append("Session Id: ").append(sessionId).append("\n");
    if (jobExecutor != null) {
        jobExecutor.addShutdownListener(() -> ComponentUtil.getProcessHelper().destroyProcess(sessionId));
    }
    final TimeoutTask timeoutTask = createTimeoutTask();
    try {
        executeThumbnailGenerator();
    } catch (final Exception e) {
        logger.warn("Failed to generate thumbnails.", e);
        resultBuf.append(e.getMessage()).append("\n");
    } finally {
        if (timeoutTask != null && !timeoutTask.isCanceled()) {
            timeoutTask.cancel();
        }
    }
    return resultBuf.toString();
}
Also used : JobProcessingException(org.codelibs.fess.exception.JobProcessingException) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Example 12 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class ScriptExecutorJob method process.

protected void process(final LaJobRuntime runtime) {
    if (!runtime.getParameterMap().containsKey(Constants.SCHEDULED_JOB)) {
        logger.warn("{} is empty.", Constants.SCHEDULED_JOB);
        return;
    }
    runtime.stopIfNeeds();
    final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
    final JobManager jobManager = ComponentUtil.getJobManager();
    final ScheduledJob scheduledJob = (ScheduledJob) runtime.getParameterMap().get(Constants.SCHEDULED_JOB);
    final String id = scheduledJob.getId();
    final String target = scheduledJob.getTarget();
    if (!ComponentUtil.getFessConfig().isSchedulerTarget(target)) {
        logger.info("Ignore Job {}:{} because of not target: {}", scheduledJob.getName(), id, scheduledJob.getTarget());
        return;
    }
    final JobHelper jobHelper = ComponentUtil.getJobHelper();
    if (!jobHelper.isAvailable(id)) {
        logger.info("Job {} is unavailable. Unregistering this job.", id);
        jobHelper.unregister(scheduledJob);
        return;
    }
    final JobLog jobLog = new JobLog(scheduledJob);
    final String scriptType = scheduledJob.getScriptType();
    final String script = scheduledJob.getScriptData();
    final JobExecutor jobExecutor = ComponentUtil.getJobExecutor(scriptType);
    if (jobExecutor == null) {
        throw new ScheduledJobException("No jobExecutor: " + scriptType);
    }
    if (!jobManager.findJobByUniqueOf(LaJobUnique.of(id)).isPresent()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Job {} is running.", id);
        }
        return;
    }
    TimeoutTask task = null;
    try {
        if (scheduledJob.isLoggingEnabled()) {
            jobHelper.store(jobLog);
            task = jobHelper.startMonitorTask(jobLog);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Starting Job {}. scriptType: {}, script: {}", id, scriptType, script);
        } else if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
            logger.info("Starting Job {}.", id);
        }
        final Object ret = jobExecutor.execute(Constants.DEFAULT_SCRIPT, script);
        if (ret == null) {
            if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
                logger.info("Finished Job {}.", id);
            }
        } else {
            if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
                logger.info("Finished Job {}. The return value is:\n{}", id, ret);
            }
            jobLog.setScriptResult(ret.toString());
        }
        jobLog.setJobStatus(Constants.OK);
    } catch (final Throwable t) {
        logger.warn("Failed to execute {}: {}", id, script, t);
        jobLog.setJobStatus(Constants.FAIL);
        jobLog.setScriptResult(systemHelper.abbreviateLongText(t.getLocalizedMessage()));
    } finally {
        if (task != null) {
            try {
                task.stop();
            } catch (final Exception e) {
                logger.warn("Failed to stop {}", jobLog, e);
            }
        }
        jobLog.setEndTime(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
        if (logger.isDebugEnabled()) {
            logger.debug("jobLog: {}", jobLog);
        }
        if (scheduledJob.isLoggingEnabled()) {
            jobHelper.store(jobLog);
        }
    }
}
Also used : SystemHelper(org.codelibs.fess.helper.SystemHelper) ScheduledJobException(org.codelibs.fess.exception.ScheduledJobException) JobExecutor(org.codelibs.fess.job.JobExecutor) ScheduledJob(org.codelibs.fess.es.config.exentity.ScheduledJob) JobHelper(org.codelibs.fess.helper.JobHelper) JobLog(org.codelibs.fess.es.config.exentity.JobLog) JobManager(org.lastaflute.job.JobManager) ScheduledJobException(org.codelibs.fess.exception.ScheduledJobException) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Example 13 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class Crawler method main.

public static void main(final String[] args) {
    final Options options = new Options();
    final CmdLineParser parser = new CmdLineParser(options);
    try {
        parser.parseArgument(args);
    } catch (final CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("java " + Crawler.class.getCanonicalName() + " [options...] arguments...");
        parser.printUsage(System.err);
        return;
    }
    if (logger.isDebugEnabled()) {
        try {
            ManagementFactory.getRuntimeMXBean().getInputArguments().stream().forEach(s -> logger.debug("Parameter: {}", s));
            System.getProperties().entrySet().stream().forEach(e -> logger.debug("Property: {}={}", e.getKey(), e.getValue()));
            System.getenv().entrySet().forEach(e -> logger.debug("Env: {}={}", e.getKey(), e.getValue()));
            logger.debug("Option: {}", options);
        } catch (final Exception e) {
        // ignore
        }
    }
    initializeProbes();
    final String httpAddress = System.getProperty(Constants.FESS_ES_HTTP_ADDRESS);
    if (StringUtil.isNotBlank(httpAddress)) {
        System.setProperty(FesenClient.HTTP_ADDRESS, httpAddress);
    }
    TimeoutTask systemMonitorTask = null;
    Thread commandThread = null;
    int exitCode;
    try {
        running.set(true);
        SingletonLaContainerFactory.setConfigPath("app.xml");
        SingletonLaContainerFactory.setExternalContext(new GenericExternalContext());
        SingletonLaContainerFactory.setExternalContextComponentDefRegister(new GenericExternalContextComponentDefRegister());
        SingletonLaContainerFactory.init();
        final Thread shutdownCallback = new Thread("ShutdownHook") {

            @Override
            public void run() {
                destroyContainer();
            }
        };
        Runtime.getRuntime().addShutdownHook(shutdownCallback);
        commandThread = new Thread(() -> {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                String command;
                while (true) {
                    try {
                        while (!reader.ready()) {
                            ThreadUtil.sleep(1000L);
                        }
                        command = reader.readLine().trim();
                        if (logger.isDebugEnabled()) {
                            logger.debug("Process command: {}", command);
                        }
                        if (Constants.CRAWLER_PROCESS_COMMAND_THREAD_DUMP.equals(command)) {
                            ThreadDumpUtil.printThreadDump();
                        } else {
                            logger.warn("Unknown process command: {}", command);
                        }
                        if (Thread.interrupted()) {
                            return;
                        }
                    } catch (final InterruptedRuntimeException e) {
                        return;
                    }
                }
            } catch (final IOException e) {
                logger.debug("I/O exception.", e);
            }
        }, "ProcessCommand");
        commandThread.start();
        systemMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(new SystemMonitorTarget(), ComponentUtil.getFessConfig().getCrawlerSystemMonitorIntervalAsInteger(), true);
        exitCode = process(options);
    } catch (final ContainerNotAvailableException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Crawler is stopped.", e);
        } else if (logger.isInfoEnabled()) {
            logger.info("Crawler is stopped.");
        }
        exitCode = Constants.EXIT_FAIL;
    } catch (final Throwable t) {
        logger.error("Crawler does not work correctly.", t);
        exitCode = Constants.EXIT_FAIL;
    } finally {
        if (commandThread != null && commandThread.isAlive()) {
            commandThread.interrupt();
        }
        if (systemMonitorTask != null) {
            systemMonitorTask.cancel();
        }
        destroyContainer();
    }
    if (exitCode != Constants.EXIT_OK) {
        System.exit(exitCode);
    }
}
Also used : ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) InterruptedRuntimeException(org.codelibs.core.exception.InterruptedRuntimeException) CmdLineParser(org.kohsuke.args4j.CmdLineParser) InputStreamReader(java.io.InputStreamReader) SystemMonitorTarget(org.codelibs.fess.timer.SystemMonitorTarget) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) InterruptedRuntimeException(org.codelibs.core.exception.InterruptedRuntimeException) ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) IOException(java.io.IOException) TimeoutTask(org.codelibs.core.timer.TimeoutTask) BufferedReader(java.io.BufferedReader) GenericExternalContext(org.lastaflute.di.core.external.GenericExternalContext) CmdLineException(org.kohsuke.args4j.CmdLineException) GenericExternalContextComponentDefRegister(org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister)

Aggregations

TimeoutTask (org.codelibs.core.timer.TimeoutTask)13 AccessTimeoutTarget (org.codelibs.fess.crawler.client.AccessTimeoutTarget)5 JobProcessingException (org.codelibs.fess.exception.JobProcessingException)4 ContainerNotAvailableException (org.codelibs.fess.exception.ContainerNotAvailableException)3 SystemMonitorTarget (org.codelibs.fess.timer.SystemMonitorTarget)3 CmdLineException (org.kohsuke.args4j.CmdLineException)3 CmdLineParser (org.kohsuke.args4j.CmdLineParser)3 GenericExternalContext (org.lastaflute.di.core.external.GenericExternalContext)3 GenericExternalContextComponentDefRegister (org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister)3 IOException (java.io.IOException)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 BasicHeader (org.apache.http.message.BasicHeader)1 InterruptedRuntimeException (org.codelibs.core.exception.InterruptedRuntimeException)1