diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index b56488a..7d08097 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -111,7 +111,7 @@ public abstract class AccountingPersistenceBackendFactory { }; protected static AccountingPersistenceBackend rediscoverAccountingPersistenceBackend(AccountingPersistenceBackend actual, String scope){ - long now = Calendar.getInstance().getTimeInMillis(); + Long now = Calendar.getInstance().getTimeInMillis(); Long lastCheckTimestamp = fallbackLastCheck.get(scope); logger.trace("Last check was {}", lastCheckTimestamp); @@ -122,8 +122,13 @@ public abstract class AccountingPersistenceBackendFactory { try { lock.lock(); logger.trace("Renewing Last check Timestamp. The next one will be {}", now); + Long reCheckTimestamp = fallbackLastCheck.get(scope); + if(lastCheckTimestamp.compareTo(reCheckTimestamp)!=0){ + // Someone else arrived here before. Stopping to try to + logger.debug("Someone else arrived here before. Stopping to try to rediscover AccountingPersistenceBackend"); + return actual; + } fallbackLastCheck.put(scope, now); - lastCheckTimestamp = fallbackLastCheck.get(scope); lock.unlock(); AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope); diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendMonitor.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendMonitor.java index c4dc33d..608b74f 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendMonitor.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendMonitor.java @@ -26,6 +26,7 @@ public class AccountingPersistenceBackendMonitor implements Runnable { private final static Logger logger = LoggerFactory.getLogger(AccountingPersistenceBackendMonitorTest.class); private final static String ELABORATION_FILE_SUFFIX = ".ELABORATION"; + private final static String ELABORATION_FILE_NOT_DELETED_SUFFIX = ".ELABORATION.NOT-DELETED"; protected final ScheduledExecutorService scheduler; @@ -44,8 +45,7 @@ public class AccountingPersistenceBackendMonitor implements Runnable { UsageRecord usageRecord = BasicUsageRecord.getUsageRecord(line); accountingPersistenceBackend.accountWithFallback(usageRecord); } catch(Exception e){ - logger.error("", e); - // TODO write line on file + logger.error("Line {} will be lost", line, e); } } } catch (FileNotFoundException e) { @@ -74,6 +74,13 @@ public class AccountingPersistenceBackendMonitor implements Runnable { if(elaborationFile!=null){ elaborateFile(elaborationFile); + boolean deleted = elaborationFile.delete(); + if(!deleted){ + logger.debug("Failed to delete file {}", elaborationFile.getAbsolutePath()); + File elaborationFileNotDeleted = new File(elaborationFile.getAbsolutePath()+ELABORATION_FILE_NOT_DELETED_SUFFIX); + elaborationFile.renameTo(elaborationFileNotDeleted); + } + } }