package org.exist.storage.sync;

import java.io.File;
import java.util.Properties;

import org.exist.EXistException;
import org.exist.storage.BrokerPool;
import org.exist.storage.DBBroker;
import org.exist.storage.SystemTask;
import org.exist.util.Configuration;

public class SyncTask implements SystemTask {

    private final static String JOB_GROUP = "eXist.internal";
    private final static String JOB_NAME = "Sync";

    public static String getJobName() {
        return JOB_NAME;
    }

    public static String getJobGroup() {
        return JOB_GROUP;
    }

    private File partition;
    private long diskSpaceMin = 64 * 1024L * 1024L;

    @Override
    public boolean afterCheckpoint() {
        // a checkpoint is created by the MAJOR_SYNC event
        return false;
    }

    @Override
    public void configure(Configuration config, Properties properties)
            throws EXistException {
        Integer min = (Integer) config.getProperty(BrokerPool.DISK_SPACE_MIN_PROPERTY);
        if (min != null)
            diskSpaceMin = min * 1024L * 1024L;

        File homeDir = config.getExistHome();
        partition = homeDir;
        File parent = homeDir.getParentFile();
        while (parent != null) {
            partition = parent;
            parent = parent.getParentFile();
        }
        LOG.info("Using Partition: " + partition.getAbsolutePath() + ". Minimal disk space required for database " +
            "to continue operations: " + diskSpaceMin);
    }

    @Override
    public void execute(DBBroker broker) throws EXistException {
        BrokerPool pool = broker.getBrokerPool();
        if (!checkDiskSpace(pool)) {
            LOG.fatal("Partition " + partition.getAbsolutePath() + " is running out of disk space. " +
                "Switching eXist-db to read only to prevent data loss!");
            pool.setReadOnly();
        }
        if(System.currentTimeMillis() - pool.getLastMajorSync() >
                pool.getMajorSyncPeriod()) {
            pool.sync(broker, Sync.MAJOR_SYNC);
        } else {
            pool.sync(broker, Sync.MINOR_SYNC);
        }
    }

    private boolean checkDiskSpace(BrokerPool pool) {
        long space = partition.getUsableSpace();
        //LOG.info("Usable space on partition " + partition.getAbsolutePath() + ": " + (space / 1024 / 1024) + "mb");
        return space > diskSpaceMin;
    }
}
@@ -1,8 +1,885 @@
+/*%0A * eXist Open Source Native XML Database%0A * Copyright (C) 2011-2013 The eXist-db Project%0A * http://exist-db.org%0A *%0A * This program is free software; you can redistribute it and/or%0A * modify it under the terms of the GNU Lesser General Public License%0A * as published by the Free Software Foundation; either version 2%0A * of the License, or (at your option) any later version.%0A *  %0A * This program is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Lesser General Public License for more details.%0A * %0A * You should have received a copy of the GNU Lesser General Public License%0A * along with this program; if not, write to the Free Software Foundation%0A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.%0A *  %0A *  $Id$%0A */%0A
 package 
@@ -1458,25 +1458,23 @@
 te File 
-partition
+dataDir
 ;%0A    pr
@@ -1956,241 +1956,196 @@
     
-File homeDir = config.getExistHome();%0A        partition = homeDir;%0A        File parent = homeDir.getParentFile();%0A        while (parent != null) %7B%0A            partition = parent;%0A            parent = parent.getParentFile();%0A        %7D
+// fixme! - Shouldn't it be data dir AND journal dir we check%0A        // rather than EXIST_HOME? /ljo%0A        dataDir = new File((String) config.getProperty(BrokerPool.PROPERTY_DATA_DIR));
 %0A   
@@ -2169,32 +2169,29 @@
 ing 
-Partition: %22 + partition
+DATA_DIR: %22 + dataDir
 .get
@@ -2267,16 +2267,21 @@
         
+     
 %22to cont
@@ -2301,16 +2301,17 @@
 ns: %22 + 
+(
 diskSpac
@@ -2314,16 +2314,220 @@
 SpaceMin
+ / 1024 / 1024) + %22mb%22);%0A        long space = dataDir.getUsableSpace();%0A        LOG.info(%22Usable space on partition containing DATA_DIR: %22 + dataDir.getAbsolutePath() + %22: %22 + (space / 1024 / 1024) + %22mb%22
 );%0A    %7D
@@ -2723,37 +2723,56 @@
 (%22Partition 
-%22 + partition
+containing DATA_DIR: %22 + dataDir
 .getAbsolute
@@ -3248,25 +3248,23 @@
 space = 
-partition
+dataDir
 .getUsab
@@ -3324,21 +3324,40 @@
 ion 
-%22 + partition
+containing DATA_DIR: %22 + dataDir
 .get
@@ -3454,9 +3454,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Check free space on partition containing DATA_DIR instead of EXIST_HOME ancestor root partition. Should probably also check the one containing JOURNAL_DIR.