001package ca.uhn.fhir.jpa.search;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.api.config.DaoConfig;
024import ca.uhn.fhir.jpa.model.sched.HapiJob;
025import ca.uhn.fhir.jpa.model.sched.ISchedulerService;
026import ca.uhn.fhir.jpa.model.sched.ScheduledJobDefinition;
027import ca.uhn.fhir.jpa.search.cache.ISearchCacheSvc;
028import org.quartz.JobExecutionContext;
029import org.springframework.beans.factory.annotation.Autowired;
030import org.springframework.transaction.annotation.Propagation;
031import org.springframework.transaction.annotation.Transactional;
032
033import javax.annotation.PostConstruct;
034
035import static ca.uhn.fhir.jpa.search.cache.DatabaseSearchCacheSvcImpl.SEARCH_CLEANUP_JOB_INTERVAL_MILLIS;
036
037/**
038 * Deletes old searches
039 */
040//
041// NOTE: This is not a @Service because we manually instantiate
042// it in BaseConfig. This is so that we can override the definition
043// in Smile.
044//
045public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc {
046        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(StaleSearchDeletingSvcImpl.class);
047        @Autowired
048        private DaoConfig myDaoConfig;
049        @Autowired
050        private ISearchCacheSvc mySearchCacheSvc;
051        @Autowired
052        private ISchedulerService mySchedulerService;
053
054        @Override
055        @Transactional(propagation = Propagation.NEVER)
056        public void pollForStaleSearchesAndDeleteThem() {
057                mySearchCacheSvc.pollForStaleSearchesAndDeleteThem();
058        }
059
060        @PostConstruct
061        public void scheduleJob() {
062                ScheduledJobDefinition jobDetail = new ScheduledJobDefinition();
063                jobDetail.setId(getClass().getName());
064                jobDetail.setJobClass(Job.class);
065                mySchedulerService.scheduleClusteredJob(SEARCH_CLEANUP_JOB_INTERVAL_MILLIS, jobDetail);
066        }
067
068        public static class Job implements HapiJob {
069                @Autowired
070                private IStaleSearchDeletingSvc myTarget;
071
072                @Override
073                public void execute(JobExecutionContext theContext) {
074                        myTarget.schedulePollForStaleSearches();
075                }
076        }
077
078        @Transactional(propagation = Propagation.NEVER)
079        @Override
080        public synchronized void schedulePollForStaleSearches() {
081                if (!myDaoConfig.isSchedulingDisabled() && myDaoConfig.isEnableTaskStaleSearchCleanup()) {
082                        pollForStaleSearchesAndDeleteThem();
083                }
084        }
085}