Search in sources :

Example 1 with JobManager

use of org.lastaflute.job.JobManager in project fess by codelibs.

the class AdminWizardAction method startCrawling.

@Execute
public HtmlResponse startCrawling(final StartCrawlingForm form) {
    verifyToken(() -> asIndexHtml());
    if (!processHelper.isProcessRunning()) {
        final List<ScheduledJob> scheduledJobList = scheduledJobService.getCrawlerJobList();
        final JobManager jobManager = ComponentUtil.getJobManager();
        for (final ScheduledJob scheduledJob : scheduledJobList) {
            jobManager.findJobByUniqueOf(LaJobUnique.of(scheduledJob.getId())).ifPresent(job -> {
                job.launchNow();
            });
        }
        saveInfo(messages -> messages.addSuccessStartCrawlProcess(GLOBAL));
    } else {
        saveError(messages -> messages.addErrorsFailedToStartCrawlProcess(GLOBAL));
    }
    return redirect(AdminWizardAction.class);
}
Also used : ScheduledJob(org.codelibs.fess.es.config.exentity.ScheduledJob) JobManager(org.lastaflute.job.JobManager) Execute(org.lastaflute.web.Execute)

Example 2 with JobManager

use of org.lastaflute.job.JobManager in project fess by codelibs.

the class JobHelper method register.

public void register(final ScheduledJob scheduledJob) {
    final JobManager jobManager = ComponentUtil.getJobManager();
    jobManager.schedule(cron -> register(cron, scheduledJob));
}
Also used : JobManager(org.lastaflute.job.JobManager)

Example 3 with JobManager

use of org.lastaflute.job.JobManager in project fess by codelibs.

the class ScriptExecutorJob method run.

@Override
public void run(final LaJobRuntime runtime) {
    if (!runtime.getParameterMap().containsKey(Constants.SCHEDULED_JOB)) {
        logger.warn(Constants.SCHEDULED_JOB + " is empty.");
        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 (!isTarget(target)) {
        logger.info("Ignore Job " + id + ":" + scheduledJob.getName() + " because of not target: " + scheduledJob.getTarget());
        return;
    }
    final JobHelper jobHelper = ComponentUtil.getJobHelper();
    if (!jobHelper.isAvailable(id)) {
        logger.info("Job " + id + " is unavailable. Unregistering this job.");
        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 " + id + " is running.");
        }
        return;
    }
    TimeoutTask task = null;
    try {
        if (scheduledJob.isLoggingEnabled()) {
            jobHelper.store(jobLog);
            task = jobHelper.startMonitorTask(jobLog);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Starting Job " + id + ". scriptType: " + scriptType + ", script: " + script);
        } else if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
            logger.info("Starting Job " + id + ".");
        }
        final Object ret = jobExecutor.execute(script);
        if (ret == null) {
            if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
                logger.info("Finished Job " + id + ".");
            }
        } else {
            if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
                logger.info("Finished Job " + id + ". The return value is:\n" + ret);
            }
            jobLog.setScriptResult(ret.toString());
        }
        jobLog.setJobStatus(Constants.OK);
    } catch (final Throwable t) {
        logger.error("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.job.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.job.ScheduledJobException) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Aggregations

JobManager (org.lastaflute.job.JobManager)3 ScheduledJob (org.codelibs.fess.es.config.exentity.ScheduledJob)2 TimeoutTask (org.codelibs.core.timer.TimeoutTask)1 JobLog (org.codelibs.fess.es.config.exentity.JobLog)1 JobHelper (org.codelibs.fess.helper.JobHelper)1 SystemHelper (org.codelibs.fess.helper.SystemHelper)1 JobExecutor (org.codelibs.fess.job.JobExecutor)1 ScheduledJobException (org.codelibs.fess.job.ScheduledJobException)1 Execute (org.lastaflute.web.Execute)1