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:
2026-11-25 07:56:47 -07:00
co-authored by Claude Opus 5.5
parent ba6114f367
commit c6c4c40600
18 changed files with 873 additions and 150 deletions
+47 -40
View File
@@ -13,6 +13,7 @@ import (
"time"
"supermarket/internal/api/gsc"
"supermarket/internal/jobs"
"supermarket/internal/oauth"
"supermarket/internal/outreach"
"supermarket/internal/profile"
@@ -263,46 +264,50 @@ func (s *Server) syncSearchConsole(ctx context.Context) error {
// RunSEO syncs Search Console twice a day.
func (s *Server) RunSEO(ctx context.Context) {
if err := seo.RecoverAudits(s.DB); err != nil {
s.Log.Error("seo: recovering audits", "err", err)
}
if err := seo.RecoverChanges(s.DB); err != nil {
s.Log.Error("seo: recovering changes", "err", err)
}
if err := seo.RecoverBriefs(s.DB); err != nil {
s.Log.Error("seo: recovering briefs", "err", err)
}
if err := sites.RecoverPages(s.DB); err != nil {
s.Log.Error("sites: recovering pages", "err", err)
if s.alone() { // otherwise "running" may be another copy's work
if err := seo.RecoverAudits(s.DB); err != nil {
s.Log.Error("seo: recovering audits", "err", err)
}
if err := seo.RecoverChanges(s.DB); err != nil {
s.Log.Error("seo: recovering changes", "err", err)
}
if err := seo.RecoverBriefs(s.DB); err != nil {
s.Log.Error("seo: recovering briefs", "err", err)
}
if err := sites.RecoverPages(s.DB); err != nil {
s.Log.Error("sites: recovering pages", "err", err)
}
}
tick := time.NewTicker(30 * time.Minute)
defer tick.Stop()
for {
s.weeklyAudits(time.Now())
if s.gsc() != nil {
due := false
for _, site := range s.seoSites() {
if site.Property != "" && (site.Last == nil || time.Since(site.Last.At) > 12*time.Hour) {
due = true
s.jobDo(ctx, "seo", func(ctx context.Context) {
s.weeklyAudits(time.Now())
if s.gsc() != nil {
due := false
for _, site := range s.seoSites() {
if site.Property != "" && (site.Last == nil || time.Since(site.Last.At) > 12*time.Hour) {
due = true
}
}
}
if due {
sctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
if err := s.syncSearchConsole(sctx); err != nil {
s.Log.Warn("seo: Search Console sync", "err", err)
if due {
sctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
if err := s.syncSearchConsole(sctx); err != nil {
s.Log.Warn("seo: Search Console sync", "err", err)
}
cancel()
}
cancel()
s.weeklyReports(ctx, time.Now())
s.dailyChecks(ctx)
}
s.weeklyReports(ctx, time.Now())
s.dailyChecks(ctx)
}
s.weeklyBingLinks(ctx)
s.dailyLinkChecks(ctx)
s.monthlyPartnerPages(ctx)
s.refreshChanges(ctx)
s.refreshBriefs(ctx)
s.refreshSitePages(ctx)
s.pingNewlyLive(ctx)
s.weeklyBingLinks(ctx)
s.dailyLinkChecks(ctx)
s.monthlyPartnerPages(ctx)
s.refreshChanges(ctx)
s.refreshBriefs(ctx)
s.refreshSitePages(ctx)
s.pingNewlyLive(ctx)
})
select {
case <-ctx.Done():
return
@@ -543,9 +548,6 @@ func (s *Server) dailyChecks(ctx context.Context) {
var pageSpeedKey = secretField{"pagespeed", secrets.PageSpeedAPIKey, "PageSpeed Insights API key (optional)",
"Free. In Google Cloud, enable the PageSpeed Insights API and create an API key under Credentials. Without one, Google allows only a few speed checks.", googleCreds}
// auditing marks the sites being audited now.
var auditing sync.Map
// auditSites are every website in the profile (an audit doesn't need
// Search Console; with it, issues are ranked by traffic).
func (s *Server) auditSites(r *http.Request) ([]seoSite, *seoSite) {
@@ -611,18 +613,23 @@ func (s *Server) seoAuditRun(w http.ResponseWriter, r *http.Request) {
// startAudit runs one in the background, unless one is running for the
// site.
func (s *Server) startAudit(site seoSite, speed bool) {
if _, busy := auditing.LoadOrStore(site.URL, true); busy {
// One audit per site at a time, across copies of the app.
lctx, release, err := s.jobs().TryLock(context.Background(), "audit:"+site.URL)
if err != nil {
if !errors.Is(err, jobs.ErrBusy) {
s.Log.Error("seo: starting the audit", "err", err)
}
return
}
id, err := seo.StartAudit(s.DB, site.URL, time.Now())
if err != nil {
auditing.Delete(site.URL)
release()
s.Log.Error("seo: starting the audit", "err", err)
return
}
go func() {
defer auditing.Delete(site.URL)
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Minute)
defer release()
ctx, cancel := context.WithTimeout(lctx, 45*time.Minute)
defer cancel()
a, err := seo.RunAudit(ctx, s.DB, seo.NewFetcher("Supermarket/"+s.Version+" (SEO audit)"), site.URL, site.Property)
if err == nil && speed {