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.plugins.contentio.archive;
017
018import java.io.File;
019import java.nio.file.Path;
020import java.time.ZonedDateTime;
021import java.time.format.DateTimeFormatter;
022
023import org.apache.commons.io.FileUtils;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.core.schedule.progression.ContainerProgressionTracker;
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * Job for archiving data on disk, for backup or other purposes.
031 */
032public class ExportArchiveSchedulable extends AbstractArchiveSchedulable
033{
034    @Override
035    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
036    {
037        String userEmail = _getUserEmail();
038        File output = _createFileOutput();
039        
040        getLogger().info("Exporting archive {} ...", output.getAbsolutePath());
041        long t0 = System.currentTimeMillis();
042        
043        try
044        {
045            _archiveHandler.export(output);
046            
047            getLogger().info("Archive {} exported without error in {} ms", output.getAbsolutePath(), System.currentTimeMillis() - t0);
048            
049            // Once finished with archive, inform user
050            String subject = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAIL_SUBJECT")); 
051            String body = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAIL_BODY"));
052            
053            _sendMail(subject, body + " " + output.getCanonicalPath(), userEmail);
054        }
055        catch (Exception e)
056        {
057            FileUtils.deleteQuietly(output);
058            
059            // Archiving encountered an error during execution, send error mail
060            String subject = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAILERROR_SUBJECT")); 
061            String body = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAILERROR_BODY"));
062            
063            _sendMail(subject, body, userEmail);
064            
065            // rethrow to ensure the job is marked as failed
066            throw e;
067        }
068    }
069    
070    private File _createFileOutput()
071    {
072        String timestamp = DateTimeFormatter.ofPattern("uuuuMMddHHmmss").format(ZonedDateTime.now());
073        Path parent = _archiveHandler.getArchiveFolder();
074        File output = parent.resolve("archive-" + timestamp + ".zip").toFile();
075        return output;
076    }
077}