View Javadoc
1   /*
2    * Copyright 2012-2025 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.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.core.timer.TimeoutManager;
22  import org.codelibs.fess.Constants;
23  import org.codelibs.fess.app.logic.AccessContextLogic;
24  import org.codelibs.fess.app.service.ScheduledJobService;
25  import org.codelibs.fess.helper.JobHelper;
26  import org.codelibs.fess.helper.SystemHelper;
27  import org.codelibs.fess.mylasta.direction.FessConfig;
28  import org.codelibs.fess.opensearch.config.exbhv.JobLogBhv;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.dbflute.optional.OptionalThing;
31  import org.lastaflute.core.time.TimeManager;
32  import org.lastaflute.job.LaCron;
33  import org.lastaflute.job.LaJob;
34  import org.lastaflute.job.LaJobRunner;
35  import org.lastaflute.job.LaJobScheduler;
36  
37  import jakarta.annotation.Resource;
38  
39  /**
40   * Job scheduler for managing all scheduled jobs in Fess.
41   * Implements LaJobScheduler to handle job scheduling and execution.
42   */
43  public class AllJobScheduler implements LaJobScheduler {
44  
45      private static final Logger logger = LogManager.getLogger(AllJobScheduler.class);
46  
47      /** Application type identifier for job context */
48      protected static final String APP_TYPE = "JOB";
49  
50      /**
51       * Default constructor.
52       */
53      public AllJobScheduler() {
54          // Default constructor
55      }
56  
57      @Resource
58      private TimeManager timeManager;
59  
60      @Resource
61      private FessConfig fessConfig;
62  
63      @Resource
64      private AccessContextLogic accessContextLogic;
65  
66      @Resource
67      private ScheduledJobService scheduledJobService;
68  
69      @Resource
70      private SystemHelper systemHelper;
71  
72      @Resource
73      private JobHelper jobHelper;
74  
75      /** The job class to be executed by this scheduler */
76      protected Class<? extends LaJob> jobClass = ScriptExecutorJob.class;
77  
78      /** The timestamp when the scheduler was last updated */
79      protected long schedulerTime;
80  
81      @Override
82      public void schedule(final LaCron cron) {
83          schedulerTime = systemHelper.getCurrentTimeAsLong();
84          scheduledJobService.start(cron);
85  
86          final String myName = fessConfig.getSchedulerTargetName();
87          if (StringUtil.isNotBlank(myName)) {
88              ComponentUtil.getComponent(JobLogBhv.class).queryDelete(cb -> {
89                  cb.query().setJobStatus_Equal(Constants.RUNNING);
90                  cb.query().setTarget_Equal(myName);
91              });
92          }
93  
94          TimeoutManager.getInstance().addTimeoutTarget(() -> {
95              if (logger.isDebugEnabled()) {
96                  logger.debug("Updating scheduled jobs: time={}", schedulerTime);
97              }
98              final long now = systemHelper.getCurrentTimeAsLong();
99              scheduledJobService.getScheduledJobListAfter(schedulerTime).forEach(scheduledJob -> {
100                 if (logger.isDebugEnabled()) {
101                     logger.debug("Updating job schedule: name={}", scheduledJob.getName());
102                 }
103                 try {
104                     jobHelper.register(scheduledJob);
105                 } catch (final Exception e) {
106                     logger.warn("Failed to update schedule: job={}", scheduledJob, e);
107                 }
108             });
109             schedulerTime = now;
110         }, fessConfig.getSchedulerMonitorIntervalAsInteger(), true);
111     }
112 
113     @Override
114     public LaJobRunner createRunner() {
115         return new LaJobRunner().useAccessContext(
116                 resource -> accessContextLogic.create(resource, OptionalThing::empty, OptionalThing::empty, () -> APP_TYPE));
117     }
118 
119     /**
120      * Sets the job class to be executed by this scheduler.
121      *
122      * @param jobClass the job class to set
123      */
124     public void setJobClass(final Class<? extends LaJob> jobClass) {
125         this.jobClass = jobClass;
126     }
127 }