001/*
002 *  Copyright 2019 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.cms.scripts;
017
018import java.io.File;
019import java.nio.file.Files;
020import java.nio.file.Path;
021import java.time.Instant;
022import java.util.List;
023import java.util.function.Predicate;
024import java.util.stream.Collectors;
025
026import org.quartz.JobExecutionContext;
027
028import org.ametys.core.schedule.Schedulable;
029import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
030import org.ametys.runtime.config.Config;
031
032/**
033 * A {@link Schedulable} job for deleting report files older than 30 minutes
034 */
035public class DeleteReportsSchedulable extends AbstractStaticSchedulable
036{
037    @Override
038    public void execute(JobExecutionContext context) throws Exception
039    {
040        Path scriptReportTempPath = ReportLocationAction.getScriptReportDirectory();
041        
042        List<Path> filesPath = Files.walk(scriptReportTempPath, 3)
043                .filter(Predicate.not(Files::isDirectory)).collect(Collectors.toList());
044        
045        filesPath.forEach(filePath ->
046        {
047            File file = filePath.toFile();
048            Long longevity = Config.getInstance().getValue("org.ametys.plugin.cms.delete.reports.longevity");
049            if (Instant.now().toEpochMilli() - file.lastModified() > longevity * 60 * 1000)
050            {
051                file.delete();
052            }
053        });
054    }
055}