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