Search in sources :

Example 6 with ScheduledJob

use of org.codelibs.fess.es.config.exentity.ScheduledJob 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)

Example 7 with ScheduledJob

use of org.codelibs.fess.es.config.exentity.ScheduledJob in project fess by codelibs.

the class JobHelper method register.

public void register(final LaCron cron, final ScheduledJob scheduledJob) {
    if (scheduledJob == null) {
        throw new ScheduledJobException("No job.");
    }
    final String id = scheduledJob.getId();
    if (!Constants.T.equals(scheduledJob.getAvailable())) {
        logger.info("Inactive Job " + id + ":" + scheduledJob.getName());
        try {
            unregister(scheduledJob);
        } catch (final Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to delete Job " + scheduledJob, e);
            }
        }
        return;
    }
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final CronParamsSupplier paramsOp = () -> {
        final Map<String, Object> params = new HashMap<>();
        params.put(Constants.SCHEDULED_JOB, ComponentUtil.getComponent(ScheduledJobBhv.class).selectByPK(scheduledJob.getId()).orElseThrow(() -> new JobNotFoundException(scheduledJob)));
        return params;
    };
    findJobByUniqueOf(LaJobUnique.of(id)).ifPresent(job -> {
        if (!job.isUnscheduled()) {
            if (StringUtil.isNotBlank(scheduledJob.getCronExpression())) {
                logger.info("Starting Job " + id + ":" + scheduledJob.getName());
                final String cronExpression = scheduledJob.getCronExpression();
                job.reschedule(cronExpression, op -> op.changeNoticeLogToDebug().params(paramsOp));
            } else {
                logger.info("Inactive Job " + id + ":" + scheduledJob.getName());
                job.becomeNonCron();
            }
        } else if (StringUtil.isNotBlank(scheduledJob.getCronExpression())) {
            logger.info("Starting Job " + id + ":" + scheduledJob.getName());
            final String cronExpression = scheduledJob.getCronExpression();
            job.reschedule(cronExpression, op -> op.changeNoticeLogToDebug().params(paramsOp));
        }
    }).orElse(() -> {
        if (StringUtil.isNotBlank(scheduledJob.getCronExpression())) {
            logger.info("Starting Job " + id + ":" + scheduledJob.getName());
            final String cronExpression = scheduledJob.getCronExpression();
            cron.register(cronExpression, fessConfig.getSchedulerJobClassAsClass(), fessConfig.getSchedulerConcurrentExecModeAsEnum(), op -> op.uniqueBy(id).changeNoticeLogToDebug().params(paramsOp));
        } else {
            logger.info("Inactive Job " + id + ":" + scheduledJob.getName());
            cron.registerNonCron(fessConfig.getSchedulerJobClassAsClass(), fessConfig.getSchedulerConcurrentExecModeAsEnum(), op -> op.uniqueBy(id).changeNoticeLogToDebug().params(paramsOp));
        }
    });
}
Also used : LaCron(org.lastaflute.job.LaCron) Constants(org.codelibs.fess.Constants) LaScheduledJob(org.lastaflute.job.LaScheduledJob) CronParamsSupplier(org.lastaflute.job.subsidiary.CronParamsSupplier) Logger(org.slf4j.Logger) OptionalThing(org.dbflute.optional.OptionalThing) StringUtil(org.codelibs.core.lang.StringUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) JobLog(org.codelibs.fess.es.config.exentity.JobLog) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) ComponentUtil(org.codelibs.fess.util.ComponentUtil) ScheduledJobException(org.codelibs.fess.job.ScheduledJobException) JobManager(org.lastaflute.job.JobManager) Map(java.util.Map) TimeoutTarget(org.codelibs.core.timer.TimeoutTarget) ScheduledJobBhv(org.codelibs.fess.es.config.exbhv.ScheduledJobBhv) TimeoutManager(org.codelibs.core.timer.TimeoutManager) LaJobUnique(org.lastaflute.job.key.LaJobUnique) TimeoutTask(org.codelibs.core.timer.TimeoutTask) JobLogBhv(org.codelibs.fess.es.config.exbhv.JobLogBhv) ScheduledJob(org.codelibs.fess.es.config.exentity.ScheduledJob) JobNotFoundException(org.codelibs.fess.exception.JobNotFoundException) ScheduledJobException(org.codelibs.fess.job.ScheduledJobException) JobNotFoundException(org.codelibs.fess.exception.JobNotFoundException) CronParamsSupplier(org.lastaflute.job.subsidiary.CronParamsSupplier) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) HashMap(java.util.HashMap) Map(java.util.Map) ScheduledJobException(org.codelibs.fess.job.ScheduledJobException) JobNotFoundException(org.codelibs.fess.exception.JobNotFoundException)

Aggregations

ScheduledJob (org.codelibs.fess.es.config.exentity.ScheduledJob)7 Execute (org.lastaflute.web.Execute)5 Constants (org.codelibs.fess.Constants)4 SchedulerPager (org.codelibs.fess.app.pager.SchedulerPager)4 AdminSchedulerAction.getScheduledJob (org.codelibs.fess.app.web.admin.scheduler.AdminSchedulerAction.getScheduledJob)4 ApiResult (org.codelibs.fess.app.web.api.ApiResult)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Resource (javax.annotation.Resource)3 ScheduledJobService (org.codelibs.fess.app.service.ScheduledJobService)3 CrudMode (org.codelibs.fess.app.web.CrudMode)3 ApiResponse (org.codelibs.fess.app.web.api.ApiResult.ApiResponse)3 Status (org.codelibs.fess.app.web.api.ApiResult.Status)3 FessApiAdminAction (org.codelibs.fess.app.web.api.admin.FessApiAdminAction)3 JobManager (org.lastaflute.job.JobManager)3 HtmlResponse (org.lastaflute.web.response.HtmlResponse)3 JsonResponse (org.lastaflute.web.response.JsonResponse)3 TimeoutTask (org.codelibs.core.timer.TimeoutTask)2 JobLog (org.codelibs.fess.es.config.exentity.JobLog)2 ScheduledJobException (org.codelibs.fess.job.ScheduledJobException)2