mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-05-18 02:33:25 -05:00
Update backups service to integrate with cron scheduler
This commit is contained in:
+2
-2
@@ -13,7 +13,7 @@ import (
|
||||
func main() {
|
||||
env := config.GetEnv()
|
||||
|
||||
_, err := cron.New()
|
||||
cr, err := cron.New()
|
||||
if err != nil {
|
||||
logger.FatalError("error initializing cron scheduler", logger.KV{"error": err})
|
||||
}
|
||||
@@ -23,5 +23,5 @@ func main() {
|
||||
dbgen := dbgen.New(db)
|
||||
|
||||
ints := integration.New()
|
||||
_ = service.New(env, dbgen, ints)
|
||||
_ = service.New(env, dbgen, cr, ints)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,25 @@
|
||||
package backups
|
||||
|
||||
import "github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
import (
|
||||
"github.com/eduardolat/pgbackweb/internal/cron"
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
"github.com/eduardolat/pgbackweb/internal/service/executions"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
dbgen *dbgen.Queries
|
||||
dbgen *dbgen.Queries
|
||||
cr *cron.Cron
|
||||
executionsService *executions.Service
|
||||
}
|
||||
|
||||
func New(dbgen *dbgen.Queries) *Service {
|
||||
func New(
|
||||
dbgen *dbgen.Queries,
|
||||
cr *cron.Cron,
|
||||
executionsService *executions.Service,
|
||||
) *Service {
|
||||
return &Service{
|
||||
dbgen: dbgen,
|
||||
dbgen: dbgen,
|
||||
cr: cr,
|
||||
executionsService: executionsService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,5 +9,14 @@ import (
|
||||
func (s *Service) CreateBackup(
|
||||
ctx context.Context, params dbgen.BackupsServiceCreateBackupParams,
|
||||
) (dbgen.Backup, error) {
|
||||
return s.dbgen.BackupsServiceCreateBackup(ctx, params)
|
||||
backup, err := s.dbgen.BackupsServiceCreateBackup(ctx, params)
|
||||
if err != nil {
|
||||
return backup, err
|
||||
}
|
||||
|
||||
if !backup.IsActive {
|
||||
return backup, s.jobRemove(backup.ID)
|
||||
}
|
||||
|
||||
return backup, s.jobUpsert(backup.ID, backup.TimeZone, backup.CronExpression)
|
||||
}
|
||||
|
||||
@@ -9,5 +9,10 @@ import (
|
||||
func (s *Service) DeleteBackup(
|
||||
ctx context.Context, id uuid.UUID,
|
||||
) error {
|
||||
err := s.jobRemove(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.dbgen.BackupsServiceDeleteBackup(ctx, id)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package backups
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
func (s *Service) jobRemove(backupID uuid.UUID) error {
|
||||
return s.cr.RemoveJob(backupID)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package backups
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) jobUpsert(
|
||||
backupID uuid.UUID, timeZone string, cronExpression string,
|
||||
) error {
|
||||
return s.cr.UpsertJob(
|
||||
backupID, timeZone, cronExpression,
|
||||
s.executionsService.RunExecution, context.Background(), backupID,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package backups
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) ToggleIsActive(ctx context.Context, backupID uuid.UUID) error {
|
||||
backup, err := s.dbgen.BackupsServiceToggleIsActive(ctx, backupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !backup.IsActive {
|
||||
return s.jobRemove(backupID)
|
||||
}
|
||||
|
||||
return s.jobUpsert(backupID, backup.TimeZone, backup.CronExpression)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
-- name: BackupsServiceToggleIsActive :one
|
||||
UPDATE backups
|
||||
SET is_active = NOT is_active
|
||||
WHERE id = @backup_id
|
||||
RETURNING *;
|
||||
@@ -9,5 +9,14 @@ import (
|
||||
func (s *Service) UpdateBackup(
|
||||
ctx context.Context, params dbgen.BackupsServiceUpdateBackupParams,
|
||||
) (dbgen.Backup, error) {
|
||||
return s.dbgen.BackupsServiceUpdateBackup(ctx, params)
|
||||
backup, err := s.dbgen.BackupsServiceUpdateBackup(ctx, params)
|
||||
if err != nil {
|
||||
return backup, err
|
||||
}
|
||||
|
||||
if !backup.IsActive {
|
||||
return backup, s.jobRemove(backup.ID)
|
||||
}
|
||||
|
||||
return backup, s.jobUpsert(backup.ID, backup.TimeZone, backup.CronExpression)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"github.com/eduardolat/pgbackweb/internal/config"
|
||||
"github.com/eduardolat/pgbackweb/internal/cron"
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
"github.com/eduardolat/pgbackweb/internal/integration"
|
||||
"github.com/eduardolat/pgbackweb/internal/service/auth"
|
||||
@@ -22,14 +23,15 @@ type Service struct {
|
||||
}
|
||||
|
||||
func New(
|
||||
env *config.Env, dbgen *dbgen.Queries, ints *integration.Integration,
|
||||
env *config.Env, dbgen *dbgen.Queries,
|
||||
cr *cron.Cron, ints *integration.Integration,
|
||||
) *Service {
|
||||
authService := auth.New(env, dbgen)
|
||||
backupsService := backups.New(dbgen)
|
||||
databasesService := databases.New(env, dbgen)
|
||||
destinationsService := destinations.New(env, dbgen)
|
||||
executionsService := executions.New(env, dbgen, ints)
|
||||
usersService := users.New(dbgen)
|
||||
backupsService := backups.New(dbgen, cr, executionsService)
|
||||
|
||||
return &Service{
|
||||
AuthService: authService,
|
||||
|
||||
Reference in New Issue
Block a user