View Javadoc
1   /*
2    * Copyright 2012-2021 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 javax.annotation.Resource;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.core.timer.TimeoutManager;
24  import org.codelibs.fess.Constants;
25  import org.codelibs.fess.app.logic.AccessContextLogic;
26  import org.codelibs.fess.app.service.ScheduledJobService;
27  import org.codelibs.fess.es.config.exbhv.JobLogBhv;
28  import org.codelibs.fess.helper.JobHelper;
29  import org.codelibs.fess.helper.SystemHelper;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.dbflute.optional.OptionalThing;
33  import org.lastaflute.core.time.TimeManager;
34  import org.lastaflute.job.LaCron;
35  import org.lastaflute.job.LaJob;
36  import org.lastaflute.job.LaJobRunner;
37  import org.lastaflute.job.LaJobScheduler;
38  
39  public class AllJobScheduler implements LaJobScheduler {
40  
41      private static final Logger logger = LogManager.getLogger(AllJobScheduler.class);
42  
43      protected static final String APP_TYPE = "JOB";
44  
45      @Resource
46      private TimeManager timeManager;
47  
48      @Resource
49      private FessConfig fessConfig;
50  
51      @Resource
52      private AccessContextLogic accessContextLogic;
53  
54      @Resource
55      private ScheduledJobService scheduledJobService;
56  
57      @Resource
58      private SystemHelper systemHelper;
59  
60      @Resource
61      private JobHelper jobHelper;
62  
63      protected Class<? extends LaJob> jobClass = ScriptExecutorJob.class;
64  
65      protected long schedulerTime;
66  
67      @Override
68      public void schedule(final LaCron cron) {
69          schedulerTime = System.currentTimeMillis();
70          scheduledJobService.start(cron);
71  
72          final String myName = fessConfig.getSchedulerTargetName();
73          if (StringUtil.isNotBlank(myName)) {
74              ComponentUtil.getComponent(JobLogBhv.class).queryDelete(cb -> {
75                  cb.query().setJobStatus_Equal(Constants.RUNNING);
76                  cb.query().setTarget_Equal(myName);
77              });
78          }
79  
80          TimeoutManager.getInstance().addTimeoutTarget(() -> {
81              if (logger.isDebugEnabled()) {
82                  logger.debug("Updating scheduled jobs. time:{}", schedulerTime);
83              }
84              final long now = System.currentTimeMillis();
85              scheduledJobService.getScheduledJobListAfter(schedulerTime).forEach(scheduledJob -> {
86                  if (logger.isDebugEnabled()) {
87                      logger.debug("Updating job schedule:{}", scheduledJob.getName());
88                  }
89                  try {
90                      jobHelper.register(scheduledJob);
91                  } catch (final Exception e) {
92                      logger.warn("Failed to update schdule {}", scheduledJob, e);
93                  }
94              });
95              schedulerTime = now;
96          }, fessConfig.getSchedulerMonitorIntervalAsInteger(), true);
97      }
98  
99      @Override
100     public LaJobRunner createRunner() {
101         return new LaJobRunner().useAccessContext(
102                 resource -> accessContextLogic.create(resource, OptionalThing::empty, OptionalThing::empty, () -> APP_TYPE));
103     }
104 
105     public void setJobClass(final Class<? extends LaJob> jobClass) {
106         this.jobClass = jobClass;
107     }
108 }