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.nio.file.Path;
019import java.time.Instant;
020import java.time.temporal.ChronoUnit;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.quartz.JobExecutionContext;
024
025import org.ametys.cms.schedule.AbstractDeleteFilesSchedulable;
026import org.ametys.core.schedule.Schedulable;
027import org.ametys.runtime.config.Config;
028
029/**
030 * A {@link Schedulable} job for deleting report files older than 30 minutes
031 */
032public class DeleteReportsSchedulable extends AbstractDeleteFilesSchedulable implements Initializable
033{
034    private long _longevity;
035    private Path _rootPath;
036    
037    @Override
038    public void initialize() throws Exception
039    {
040        _rootPath = ReportLocationAction.getScriptReportDirectory();
041        _longevity = Config.getInstance().getValue("org.ametys.plugin.cms.delete.reports.longevity");
042    }
043    
044    @Override
045    protected DeleteFilesConfiguration _getConfiguration(JobExecutionContext context)
046    {
047        DeleteFilesConfiguration configuration = new DeleteFilesConfiguration(_rootPath);
048        configuration.setAgeLimit(Instant.now().minus(_longevity, ChronoUnit.MINUTES));
049        return configuration;
050    }
051}