use of org.codelibs.fess.exception.ScheduledJobException 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<>();
ComponentUtil.getComponent(ScheduledJobBhv.class).selectByPK(scheduledJob.getId()).ifPresent(e -> params.put(Constants.SCHEDULED_JOB, e)).orElse(() -> {
logger.warn("Job {} is not found.", scheduledJob.getId());
});
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));
}
});
}
use of org.codelibs.fess.exception.ScheduledJobException 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);
}
}
}
Aggregations