Files
tenant-registry/cmd/erpmock/main.go
Sharang ParnerkarandClaude Opus 5.5 e5ed990bf7
ci / shared (pull_request) Successful in 11s
ci / test (pull_request) Successful in 25m15s
ci / image (pull_request) Skipped
fix(outbox): dead-letter after a week; ERP mock names unique per boot
Stage logged ~11,100 failed usage.record items a day ('tenant has no ERP
customer yet') behind 36 customer.upsert failures on the unique
tenants_erp_customer_id_key (backlog t25). The ERP mock keeps its docs
in memory and restarts with every registry deploy on stage: its counter
began at CUST-00001 again and handed new tenants names older tenants are
still linked to, so they never got linked and their usage waited
forever.

- erpmock: Config.NameToken goes into every assigned name; cmd/erpmock
  sets one per boot (CUST-<yymmddHHMMSS>-00001). Tests keep plain names.
- outbox: after maxOutboxAttempts (170, about a week at the 1 h cap) an
  item is dead-lettered instead of retried forever: done_at set,
  last_error 'dead letter: <err>', payload kept for a re-queue, and an
  ERROR line (the prod error alert sees it). Store gains
  DeadLetterOutbox (memory + postgres).

Tests: dead letter after the last attempt and nothing claimed after,
memory and postgres; mock names carry the token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
2026-10-29 18:21:01 +01:00

61 lines
1.9 KiB
Go

// erpmock — the staging stand-in for ERPNext (decision 18). See
// internal/erpmock. Configuration:
//
// ERPMOCK_ADDR listen address (default :8000)
// ERP_API_KEY expected `token key:secret` (default mock / mock)
// ERP_API_SECRET
// ERP_WEBHOOK_URL registry receiver, e.g. http://tenant-registry:8090/v1/internal/erp/webhook
// ERP_WEBHOOK_SECRET HMAC secret shared with the registry (ERP_WEBHOOK_SECRET there too)
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/erpmock"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
cfg := erpmock.Config{
APIKey: env("ERP_API_KEY", "mock"),
APISecret: env("ERP_API_SECRET", "mock"),
WebhookURL: os.Getenv("ERP_WEBHOOK_URL"),
WebhookSecret: os.Getenv("ERP_WEBHOOK_SECRET"),
// One token per boot keeps assigned names unique across restarts.
NameToken: time.Now().UTC().Format("060102150405"),
}
addr := env("ERPMOCK_ADDR", ":8000")
if cfg.WebhookURL == "" {
slog.Warn("ERP_WEBHOOK_URL not set — /_mock/fire/* simulates events without delivering webhooks")
}
srv := &http.Server{Addr: addr, Handler: erpmock.New(cfg), ReadTimeout: 10 * time.Second, WriteTimeout: 30 * time.Second}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
slog.Info("erpmock listening", "addr", addr, "webhook_url", cfg.WebhookURL)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server crashed", "err", err)
os.Exit(1)
}
}()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
}
func env(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}