View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.job;
17  
18  import org.codelibs.core.timer.TimeoutTask;
19  import org.codelibs.fess.Constants;
20  import org.codelibs.fess.es.config.exentity.JobLog;
21  import org.codelibs.fess.es.config.exentity.ScheduledJob;
22  import org.codelibs.fess.helper.JobHelper;
23  import org.codelibs.fess.helper.SystemHelper;
24  import org.codelibs.fess.job.JobExecutor;
25  import org.codelibs.fess.job.ScheduledJobException;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.lastaflute.job.JobManager;
28  import org.lastaflute.job.LaJob;
29  import org.lastaflute.job.LaJobRuntime;
30  import org.lastaflute.job.key.LaJobUnique;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class ScriptExecutorJob implements LaJob {
35      private static final Logger logger = LoggerFactory.getLogger(ScriptExecutorJob.class);
36  
37      @Override
38      public void run(final LaJobRuntime runtime) {
39          if (!runtime.getParameterMap().containsKey(Constants.SCHEDULED_JOB)) {
40              logger.warn(Constants.SCHEDULED_JOB + " is empty.");
41              return;
42          }
43          runtime.stopIfNeeds();
44  
45          final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
46          final JobManager jobManager = ComponentUtil.getJobManager();
47          final ScheduledJob../org/codelibs/fess/es/config/exentity/ScheduledJob.html#ScheduledJob">ScheduledJob scheduledJob = (ScheduledJob) runtime.getParameterMap().get(Constants.SCHEDULED_JOB);
48          final String id = scheduledJob.getId();
49          final String target = scheduledJob.getTarget();
50          if (!ComponentUtil.getFessConfig().isSchedulerTarget(target)) {
51              logger.info("Ignore Job " + id + ":" + scheduledJob.getName() + " because of not target: " + scheduledJob.getTarget());
52              return;
53          }
54  
55          final JobHelper jobHelper = ComponentUtil.getJobHelper();
56          if (!jobHelper.isAvailable(id)) {
57              logger.info("Job " + id + " is unavailable. Unregistering this job.");
58              jobHelper.unregister(scheduledJob);
59              return;
60          }
61  
62          final JobLogig/exentity/JobLog.html#JobLog">JobLog jobLog = new JobLog(scheduledJob);
63          final String scriptType = scheduledJob.getScriptType();
64          final String script = scheduledJob.getScriptData();
65  
66          final JobExecutor jobExecutor = ComponentUtil.getJobExecutor(scriptType);
67          if (jobExecutor == null) {
68              throw new ScheduledJobException("No jobExecutor: " + scriptType);
69          }
70  
71          if (!jobManager.findJobByUniqueOf(LaJobUnique.of(id)).isPresent()) {
72              if (logger.isDebugEnabled()) {
73                  logger.debug("Job " + id + " is running.");
74              }
75              return;
76          }
77  
78          TimeoutTask task = null;
79          try {
80              if (scheduledJob.isLoggingEnabled()) {
81                  jobHelper.store(jobLog);
82                  task = jobHelper.startMonitorTask(jobLog);
83              }
84  
85              if (logger.isDebugEnabled()) {
86                  logger.debug("Starting Job " + id + ". scriptType: " + scriptType + ", script: " + script);
87              } else if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
88                  logger.info("Starting Job " + id + ".");
89              }
90  
91              final Object ret = jobExecutor.execute(script);
92              if (ret == null) {
93                  if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
94                      logger.info("Finished Job " + id + ".");
95                  }
96              } else {
97                  if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
98                      logger.info("Finished Job " + id + ". The return value is:\n" + ret);
99                  }
100                 jobLog.setScriptResult(ret.toString());
101             }
102             jobLog.setJobStatus(Constants.OK);
103         } catch (final Throwable t) {
104             logger.error("Failed to execute " + id + ": " + script, t);
105             jobLog.setJobStatus(Constants.FAIL);
106             jobLog.setScriptResult(systemHelper.abbreviateLongText(t.getLocalizedMessage()));
107         } finally {
108             if (task != null) {
109                 try {
110                     task.stop();
111                 } catch (final Exception e) {
112                     logger.warn("Failed to stop " + jobLog, e);
113                 }
114             }
115             jobLog.setEndTime(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
116             if (logger.isDebugEnabled()) {
117                 logger.debug("jobLog: " + jobLog);
118             }
119             if (scheduledJob.isLoggingEnabled()) {
120                 jobHelper.store(jobLog);
121             }
122         }
123     }
124 
125 }