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.crawler.interval;
17  
18  import org.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.codelibs.fess.crawler.interval.impl.DefaultIntervalController;
21  import org.codelibs.fess.helper.IntervalControlHelper;
22  import org.codelibs.fess.util.ComponentUtil;
23  
24  /**
25   * FessIntervalController extends DefaultIntervalController to provide
26   * Fess-specific interval control functionality for web crawling operations.
27   * This controller manages delays and timing for various crawling states
28   * including processing delays, queue waiting times, and new URL discovery.
29   */
30  public class FessIntervalController extends DefaultIntervalController {
31  
32      private static final Logger logger = LogManager.getLogger(FessIntervalController.class);
33  
34      /**
35       * Default constructor.
36       */
37      public FessIntervalController() {
38          super();
39      }
40  
41      /**
42       * Gets the delay time in milliseconds after processing a URL.
43       *
44       * @return the delay time in milliseconds after processing
45       */
46      public long getDelayMillisAfterProcessing() {
47          return delayMillisAfterProcessing;
48      }
49  
50      /**
51       * Sets the delay time in milliseconds after processing a URL.
52       *
53       * @param delayMillisAfterProcessing the delay time in milliseconds after processing
54       */
55      public void setDelayMillisAfterProcessing(final long delayMillisAfterProcessing) {
56          this.delayMillisAfterProcessing = delayMillisAfterProcessing;
57      }
58  
59      /**
60       * Gets the delay time in milliseconds when there are no URLs in the queue.
61       *
62       * @return the delay time in milliseconds when no URLs are available
63       */
64      public long getDelayMillisAtNoUrlInQueue() {
65          return delayMillisAtNoUrlInQueue;
66      }
67  
68      /**
69       * Sets the delay time in milliseconds when there are no URLs in the queue.
70       *
71       * @param delayMillisAtNoUrlInQueue the delay time in milliseconds when no URLs are available
72       */
73      public void setDelayMillisAtNoUrlInQueue(final long delayMillisAtNoUrlInQueue) {
74          this.delayMillisAtNoUrlInQueue = delayMillisAtNoUrlInQueue;
75      }
76  
77      /**
78       * Gets the delay time in milliseconds before processing a URL.
79       *
80       * @return the delay time in milliseconds before processing
81       */
82      public long getDelayMillisBeforeProcessing() {
83          return delayMillisBeforeProcessing;
84      }
85  
86      /**
87       * Sets the delay time in milliseconds before processing a URL.
88       *
89       * @param delayMillisBeforeProcessing the delay time in milliseconds before processing
90       */
91      public void setDelayMillisBeforeProcessing(final long delayMillisBeforeProcessing) {
92          this.delayMillisBeforeProcessing = delayMillisBeforeProcessing;
93      }
94  
95      /**
96       * Gets the delay time in milliseconds for waiting for new URLs.
97       *
98       * @return the delay time in milliseconds for waiting for new URLs
99       */
100     public long getDelayMillisForWaitingNewUrl() {
101         return delayMillisForWaitingNewUrl;
102     }
103 
104     /**
105      * Sets the delay time in milliseconds for waiting for new URLs.
106      *
107      * @param delayMillisForWaitingNewUrl the delay time in milliseconds for waiting for new URLs
108      */
109     public void setDelayMillisForWaitingNewUrl(final long delayMillisForWaitingNewUrl) {
110         this.delayMillisForWaitingNewUrl = delayMillisForWaitingNewUrl;
111     }
112 
113     /**
114      * Delays the crawler while waiting for new URLs to be available.
115      * This method calibrates CPU load, checks crawler status, applies
116      * interval control rules, and then calls the parent implementation.
117      * All operations are wrapped in exception handling to ensure robustness
118      * in test and production environments.
119      */
120     @Override
121     protected void delayForWaitingNewUrl() {
122         try {
123             ComponentUtil.getSystemHelper().calibrateCpuLoad();
124         } catch (final Exception e) {
125             if (logger.isDebugEnabled()) {
126                 logger.debug("Failed to calibrate CPU load", e);
127             }
128         }
129 
130         try {
131             final IntervalControlHelper intervalControlHelper = ComponentUtil.getIntervalControlHelper();
132             intervalControlHelper.checkCrawlerStatus();
133             intervalControlHelper.delayByRules();
134         } catch (final Exception e) {
135             if (logger.isDebugEnabled()) {
136                 logger.debug("Failed to apply interval control rules", e);
137             }
138         }
139 
140         try {
141             super.delayForWaitingNewUrl();
142         } catch (final Exception e) {
143             if (logger.isDebugEnabled()) {
144                 logger.debug("Failed to execute parent delay logic", e);
145             }
146         }
147     }
148 }