Skip to content

Commit 844c7d3

Browse files
committed
Added scheduled sync implementation
1 parent b6622d4 commit 844c7d3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

internal/server/sync.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ func (s *Server) runSyncJobs() error {
162162
continue
163163
}
164164

165+
if !entry.Status.LastExecutionTime.IsZero() && entry.Status.LastExecutionTime.Add(time.Duration(entry.Metadata.ScheduleFrequency)*time.Minute).After(time.Now()) {
166+
s.Trace().Msgf("Sync job %s not ready to run", entry.Id)
167+
continue
168+
}
169+
170+
if entry.Status.FailureCount >= s.config.System.MaxSyncFailureCount {
171+
s.Trace().Msgf("Sync job %s has failed too many times, skipping", entry.Id)
172+
continue
173+
}
174+
165175
_, _, err = s.runSyncJob(ctx, types.Transaction{}, entry, true, repoCache) // each sync runs in its own transaction
166176
if err != nil {
167177
s.Error().Err(err).Msgf("Error running sync job %s", entry.Id)
@@ -172,7 +182,8 @@ func (s *Server) runSyncJobs() error {
172182
return nil
173183
}
174184

175-
func (s *Server) runSyncJob(ctx context.Context, inputTx types.Transaction, entry *types.SyncEntry, checkCommitHash bool, repoCache *RepoCache) (*types.SyncJobStatus, []types.AppPathDomain, error) {
185+
func (s *Server) runSyncJob(ctx context.Context, inputTx types.Transaction, entry *types.SyncEntry,
186+
checkCommitHash bool, repoCache *RepoCache) (*types.SyncJobStatus, []types.AppPathDomain, error) {
176187
var tx types.Transaction
177188
var err error
178189
if inputTx.Tx == nil {
@@ -186,6 +197,7 @@ func (s *Server) runSyncJob(ctx context.Context, inputTx types.Transaction, entr
186197
// No rollback here if transaction is passed in
187198
}
188199

200+
s.Debug().Msgf("Running sync job %s", entry.Id)
189201
if repoCache == nil {
190202
// Create a new repo cache if not passed in
191203
repoCache, err = NewRepoCache(s)
@@ -213,8 +225,10 @@ func (s *Server) runSyncJob(ctx context.Context, inputTx types.Transaction, entr
213225
status.Error = applyErr.Error()
214226
applyInfo = &types.AppApplyResponse{}
215227
applyInfo.FilteredApps = lastRunApps
228+
status.FailureCount++
216229
} else {
217230
status.CommitId = applyInfo.CommitId
231+
status.FailureCount = 0
218232
}
219233

220234
status.ApplyResponse = *applyInfo
@@ -270,8 +284,8 @@ func (s *Server) runSyncJob(ctx context.Context, inputTx types.Transaction, entr
270284
status.ApplyResponse.PromoteResults = promoteResults
271285
}
272286
}
273-
status.ApplyResponse = *applyInfo
274287

288+
status.ApplyResponse = *applyInfo
275289
err = s.db.UpdateSyncStatus(ctx, tx, entry.Id, &status)
276290
if err != nil {
277291
return nil, nil, err

internal/system/clace.default.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ default_domain = "localhost" # default domain for apps
6464
root_serve_list_apps = "auto" # "auto" means serve list_apps app for default domain, "disable" means don't server for any domain,
6565
enable_compression = false # enable compression for HTTP responses
6666
default_schedule_mins = 5 # default sync schedule interval in minutes
67+
max_sync_failure_count = 5 # max number of sync failures before sync is marked as disabled
6768
# any other value means server for specified domain
6869

6970
http_event_retention_days = 90 # number of days to retain http events

internal/types/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ type SystemConfig struct {
218218
EnableCompression bool `toml:"enable_compression"`
219219
HttpEventRetentionDays int `toml:"http_event_retention_days"`
220220
NonHttpEventRetentionDays int `toml:"non_http_event_retention_days"`
221-
AllowedEnv []string `toml:"allowed_env"` // List of environment variables that are allowed to be used in the node config
222-
DefaultScheduleMins int `toml:"default_schedule_mins"` // Default schedule time in minutes for scheduled sync
221+
AllowedEnv []string `toml:"allowed_env"` // List of environment variables that are allowed to be used in the node config
222+
DefaultScheduleMins int `toml:"default_schedule_mins"` // Default schedule time in minutes for scheduled sync
223+
MaxSyncFailureCount int `toml:"max_sync_failure_count"` // Max failure count for sync jobs
223224
}
224225

225226
// GitAuth is a github auth config entry
@@ -604,6 +605,7 @@ type SyncMetadata struct {
604605
}
605606

606607
type SyncJobStatus struct {
608+
FailureCount int `json:"failure_count"` // the number of times the sync job has failed recently
607609
LastExecutionTime time.Time `json:"last_execution_time"` // the last time the sync job was executed
608610
Error string `json:"error"` // the error message if the sync job failed
609611
CommitId string `json:"commit_id"` // the commit id of the sync job

0 commit comments

Comments
 (0)