Search in sources :

Example 1 with CronParamsSupplier

use of org.lastaflute.job.subsidiary.CronParamsSupplier 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

HashMap (java.util.HashMap)1 Map (java.util.Map)1 StringUtil (org.codelibs.core.lang.StringUtil)1 TimeoutManager (org.codelibs.core.timer.TimeoutManager)1 TimeoutTarget (org.codelibs.core.timer.TimeoutTarget)1 TimeoutTask (org.codelibs.core.timer.TimeoutTask)1 Constants (org.codelibs.fess.Constants)1 JobLogBhv (org.codelibs.fess.es.config.exbhv.JobLogBhv)1 ScheduledJobBhv (org.codelibs.fess.es.config.exbhv.ScheduledJobBhv)1 JobLog (org.codelibs.fess.es.config.exentity.JobLog)1 ScheduledJob (org.codelibs.fess.es.config.exentity.ScheduledJob)1 JobNotFoundException (org.codelibs.fess.exception.JobNotFoundException)1 ScheduledJobException (org.codelibs.fess.job.ScheduledJobException)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 ComponentUtil (org.codelibs.fess.util.ComponentUtil)1 OptionalThing (org.dbflute.optional.OptionalThing)1 JobManager (org.lastaflute.job.JobManager)1 LaCron (org.lastaflute.job.LaCron)1 LaScheduledJob (org.lastaflute.job.LaScheduledJob)1 LaJobUnique (org.lastaflute.job.key.LaJobUnique)1