Only update the agent last sync when all copies were successful

This commit is contained in:
Melon 2025-02-18 21:40:56 +00:00
parent 8407f21090
commit 56be812ff5
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

View File

@ -159,6 +159,8 @@ func (a *Agent) syncSingleAgentCertPairs(startTime time.Time, agent syncAgent, r
return fmt.Errorf("scp client: %w", err) return fmt.Errorf("scp client: %w", err)
} }
hadError := false
for _, row := range rows { for _, row := range rows {
err := a.copySingleCertPair(&scpClient, row) err := a.copySingleCertPair(&scpClient, row)
if err != nil { if err != nil {
@ -166,19 +168,23 @@ func (a *Agent) syncSingleAgentCertPairs(startTime time.Time, agent syncAgent, r
// same agent from copying. // same agent from copying.
err = fmt.Errorf("copySingleCertPair: %w", err) err = fmt.Errorf("copySingleCertPair: %w", err)
Logger.Warn("Agent certificate sync failed", "agent", row.AgentID, "cert", row.CertID, "not after", row.CertNotAfter, "err", err) Logger.Warn("Agent certificate sync failed", "agent", row.AgentID, "cert", row.CertID, "not after", row.CertNotAfter, "err", err)
hadError = true
continue continue
} }
} }
// Update last sync to the time when the database request happened. This ensures // The agent last sync will only update if all scp copies were successful.
// that certificates updated after the database request and before the agent if !hadError {
// syncing are updated properly. // Update last sync to the time when the database request happened. This ensures
err = a.db.UpdateAgentLastSync(context.Background(), database.UpdateAgentLastSyncParams{ // that certificates updated after the database request and before the agent
LastSync: sql.NullTime{Time: startTime, Valid: true}, // syncing are updated properly.
ID: agent.agentId, err = a.db.UpdateAgentLastSync(context.Background(), database.UpdateAgentLastSyncParams{
}) LastSync: sql.NullTime{Time: startTime, Valid: true},
if err != nil { ID: agent.agentId,
return fmt.Errorf("error updating agent last sync: %v", err) })
if err != nil {
return fmt.Errorf("error updating agent last sync: %v", err)
}
} }
return nil return nil