Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (healthcheck): Add background service to healthcheck

Rostislav Duginaad101606479
diff --git a/backend/internal/features/backups/background_service.go b/backend/internal/features/backups/background_service.go
index 9bc2545b..7d17fce7 100644
--- a/backend/internal/features/backups/background_service.go
+++ b/backend/internal/features/backups/background_service.go
@@ -11,11 +11,15 @@ type BackupBackgroundService struct {
backupService *BackupService
backupRepository *BackupRepository
databaseService *databases.DatabaseService
+
+ lastBackupTime time.Time
}
var log = logger.GetLogger()
func (s *BackupBackgroundService) Run() {
+ s.lastBackupTime = time.Now().UTC()
+
if err := s.failBackupsInProgress(); err != nil {
log.Error("Failed to fail backups in progress", "error", err)
panic(err)
@@ -38,10 +42,16 @@ func (s *BackupBackgroundService) Run() {
log.Error("Failed to run pending backups", "error", err)
}
+ s.lastBackupTime = time.Now().UTC()
time.Sleep(1 * time.Minute)
}
}
+func (s *BackupBackgroundService) IsBackupsRunning() bool {
+ // if last backup time is more than 5 minutes ago, return false
+ return s.lastBackupTime.After(time.Now().UTC().Add(-5 * time.Minute))
+}
+
func (s *BackupBackgroundService) failBackupsInProgress() error {
backupsInProgress, err := s.backupRepository.FindByStatus(BackupStatusInProgress)
if err != nil {
diff --git a/backend/internal/features/backups/di.go b/backend/internal/features/backups/di.go
index 1c1d3b5f..2f071542 100644
--- a/backend/internal/features/backups/di.go
+++ b/backend/internal/features/backups/di.go
@@ -7,6 +7,7 @@ import (
"postgresus-backend/internal/features/notifiers"
"postgresus-backend/internal/features/storages"
"postgresus-backend/internal/features/users"
+ "time"
)
var createPostgresqlBackupUsecase = &usecases_postgresql.CreatePostgresqlBackupUsecase{}
@@ -26,6 +27,7 @@ var backupBackgroundService = &BackupBackgroundService{
backupService,
backupRepository,
databases.GetDatabaseService(),
+ time.Now().UTC(),
}
var backupController = &BackupController{
diff --git a/backend/internal/features/healthcheck/di.go b/backend/internal/features/healthcheck/di.go
index e5c0dd58..bcf46b75 100644
--- a/backend/internal/features/healthcheck/di.go
+++ b/backend/internal/features/healthcheck/di.go
@@ -1,20 +1,16 @@
package healthcheck
-import "postgresus-backend/internal/features/disk"
-
-var (
- healthcheckService *HealthcheckService
- healthcheckController *HealthcheckController
+import (
+ "postgresus-backend/internal/features/backups"
+ "postgresus-backend/internal/features/disk"
)
-func init() {
- healthcheckService = &HealthcheckService{
- disk.GetDiskService(),
- }
-
- healthcheckController = &HealthcheckController{
- healthcheckService,
- }
+var healthcheckService = &HealthcheckService{
+ disk.GetDiskService(),
+ backups.GetBackupBackgroundService(),
+}
+var healthcheckController = &HealthcheckController{
+ healthcheckService,
}
func GetHealthcheckController() *HealthcheckController {
diff --git a/backend/internal/features/healthcheck/service.go b/backend/internal/features/healthcheck/service.go
index 83a5b919..0ea43a3b 100644
--- a/backend/internal/features/healthcheck/service.go
+++ b/backend/internal/features/healthcheck/service.go
@@ -2,12 +2,14 @@ package healthcheck
import (
"errors"
+ "postgresus-backend/internal/features/backups"
"postgresus-backend/internal/features/disk"
"postgresus-backend/internal/storage"
)
type HealthcheckService struct {
- diskService *disk.DiskService
+ diskService *disk.DiskService
+ backupBackgroundService *backups.BackupBackgroundService
}
func (s *HealthcheckService) IsHealthy() error {
@@ -27,5 +29,9 @@ func (s *HealthcheckService) IsHealthy() error {
return errors.New("cannot connect to the database")
}
+ if !s.backupBackgroundService.IsBackupsRunning() {
+ return errors.New("backups are not running for more than 5 minutes")
+ }
+
return nil
}