@@ -64,33 +64,30 @@ func checkAndEnqueueRefreshTasks(client *asynq.Client, db *gorm.DB, logger zerol
6464 Msg ("Config refresh due - checking if new restore can be created" )
6565
6666 // Check if we're already at or above max_restores limit
67- // Count restores without branches (eligible for cleanup)
6867 var totalRestores int64
6968 if err := db .Model (& models.Restore {}).Count (& totalRestores ).Error ; err != nil {
7069 logger .Error ().Err (err ).Msg ("Failed to count restores" )
7170 return
7271 }
7372
74- // Count restores with branches (protected from cleanup)
75- var restoresWithBranches int64
76- if err := db . Model ( & models. Restore {}).
77- Joins ( "JOIN branches ON branches.restore_id = restores.id" ).
78- Distinct ( "restores.id" ).
79- Count ( & restoresWithBranches ). Error ; err != nil {
80- logger . Error (). Err ( err ). Msg ( "Failed to count restores with branches" )
81- return
82- }
83-
84- // Calculate how many restores can be cleaned up
85- cleanableRestores := totalRestores - restoresWithBranches
73+ // If we're at or above max_restores, skip creating new restore
74+ // Cleanup will happen after successful restores complete
75+ if int ( totalRestores ) >= config . MaxRestores {
76+ // Count restores with branches for logging
77+ var restoresWithBranches int64
78+ if err := db . Model ( & models. Restore {}).
79+ Joins ( "JOIN branches ON branches.restore_id = restores.id" ).
80+ Distinct ( "restores.id" ).
81+ Count ( & restoresWithBranches ). Error ; err != nil {
82+ logger . Error (). Err ( err ). Msg ( "Failed to count restores with branches" )
83+ return
84+ }
8685
87- // If we're at or above max_restores and have no cleanable restores, skip
88- if int (totalRestores ) >= config .MaxRestores && cleanableRestores == 0 {
8986 logger .Warn ().
9087 Int64 ("total_restores" , totalRestores ).
9188 Int64 ("restores_with_branches" , restoresWithBranches ).
9289 Int ("max_restores" , config .MaxRestores ).
93- Msg ("Cannot create new restore - at max_restores limit and all restores have branches " )
90+ Msg ("Cannot create new restore - at max_restores limit" )
9491
9592 // Still update NextRefreshAt to prevent retrying every minute
9693 now := time .Now ()
0 commit comments