some cleanup and annonce expiry
This commit is contained in:
68
main.go
68
main.go
@@ -60,15 +60,12 @@ func init() {
|
||||
}
|
||||
|
||||
type App struct {
|
||||
pool *GrainLocalPool
|
||||
pool *CartPool
|
||||
storage *DiskStorage
|
||||
}
|
||||
|
||||
func (a *App) Save() error {
|
||||
|
||||
a.pool.mu.RLock()
|
||||
defer a.pool.mu.RUnlock()
|
||||
for id, grain := range a.pool.GetGrains() {
|
||||
for id, grain := range a.pool.SnapshotGrains() {
|
||||
if grain == nil {
|
||||
continue
|
||||
}
|
||||
@@ -80,19 +77,7 @@ func (a *App) Save() error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (a *App) HandleSave(w http.ResponseWriter, r *http.Request) {
|
||||
err := a.Save()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
var podIp = os.Getenv("POD_IP")
|
||||
@@ -121,24 +106,6 @@ func getCountryFromHost(host string) string {
|
||||
return "se"
|
||||
}
|
||||
|
||||
func getCheckoutOrder(host string, cartId CartId) *messages.CreateCheckoutOrder {
|
||||
baseUrl := fmt.Sprintf("https://%s", host)
|
||||
cartBaseUrl := os.Getenv("CART_BASE_URL")
|
||||
if cartBaseUrl == "" {
|
||||
cartBaseUrl = "https://cart.tornberg.me"
|
||||
}
|
||||
country := getCountryFromHost(host)
|
||||
|
||||
return &messages.CreateCheckoutOrder{
|
||||
Terms: fmt.Sprintf("%s/terms", baseUrl),
|
||||
Checkout: fmt.Sprintf("%s/checkout?order_id={checkout.order.id}", baseUrl),
|
||||
Confirmation: fmt.Sprintf("%s/confirmation/{checkout.order.id}", baseUrl),
|
||||
Validation: fmt.Sprintf("%s/validation", cartBaseUrl),
|
||||
Push: fmt.Sprintf("%s/push?order_id={checkout.order.id}", cartBaseUrl),
|
||||
Country: country,
|
||||
}
|
||||
}
|
||||
|
||||
func GetDiscovery() Discovery {
|
||||
if podIp == "" {
|
||||
return nil
|
||||
@@ -157,32 +124,27 @@ func GetDiscovery() Discovery {
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
storage, err := NewDiskStorage(fmt.Sprintf("data/s_%s.gob", name))
|
||||
if err != nil {
|
||||
log.Printf("Error loading state: %v\n", err)
|
||||
}
|
||||
localPool := NewGrainLocalPool(2*65535, 15*time.Minute, spawn)
|
||||
pool, err := NewCartPool(2*65535, 15*time.Minute, podIp, spawn, GetDiscovery())
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating cart pool: %v\n", err)
|
||||
}
|
||||
app := &App{
|
||||
pool: localPool,
|
||||
pool: pool,
|
||||
storage: storage,
|
||||
}
|
||||
|
||||
syncedPool, err := NewSyncedPool(localPool, podIp, GetDiscovery())
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating synced pool: %v\n", err)
|
||||
}
|
||||
|
||||
// Start unified gRPC server (CartActor + ControlPlane) replacing legacy RPC server on :1337
|
||||
// TODO: Remove any remaining legacy RPC server references and deprecated frame-based code after full gRPC migration is validated.
|
||||
grpcSrv, err := StartGRPCServer(":1337", syncedPool)
|
||||
grpcSrv, err := StartGRPCServer(":1337", pool)
|
||||
if err != nil {
|
||||
log.Fatalf("Error starting gRPC server: %v\n", err)
|
||||
}
|
||||
defer grpcSrv.GracefulStop()
|
||||
|
||||
go func() {
|
||||
for range time.Tick(time.Minute * 10) {
|
||||
for range time.Tick(time.Minute * 5) {
|
||||
err := app.Save()
|
||||
if err != nil {
|
||||
log.Printf("Error saving: %v\n", err)
|
||||
@@ -193,7 +155,7 @@ func main() {
|
||||
Url: amqpUrl,
|
||||
}
|
||||
|
||||
syncedServer := NewPoolServer(syncedPool, fmt.Sprintf("%s, %s", name, podIp))
|
||||
syncedServer := NewPoolServer(pool, fmt.Sprintf("%s, %s", name, podIp))
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/cart/", http.StripPrefix("/cart", syncedServer.Serve()))
|
||||
// only for local
|
||||
@@ -210,16 +172,13 @@ func main() {
|
||||
mux.Handle("/metrics", promhttp.Handler())
|
||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Grain pool health: simple capacity check (mirrors previous GrainHandler.IsHealthy)
|
||||
app.pool.mu.RLock()
|
||||
grainCount := len(app.pool.grains)
|
||||
capacity := app.pool.PoolSize
|
||||
app.pool.mu.RUnlock()
|
||||
grainCount, capacity := app.pool.LocalUsage()
|
||||
if grainCount >= capacity {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("grain pool at capacity"))
|
||||
return
|
||||
}
|
||||
if !syncedPool.IsHealthy() {
|
||||
if !pool.IsHealthy() {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("control plane not healthy"))
|
||||
return
|
||||
@@ -382,8 +341,9 @@ func main() {
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
fmt.Println("Shutting down due to signal:", sig)
|
||||
go syncedPool.Close()
|
||||
app.Save()
|
||||
pool.Close()
|
||||
|
||||
done <- true
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user