carlos / platform Public

console: alert the operator 60 days before a registration expires

Nothing renews domains yet, so a daily pass pages the operator once for
each registration (bought or transferred in) expiring within 60 days,
naming the domain, account and date. It marks the registration alerted
only when the page was delivered; an undelivered page is tried again on
the next day's pass. Console.db only. The purchase pass loop runs it on
its first run and then once every 24 hours. The alert text is a draft
in domainexpiry_copy.go.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b477866b52d601e03f14fdb0c006596dbc74f394 parent e4645ad
5 files changed, +298 −2
  • internal/console/domainbuy.go +4 −1
  • internal/console/domainexpiry.go +76 −0
  • internal/console/domainexpiry_copy.go +12 −0
  • internal/console/domainexpiry_test.go +204 −0
  • internal/console/jobs_test.go +2 −1
diff --git a/internal/console/domainbuy.go b/internal/console/domainbuy.go
index 20f9e8bab..b2f910cfd 100644
--- a/internal/console/domainbuy.go
+++ b/internal/console/domainbuy.go
@@ -967,11 +967,14 @@ func (s *Server) StartDomainPurchasePass(ctx context.Context) {
}
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
+ var expiry domainExpiryDaily
for {
if ctx.Err() != nil {
return
}
- _ = s.jobs.Run(ctx, "StartDomainPurchasePass", s.domainPurchasePass)
+ _ = s.jobs.Run(ctx, "StartDomainPurchasePass", func(ctx context.Context) error {
+ return errors.Join(s.domainPurchasePass(ctx), expiry.pass(ctx, s))
+ })
select {
case <-ctx.Done():
return
diff --git a/internal/console/domainexpiry.go b/internal/console/domainexpiry.go
new file mode 100644
index 000000000..bdcc57cdb
--- /dev/null
+++ b/internal/console/domainexpiry.go
@@ -0,0 +1,76 @@
+package console
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log"
+ "strconv"
+ "time"
+)
+
+// domainExpiryLead is how long before a registration expires the operator
+// is alerted. Nothing renews domains yet (auto-renew is off at the
+// registrar), so this alert is the only warning.
+const domainExpiryLead = 60 * 24 * time.Hour
+
+// domainExpiryEvery is how often the purchase pass loop runs the expiry pass.
+const domainExpiryEvery = 24 * time.Hour
+
+// domainExpiryPass alerts the operator once for each registration, bought
+// or transferred in, that expires within domainExpiryLead of now, and marks
+// it alerted only when the alert was delivered: an undelivered alert is
+// tried again on the next pass. It reads and writes console.db only.
+func (s *Server) domainExpiryPass(ctx context.Context, now time.Time) error {
+ // Expiries are whole seconds: one second on makes the bound inclusive.
+ regs, err := s.store.RegistrationsExpiringBefore(ctx, now.Add(domainExpiryLead+time.Second))
+ if err != nil {
+ return fmt.Errorf("expiring registrations: %w", err)
+ }
+ var errs []error
+ for _, r := range regs {
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ account := "#" + strconv.FormatInt(r.AccountID, 10)
+ acct, err := s.store.AccountByIDContext(ctx, r.AccountID)
+ if err != nil {
+ // The alert still goes out, naming the account by its id.
+ log.Printf("console: domain expiry %s: account %d: %v", r.Domain, r.AccountID, err)
+ jobFailed(ctx, err)
+ errs = append(errs, fmt.Errorf("domain expiry %s: %w", r.Domain, err))
+ } else if acct != nil {
+ account = acct.Sqid
+ }
+ date := r.Expires.UTC().Format("2 January 2006")
+ subject := fmt.Sprintf(domainExpiryCopy.OperatorSubject, r.Domain, date)
+ body := fmt.Sprintf(domainExpiryCopy.OperatorBody, r.Domain, account, date)
+ if err := s.alertOperator(ctx, subject, body); err != nil {
+ log.Printf("console: domain expiry %s: %v", r.Domain, err)
+ jobFailed(ctx, err)
+ errs = append(errs, fmt.Errorf("domain expiry %s: %w", r.Domain, err))
+ continue
+ }
+ if err := s.store.MarkExpiryAlerted(ctx, r.Domain, now); err != nil {
+ log.Printf("console: domain expiry %s: mark alerted: %v", r.Domain, err)
+ jobFailed(ctx, err)
+ errs = append(errs, fmt.Errorf("domain expiry %s: mark alerted: %w", r.Domain, err))
+ }
+ }
+ return errors.Join(errs...)
+}
+
+// domainExpiryDaily runs the expiry pass from the purchase pass loop on its
+// first run and then at most once every domainExpiryEvery, whatever the
+// outcome (a failed alert waits a day rather than paging every tick). It is
+// held by one loop, so it needs no lock.
+type domainExpiryDaily struct{ next time.Time }
+
+func (d *domainExpiryDaily) pass(ctx context.Context, s *Server) error {
+ now := s.transferNow()
+ if now.Before(d.next) {
+ return nil
+ }
+ d.next = now.Add(domainExpiryEvery)
+ return s.domainExpiryPass(ctx, now)
+}
diff --git a/internal/console/domainexpiry_copy.go b/internal/console/domainexpiry_copy.go
new file mode 100644
index 000000000..1168024c3
--- /dev/null
+++ b/internal/console/domainexpiry_copy.go
@@ -0,0 +1,12 @@
+package console
+
+// DRAFT operator copy for the expiry alert (spec ยง12), pending copy review.
+// Operator-facing only: it names the domain, account and date, and says
+// plainly that nothing renews domains yet.
+var domainExpiryCopy = struct {
+ OperatorSubject, OperatorBody string
+}{
+ OperatorSubject: "Domain %s expires on %s",
+ OperatorBody: "Domain: %s\nAccount: %s\nExpires: %s\n\n" +
+ "Nothing renews domains yet. Renew it at the registrar before that date, or the account loses it.\n",
+}
diff --git a/internal/console/domainexpiry_test.go b/internal/console/domainexpiry_test.go
new file mode 100644
index 000000000..8be73a7b0
--- /dev/null
+++ b/internal/console/domainexpiry_test.go
@@ -0,0 +1,204 @@
+package console
+
+import (
+ "context"
+ "errors"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+// expiryFixture is a bare Server over a fresh store, with the operator page
+// captured (or failed, while pageErr is set).
+type expiryFixture struct {
+ srv *Server
+ st *Store
+ now time.Time
+ mu sync.Mutex
+ pages []string
+ pageErr error
+}
+
+func newExpiryFixture(t *testing.T) *expiryFixture {
+ t.Helper()
+ f := &expiryFixture{st: openTestStore(t), now: time.Date(2026, 11, 22, 9, 0, 0, 0, time.UTC)}
+ f.srv = &Server{store: f.st, transferClock: func() time.Time { return f.now }}
+ previous := pageDomainPurchase
+ pageDomainPurchase = func(_ context.Context, _, subject, body string) error {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.pageErr != nil {
+ return f.pageErr
+ }
+ f.pages = append(f.pages, subject+"\n"+body)
+ return nil
+ }
+ t.Cleanup(func() { pageDomainPurchase = previous })
+ return f
+}
+
+func (f *expiryFixture) register(t *testing.T, domain string, accountID int64, sqid string, expires time.Time) {
+ t.Helper()
+ if sqid != "" {
+ if err := f.st.CreateAccount(accountID, sqid, "Shop", "owner@example.test", f.now); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := f.st.AddDomainRegistration(DomainRegistration{Domain: domain, AccountID: accountID, Expires: expires}); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func (f *expiryFixture) taken() []string {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ out := f.pages
+ f.pages = nil
+ return out
+}
+
+func (f *expiryFixture) alertedAt(t *testing.T, accountID int64, domain string) time.Time {
+ t.Helper()
+ r, err := f.st.DomainRegistrationFor(t.Context(), accountID, domain)
+ if err != nil || r == nil {
+ t.Fatalf("registration %s: %+v %v", domain, r, err)
+ }
+ return r.ExpiryAlertedAt
+}
+
+func TestExpiryPassAlertsOnceAtSixtyDays(t *testing.T) {
+ f := newExpiryFixture(t)
+ ctx := t.Context()
+ id := testAccountID()
+ expires := f.now.Add(60 * 24 * time.Hour)
+ f.register(t, "shop.example", id, "acct1", expires)
+ // A transferred-in registration has no zone; it is alerted all the same.
+ f.register(t, "moved.example", id, "", f.now.Add(10*24*time.Hour))
+
+ if err := f.srv.domainExpiryPass(ctx, f.now); err != nil {
+ t.Fatal(err)
+ }
+ pages := f.taken()
+ if len(pages) != 2 {
+ t.Fatalf("pages = %q, want one per expiring registration", pages)
+ }
+ var shop string
+ for _, p := range pages {
+ if strings.Contains(p, "shop.example") {
+ shop = p
+ }
+ }
+ for _, want := range []string{"shop.example", "acct1", expires.Format("2 January 2006"), "Nothing renews domains yet"} {
+ if !strings.Contains(shop, want) {
+ t.Errorf("page %q lacks %q", shop, want)
+ }
+ }
+ for _, p := range pages {
+ if strings.ContainsAny(p, "โ€”โ€“") {
+ t.Errorf("page has a dash: %q", p)
+ }
+ }
+ if got := f.alertedAt(t, id, "shop.example"); !got.Equal(f.now) {
+ t.Errorf("shop.example alerted at %v, want %v", got, f.now)
+ }
+
+ f.now = f.now.Add(24 * time.Hour)
+ if err := f.srv.domainExpiryPass(ctx, f.now); err != nil {
+ t.Fatal(err)
+ }
+ if pages := f.taken(); len(pages) != 0 {
+ t.Fatalf("second pass paged again: %q", pages)
+ }
+}
+
+func TestExpiryPassIgnoresLaterExpiries(t *testing.T) {
+ f := newExpiryFixture(t)
+ ctx := t.Context()
+ id := testAccountID()
+ f.register(t, "later.example", id, "acct1", f.now.Add(60*24*time.Hour+time.Second))
+ f.register(t, "unknown.example", id, "", time.Time{}) // no expiry recorded
+
+ if err := f.srv.domainExpiryPass(ctx, f.now); err != nil {
+ t.Fatal(err)
+ }
+ if pages := f.taken(); len(pages) != 0 {
+ t.Fatalf("paged for a later or unknown expiry: %q", pages)
+ }
+ if got := f.alertedAt(t, id, "later.example"); !got.IsZero() {
+ t.Errorf("later.example marked alerted at %v", got)
+ }
+
+ // A day on, it is inside the window.
+ f.now = f.now.Add(24 * time.Hour)
+ if err := f.srv.domainExpiryPass(ctx, f.now); err != nil {
+ t.Fatal(err)
+ }
+ if pages := f.taken(); len(pages) != 1 || !strings.Contains(pages[0], "later.example") {
+ t.Fatalf("pages a day on = %q, want later.example", pages)
+ }
+}
+
+func TestExpiryPassRetriesAfterFailedDelivery(t *testing.T) {
+ f := newExpiryFixture(t)
+ ctx := t.Context()
+ id := testAccountID()
+ f.register(t, "shop.example", id, "acct1", f.now.Add(30*24*time.Hour))
+
+ f.pageErr = errors.New("sink down")
+ if err := f.srv.domainExpiryPass(ctx, f.now); err == nil {
+ t.Fatal("a failed page is not an error")
+ }
+ if got := f.alertedAt(t, id, "shop.example"); !got.IsZero() {
+ t.Fatalf("marked alerted after a failed page: %v", got)
+ }
+
+ f.pageErr = nil
+ f.now = f.now.Add(24 * time.Hour)
+ if err := f.srv.domainExpiryPass(ctx, f.now); err != nil {
+ t.Fatal(err)
+ }
+ if pages := f.taken(); len(pages) != 1 {
+ t.Fatalf("pages after the sink recovered = %q, want one", pages)
+ }
+ if got := f.alertedAt(t, id, "shop.example"); !got.Equal(f.now) {
+ t.Fatalf("alerted at %v, want %v", got, f.now)
+ }
+}
+
+// TestExpiryPassRunsOnceADay: the purchase pass loop runs the expiry pass
+// on its first run and then at most once every 24 hours, whatever the
+// outcome.
+func TestExpiryPassRunsOnceADay(t *testing.T) {
+ f := newExpiryFixture(t)
+ ctx := t.Context()
+ id := testAccountID()
+ f.register(t, "shop.example", id, "acct1", f.now.Add(30*24*time.Hour))
+ f.pageErr = errors.New("sink down")
+ var daily domainExpiryDaily
+ attempts := 0
+ pageFailing := pageDomainPurchase
+ pageDomainPurchase = func(ctx context.Context, level, subject, body string) error {
+ attempts++
+ return pageFailing(ctx, level, subject, body)
+ }
+
+ if err := daily.pass(ctx, f.srv); err == nil {
+ t.Fatal("first run: a failed page is not an error")
+ }
+ f.now = f.now.Add(23 * time.Hour)
+ if err := daily.pass(ctx, f.srv); err != nil {
+ t.Fatalf("inside the day: %v", err)
+ }
+ if attempts != 1 {
+ t.Fatalf("attempts inside the day = %d, want 1", attempts)
+ }
+ f.pageErr = nil
+ f.now = f.now.Add(time.Hour)
+ if err := daily.pass(ctx, f.srv); err != nil {
+ t.Fatal(err)
+ }
+ if attempts != 2 || !f.alertedAt(t, id, "shop.example").Equal(f.now) {
+ t.Fatalf("a day on: attempts %d, alerted %v", attempts, f.alertedAt(t, id, "shop.example"))
+ }
+}
diff --git a/internal/console/jobs_test.go b/internal/console/jobs_test.go
index a396a733c..b0482d7bb 100644
--- a/internal/console/jobs_test.go
+++ b/internal/console/jobs_test.go
@@ -53,7 +53,8 @@ var jobPassFuncs = map[string][]string{
"StartDomainPurchasePass": {"domainPurchasePass", "advancePurchase", "advancePurchaseStep", "advancePurchaseEffects", "reconcileRegisteringPurchase", "purchaseMutation", "purchaseCheckout", "purchaseCheckoutStep", "registerPurchase", "registeredPurchase", "failPurchase", "purchaseAlert", "purchaseOwnerNotice", "purchaseManagedSet", "capturedPurchase", "finishPurchaseUnpointed",
"advanceTransferStep", "gatherTransfer", "gatherTransferRegistrar", "gatherTransferPayment", "gatherTransferRefund", "adoptTransferCheckout",
"readTransferStatus", "actTransfer", "captureTransfer", "submitTransfer", "refundTransfer", "completeTransfer", "ownerNotice", "ownerNoticeErr",
- "recordPurchaseRegistration", "deliverTransferNotices", "sendTransferNotice", "transferNoticesPass"},
+ "recordPurchaseRegistration", "deliverTransferNotices", "sendTransferNotice", "transferNoticesPass",
+ "domainExpiryPass"},
"StartBillingSyncPass": {"billingSyncPass", "billingDiscoveryPass", "discoverCustomer", "transitionAccountPlan", "billingMoneyPass", "reconcilePayments", "recordCharge", "recordChargeMovements", "billingRatesPass", "billingSnapshotPass"},
"PublishPlansMarker": {"publishPlansMarkerOnce"},
"StartRunSweep": {"runSweepPass", "sweepExpireQueued", "sweepLostRun", "sweepTerminalRun", "sweepOrphanRuns",