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.user.CurrentUserProvider;
031import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
032import org.ametys.plugins.core.schedule.Scheduler;
033
034/**
035 * A {@link Schedulable} job to clean caches.
036 */
037public class CleanCacheSchedulable extends AbstractStaticSchedulable
038{
039    /** The key for the cache Ids */
040    public static final String JOBDATAMAP_CACHE_IDS = "cacheIds";
041
042    /** The key for the all cache flag */
043    public static final String JOBDATAMAP_ALL_CACHES = "allCaches";
044    
045    /** The cache manager */
046    protected AbstractCacheManager _cacheManager;
047
048    private ObservationManager _observationManager;
049
050    private CurrentUserProvider _currentUserProvider;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _cacheManager = (AbstractCacheManager) manager.lookup(AbstractCacheManager.ROLE);
057        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
058        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
059    }
060    
061    @Override
062    public void execute(JobExecutionContext context) throws Exception
063    {
064        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
065        List<String> cleanedCaches;
066        
067        String[] cacheIds = (String[]) jobDataMap.get(Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_CACHE_IDS);
068        if (cacheIds.length == 0)
069        {
070            cleanedCaches = _cacheManager.resetAllMemoryCaches();
071        }
072        else
073        {
074            cleanedCaches = _cacheManager.resetCaches(List.of(cacheIds));
075        }
076
077        for (String cacheId : cleanedCaches)
078        {
079            _observationManager.notify(new Event(ObservationConstants.EVENT_CACHE_RESET, 
080                    _currentUserProvider.getUser(), 
081                    Collections.singletonMap(ObservationConstants.ARGS_CACHE_ID, cacheId)));
082            
083        }
084    }
085}