Skip to content

Commit aad1016

Browse files
FEATURE (healthcheck): Add background service to healthcheck
1 parent a94a4d2 commit aad1016

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

backend/internal/features/backups/background_service.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ type BackupBackgroundService struct {
1111
backupService *BackupService
1212
backupRepository *BackupRepository
1313
databaseService *databases.DatabaseService
14+
15+
lastBackupTime time.Time
1416
}
1517

1618
var log = logger.GetLogger()
1719

1820
func (s *BackupBackgroundService) Run() {
21+
s.lastBackupTime = time.Now().UTC()
22+
1923
if err := s.failBackupsInProgress(); err != nil {
2024
log.Error("Failed to fail backups in progress", "error", err)
2125
panic(err)
@@ -38,10 +42,16 @@ func (s *BackupBackgroundService) Run() {
3842
log.Error("Failed to run pending backups", "error", err)
3943
}
4044

45+
s.lastBackupTime = time.Now().UTC()
4146
time.Sleep(1 * time.Minute)
4247
}
4348
}
4449

50+
func (s *BackupBackgroundService) IsBackupsRunning() bool {
51+
// if last backup time is more than 5 minutes ago, return false
52+
return s.lastBackupTime.After(time.Now().UTC().Add(-5 * time.Minute))
53+
}
54+
4555
func (s *BackupBackgroundService) failBackupsInProgress() error {
4656
backupsInProgress, err := s.backupRepository.FindByStatus(BackupStatusInProgress)
4757
if err != nil {

backend/internal/features/backups/di.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"postgresus-backend/internal/features/notifiers"
88
"postgresus-backend/internal/features/storages"
99
"postgresus-backend/internal/features/users"
10+
"time"
1011
)
1112

1213
var createPostgresqlBackupUsecase = &usecases_postgresql.CreatePostgresqlBackupUsecase{}
@@ -26,6 +27,7 @@ var backupBackgroundService = &BackupBackgroundService{
2627
backupService,
2728
backupRepository,
2829
databases.GetDatabaseService(),
30+
time.Now().UTC(),
2931
}
3032

3133
var backupController = &BackupController{

backend/internal/features/healthcheck/di.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package healthcheck
22

3-
import "postgresus-backend/internal/features/disk"
4-
5-
var (
6-
healthcheckService *HealthcheckService
7-
healthcheckController *HealthcheckController
3+
import (
4+
"postgresus-backend/internal/features/backups"
5+
"postgresus-backend/internal/features/disk"
86
)
97

10-
func init() {
11-
healthcheckService = &HealthcheckService{
12-
disk.GetDiskService(),
13-
}
14-
15-
healthcheckController = &HealthcheckController{
16-
healthcheckService,
17-
}
8+
var healthcheckService = &HealthcheckService{
9+
disk.GetDiskService(),
10+
backups.GetBackupBackgroundService(),
11+
}
12+
var healthcheckController = &HealthcheckController{
13+
healthcheckService,
1814
}
1915

2016
func GetHealthcheckController() *HealthcheckController {

backend/internal/features/healthcheck/service.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package healthcheck
22

33
import (
44
"errors"
5+
"postgresus-backend/internal/features/backups"
56
"postgresus-backend/internal/features/disk"
67
"postgresus-backend/internal/storage"
78
)
89

910
type HealthcheckService struct {
10-
diskService *disk.DiskService
11+
diskService *disk.DiskService
12+
backupBackgroundService *backups.BackupBackgroundService
1113
}
1214

1315
func (s *HealthcheckService) IsHealthy() error {
@@ -27,5 +29,9 @@ func (s *HealthcheckService) IsHealthy() error {
2729
return errors.New("cannot connect to the database")
2830
}
2931

32+
if !s.backupBackgroundService.IsBackupsRunning() {
33+
return errors.New("backups are not running for more than 5 minutes")
34+
}
35+
3036
return nil
3137
}

0 commit comments

Comments
 (0)