001/* 002 * Copyright 2021 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.cache; 017 018import java.util.Collections; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.quartz.JobDataMap; 024import org.quartz.JobExecutionContext; 025 026import org.ametys.core.ObservationConstants; 027import org.ametys.core.observation.Event; 028import org.ametys.core.observation.ObservationManager; 029import org.ametys.core.schedule.Schedulable; 030import org.ametys.core.schedule.progression.ContainerProgressionTracker; 031import org.ametys.core.user.CurrentUserProvider; 032import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable; 033import org.ametys.plugins.core.schedule.Scheduler; 034 035/** 036 * A {@link Schedulable} job to clean caches. 037 */ 038public class CleanCacheSchedulable extends AbstractStaticSchedulable 039{ 040 /** The key for the cache Ids */ 041 public static final String JOBDATAMAP_CACHE_IDS = "cacheIds"; 042 043 /** The key for the all cache flag */ 044 public static final String JOBDATAMAP_ALL_CACHES = "allCaches"; 045 046 /** The cache manager */ 047 protected AbstractCacheManager _cacheManager; 048 049 private ObservationManager _observationManager; 050 051 private CurrentUserProvider _currentUserProvider; 052 053 @Override 054 public void service(ServiceManager manager) throws ServiceException 055 { 056 super.service(manager); 057 _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE); 058 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 059 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 060 } 061 062 @Override 063 public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 064 { 065 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 066 List<String> cleanedCaches; 067 068 String[] cacheIds = (String[]) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_CACHE_IDS); 069 if (cacheIds.length == 0) 070 { 071 cleanedCaches = _cacheManager.resetAllMemoryCaches(); 072 } 073 else 074 { 075 cleanedCaches = _cacheManager.resetCaches(List.of(cacheIds)); 076 } 077 078 for (String cacheId : cleanedCaches) 079 { 080 _observationManager.notify(new Event(ObservationConstants.EVENT_CACHE_RESET, 081 _currentUserProvider.getUser(), 082 Collections.singletonMap(ObservationConstants.ARGS_CACHE_ID, cacheId))); 083 084 } 085 } 086}