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.service;
17  
18  import java.util.List;
19  
20  import javax.annotation.Resource;
21  
22  import org.codelibs.core.beans.util.BeanUtil;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.pager.JobLogPager;
25  import org.codelibs.fess.es.config.cbean.JobLogCB;
26  import org.codelibs.fess.es.config.exbhv.JobLogBhv;
27  import org.codelibs.fess.es.config.exentity.JobLog;
28  import org.codelibs.fess.mylasta.direction.FessConfig;
29  import org.codelibs.fess.util.ComponentUtil;
30  import org.dbflute.cbean.result.PagingResultBean;
31  import org.dbflute.optional.OptionalEntity;
32  
33  public class JobLogService {
34  
35      @Resource
36      protected JobLogBhv jobLogBhv;
37  
38      @Resource
39      protected FessConfig fessConfig;
40  
41      protected long expiredJobInterval = 2 * 60 * 60 * 1000L; // 2hours
42  
43      public List<JobLog> getJobLogList(final JobLogPager jobLogPager) {
44  
45          final PagingResultBean<JobLog> jobLogList = jobLogBhv.selectPage(cb -> {
46              cb.paging(jobLogPager.getPageSize(), jobLogPager.getCurrentPageNumber());
47              setupListCondition(cb, jobLogPager);
48          });
49  
50          // update pager
51          BeanUtil.copyBeanToBean(jobLogList, jobLogPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
52          jobLogPager.setPageNumberList(jobLogList.pageRange(op -> {
53              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
54          }).createPageNumberList());
55  
56          return jobLogList;
57      }
58  
59      public OptionalEntity<JobLog> getJobLog(final String id) {
60          return jobLogBhv.selectByPK(id);
61      }
62  
63      public void store(final JobLog jobLog) {
64  
65          jobLogBhv.insertOrUpdate(jobLog, op -> {
66              op.setRefreshPolicy(Constants.TRUE);
67          });
68  
69      }
70  
71      public void delete(final JobLog jobLog) {
72  
73          jobLogBhv.delete(jobLog, op -> {
74              op.setRefreshPolicy(Constants.TRUE);
75          });
76  
77      }
78  
79      protected void setupListCondition(final JobLogCB cb, final JobLogPager jobLogPager) {
80          if (jobLogPager.id != null) {
81              cb.query().docMeta().setId_Equal(jobLogPager.id);
82          }
83          // TODO Long, Integer, String supported only.
84  
85          // setup condition
86          cb.query().addOrderBy_StartTime_Desc();
87          cb.query().addOrderBy_EndTime_Desc();
88  
89          // search
90  
91      }
92  
93      public void deleteBefore(final int days) {
94          final long oneday = 24 * 60 * 60 * 1000L;
95          final long targetTime = ComponentUtil.getSystemHelper().getCurrentTimeAsLong() - days * oneday;
96          jobLogBhv.queryDelete(cb -> {
97              cb.query().setEndTime_LessThan(targetTime);
98          });
99      }
100 
101     public void deleteByJobStatus(final List<String> jobStatusList) {
102         jobLogBhv.queryDelete(cb -> {
103             cb.query().setJobStatus_InScope(jobStatusList);
104         });
105     }
106 
107     public void updateStatus() {
108         final long expiry = ComponentUtil.getSystemHelper().getCurrentTimeAsLong() - expiredJobInterval;
109         final List<JobLog> list = jobLogBhv.selectList(cb -> {
110             cb.query().bool((must, should, mustNot, filter) -> {
111                 must.setLastUpdated_LessEqual(expiry);
112                 mustNot.setEndTime_Exists();
113             });
114         });
115         if (!list.isEmpty()) {
116             list.forEach(jobLog -> {
117                 jobLog.setJobStatus(Constants.FAIL);
118                 jobLog.setScriptResult("No response from Job.");
119                 jobLog.setEndTime(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
120             });
121             jobLogBhv.batchUpdate(list);
122         }
123     }
124 
125     public void setExpiredJobInterval(final long expiredJobInterval) {
126         this.expiredJobInterval = expiredJobInterval;
127     }
128 
129 }