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