diff --git a/CHANGELOG.md b/CHANGELOG.md index cf3a494..2752cba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed +- A scheduled daemon backup no longer stops partway when one user or organization fails with an unchecked exception (such as a `GHException` from github-api while listing repositories): the failure is logged as an error like any other, the remaining users and organizations are still backed up, and the run is still logged and reported as completed. Previously only an `IOException` was handled, so the rest of that run was skipped until the next interval - A backup that reaches the GitHub rate limit no longer looks hung: the GitHub client has always waited silently for the limit to reset, for up to an hour, and now a notice on standard error says so first, with the expected reset time, and suggests `GITHUB_TOKEN` when running anonymously. The waiting itself is unchanged and is documented in `USER_GUIDE.md` and `COMMANDS.md` - `GITHUB_TOKEN` is no longer dropped when the startup check fails for a reason unrelated to the token: only a `401` now prints `Invalid or expired GITHUB_TOKEN` and falls back to anonymous access, while any other failure, such as a network failure, a GitHub error or a `403` from `GET /user`, prints `Could not verify GITHUB_TOKEN` and keeps the token, so a brief outage at startup no longer leaves a daemon limited to 60 requests per hour until it is restarted - A GitHub failure is no longer reported as `not found as GitHub organization or user` just because its message contains `404`, as a connection failure's does for a name such as `team404`, and a real 404 is now recognized by its exception type instead of depending on the digits appearing in GitHub's response diff --git a/src/main/java/com/github/backup/ScheduledBackupService.java b/src/main/java/com/github/backup/ScheduledBackupService.java index c4ecf2c..847a828 100644 --- a/src/main/java/com/github/backup/ScheduledBackupService.java +++ b/src/main/java/com/github/backup/ScheduledBackupService.java @@ -8,7 +8,6 @@ import org.springframework.stereotype.Service; import jakarta.annotation.PostConstruct; -import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; @@ -82,7 +81,8 @@ public void runScheduledBackup() { for (String userOrOrg : scheduledUsers) { try { backupService.backupUserRepositories(userOrOrg); - } catch (IOException e) { + } catch (Exception e) { + // Any failure, checked or not, is this user's alone; the others still run log.error("Error backing up {}: {}", userOrOrg, e.getMessage()); } } diff --git a/src/test/java/com/github/backup/ScheduledBackupServiceTest.java b/src/test/java/com/github/backup/ScheduledBackupServiceTest.java index b6ef326..34bd6d0 100644 --- a/src/test/java/com/github/backup/ScheduledBackupServiceTest.java +++ b/src/test/java/com/github/backup/ScheduledBackupServiceTest.java @@ -234,6 +234,28 @@ void testRunScheduledBackup_LogsErrorForFailedUser() throws IOException { verify(backupService, times(1)).backupUserRepositories("github"); } + @Test + void testRunScheduledBackup_UncheckedExceptionDoesNotStopRemainingUsers() throws IOException { + scheduledBackupService = new ScheduledBackupService(backupService, usageReporting, "octocat,github", DEFAULT_INTERVAL); + + doThrow(new IllegalStateException("Listing interrupted")).when(backupService).backupUserRepositories("octocat"); + + assertDoesNotThrow(() -> scheduledBackupService.runScheduledBackup()); + + // The failure is logged like any other, and the rest of the run still happens + List logsList = logAppender.list; + assertTrue(logsList.stream().anyMatch(event -> + event.getLevel() == Level.ERROR && + event.getFormattedMessage().contains("Error backing up octocat") && + event.getFormattedMessage().contains("Listing interrupted")), + "Should log error with user name and error message"); + assertTrue(logsList.stream().anyMatch(event -> + event.getFormattedMessage().contains("Scheduled backup completed")), + "Should log when backup completes"); + verify(backupService, times(1)).backupUserRepositories("github"); + verify(usageReporting, times(1)).backupCompleted(); + } + @Test void testRunScheduledBackup_WithNoUsers_NoLogsGenerated() { scheduledBackupService = new ScheduledBackupService(backupService, usageReporting, "", DEFAULT_INTERVAL);