cleaner: Catch any exceptions while cleaner is running

If any exceptions are left uncaught, future scheduled tasks for the
cleaner will not run. See [1] for more details.

Also ensure we try to clean all entries by catching exceptions inside
the cleaning loop.

[1] https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html#scheduleAtFixedRate-java.lang.Runnable-long-long-java.util.concurrent.TimeUnit-

Change-Id: Ieb92488f00750375604c4a9e0e362cd1278af159
diff --git a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
index 969ccef..3167a48 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCache.java
@@ -97,12 +97,16 @@
   @Override
   public void cleanUp() {
     for (Path path : listFiles()) {
-      Val val = readFile(path);
-      if (val != null) {
-        Instant expires = Instant.ofEpochMilli(val.getExpiresAt());
-        if (expires.isBefore(TimeMachine.now())) {
-          deleteFile(path);
+      try {
+        Val val = readFile(path);
+        if (val != null) {
+          Instant expires = Instant.ofEpochMilli(val.getExpiresAt());
+          if (expires.isBefore(TimeMachine.now())) {
+            deleteFile(path);
+          }
         }
+      } catch (Exception e) {
+        log.atSevere().withCause(e).log("Exception while cleaning %s", path);
       }
     }
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCacheCleaner.java b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCacheCleaner.java
index 64d1e70..05dd408 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCacheCleaner.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/websession/flatfile/FlatFileWebSessionCacheCleaner.java
@@ -48,7 +48,13 @@
     @Override
     public void run() {
       log.atInfo().log("Cleaning up expired file based websessions...");
-      flatFileWebSessionCache.cleanUp();
+      try {
+        flatFileWebSessionCache.cleanUp();
+      } catch (Exception e) {
+        // log and do not prevent subsequent scheduled tasks from running
+        // see https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)
+        log.atSevere().withCause(e).log("Exception during cleaning sessions");
+      }
       log.atInfo().log("Cleaning up expired file based websessions...Done");
     }