Phase 9, part 6: background work shared safely between copies of the app
internal/jobs keeps named locks as leases in the database (schema 31: locks, instances), taken with one conditional UPDATE on SQLite or Postgres, renewed while the work runs and lapsing if its copy stops. Each background loop's turn, sending an email, audits, discovery runs and imports run under a lock; OAuth sign-ins under way are kept in the database. Copies check in, and work marked running is recovered at start only when no other copy is alive. Settings -> About lists the copies and work under way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+13
-11
@@ -184,19 +184,21 @@ func (s *Server) RunMetrics(ctx context.Context) {
|
||||
tick := time.NewTicker(10 * time.Minute)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if c := s.stripe(); c != nil {
|
||||
if _, err := metrics.SyncOutsideSpend(s.DB, time.Now()); err != nil {
|
||||
s.Log.Warn("metrics: outside spend", "err", err)
|
||||
}
|
||||
last := metrics.LastSync(s.DB)
|
||||
if last == nil || time.Since(last.At) >= time.Hour {
|
||||
sctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
if _, err := metrics.Sync(sctx, s.DB, c, time.Now(), false); err != nil && !errors.Is(err, context.Canceled) {
|
||||
s.Log.Warn("metrics: Stripe sync", "err", err)
|
||||
s.jobDo(ctx, "metrics", func(ctx context.Context) {
|
||||
if c := s.stripe(); c != nil {
|
||||
if _, err := metrics.SyncOutsideSpend(s.DB, time.Now()); err != nil {
|
||||
s.Log.Warn("metrics: outside spend", "err", err)
|
||||
}
|
||||
last := metrics.LastSync(s.DB)
|
||||
if last == nil || time.Since(last.At) >= time.Hour {
|
||||
sctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
||||
if _, err := metrics.Sync(sctx, s.DB, c, time.Now(), false); err != nil && !errors.Is(err, context.Canceled) {
|
||||
s.Log.Warn("metrics: Stripe sync", "err", err)
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
})
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user