2026-08-21 15:58:46 -07:00
|
|
|
package main
|
|
|
|
|
|
2026-08-21 18:07:12 -07:00
|
|
|
import (
|
2026-08-23 16:31:33 -07:00
|
|
|
"context"
|
2026-08-21 18:07:12 -07:00
|
|
|
"log"
|
2026-08-23 16:31:33 -07:00
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"syscall"
|
|
|
|
|
"time"
|
2026-08-21 18:07:12 -07:00
|
|
|
|
|
|
|
|
"go.temporal.io/sdk/client"
|
|
|
|
|
"go.temporal.io/sdk/worker"
|
|
|
|
|
"github.com/rockliang/poimen/workflows/action"
|
|
|
|
|
"github.com/rockliang/poimen/workflows/internal/config"
|
2026-08-23 16:31:33 -07:00
|
|
|
"github.com/rockliang/poimen/workflows/internal/health"
|
2026-08-23 16:33:49 -07:00
|
|
|
"github.com/rockliang/poimen/workflows/internal/logging"
|
2026-08-21 18:07:12 -07:00
|
|
|
"github.com/rockliang/poimen/workflows/statemachine"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-21 15:58:46 -07:00
|
|
|
func main() {
|
2026-08-23 16:33:49 -07:00
|
|
|
// Initialize structured logging
|
|
|
|
|
if err := logging.InitLogger(); err != nil {
|
|
|
|
|
log.Fatalf("failed to initialize logger: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer logging.Sync()
|
|
|
|
|
|
2026-08-21 18:07:12 -07:00
|
|
|
// Load configuration
|
|
|
|
|
cfg, err := config.LoadConfig()
|
|
|
|
|
if err != nil {
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Fatal("failed to load config", logging.Err(err))
|
2026-08-21 18:07:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect to Temporal
|
|
|
|
|
c, err := client.Dial(client.Options{
|
|
|
|
|
HostPort: cfg.Temporal.HostPort,
|
|
|
|
|
Namespace: cfg.Temporal.Namespace,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Fatal("failed to connect to temporal", logging.Err(err))
|
2026-08-21 18:07:12 -07:00
|
|
|
}
|
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
|
|
// Create worker
|
2026-08-22 10:18:11 -07:00
|
|
|
w := worker.New(c, "poimen-taskqueue", worker.Options{})
|
2026-08-21 18:07:12 -07:00
|
|
|
if w == nil {
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Fatal("failed to create worker")
|
2026-08-21 18:07:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register all workflows
|
|
|
|
|
w.RegisterWorkflow(statemachine.OrchestratorWorkflow)
|
|
|
|
|
w.RegisterWorkflow(statemachine.TaskUnitWorkflow)
|
2026-08-22 10:30:30 -07:00
|
|
|
w.RegisterWorkflow(statemachine.TestWorkflow)
|
2026-08-21 18:07:12 -07:00
|
|
|
|
|
|
|
|
// Register all activities
|
|
|
|
|
w.RegisterActivity(action.CloneRepoActivity)
|
|
|
|
|
w.RegisterActivity(action.GitWorktreeAddActivity)
|
|
|
|
|
w.RegisterActivity(action.GitCommitActivity)
|
|
|
|
|
w.RegisterActivity(action.GitPushActivity)
|
|
|
|
|
w.RegisterActivity(action.GitSquashMergeActivity)
|
2026-08-21 21:51:41 -07:00
|
|
|
w.RegisterActivity(action.GitDiffActivity)
|
2026-08-21 18:07:12 -07:00
|
|
|
w.RegisterActivity(action.PrepareSkillsActivity)
|
|
|
|
|
w.RegisterActivity(action.PlanningActivity)
|
|
|
|
|
w.RegisterActivity(action.ImplementerActivity)
|
|
|
|
|
w.RegisterActivity(action.JudgeActivity)
|
2026-08-21 21:51:41 -07:00
|
|
|
// Integration and lessons activities - register when fully tested
|
2026-08-26 15:00:11 -07:00
|
|
|
w.RegisterActivity(action.RunIntegrationTestActivity)
|
2026-08-21 18:07:12 -07:00
|
|
|
// w.RegisterActivity(action.UpdateLessonsActivity)
|
|
|
|
|
// w.RegisterActivity(action.ReadLessonsActivity)
|
|
|
|
|
|
2026-08-23 16:31:33 -07:00
|
|
|
// Initialize health checker
|
|
|
|
|
healthChecker := health.NewChecker(c)
|
|
|
|
|
healthHandler := health.NewHandler(healthChecker)
|
|
|
|
|
|
|
|
|
|
// Set up HTTP server for health checks
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
healthHandler.RegisterRoutes(mux)
|
|
|
|
|
|
|
|
|
|
healthServer := &http.Server{
|
|
|
|
|
Addr: ":8081",
|
|
|
|
|
Handler: mux,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start health check server in a goroutine
|
|
|
|
|
go func() {
|
|
|
|
|
log.Printf("Health check server listening on %s", healthServer.Addr)
|
|
|
|
|
if err := healthServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
|
log.Printf("health check server error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Set up signal handling for graceful shutdown
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
|
|
// Run worker in a goroutine
|
|
|
|
|
workerErrChan := make(chan error, 1)
|
|
|
|
|
go func() {
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Info("starting worker on queue", logging.String("queue", "poimen-taskqueue"))
|
2026-08-23 16:31:33 -07:00
|
|
|
if err := w.Run(worker.InterruptCh()); err != nil {
|
|
|
|
|
workerErrChan <- err
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Wait for either worker error or signal
|
|
|
|
|
select {
|
|
|
|
|
case err := <-workerErrChan:
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Fatal("worker failed", logging.Err(err))
|
2026-08-23 16:31:33 -07:00
|
|
|
case sig := <-sigChan:
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Info("received signal", logging.String("signal", sig.String()))
|
2026-08-23 16:31:33 -07:00
|
|
|
w.Stop()
|
|
|
|
|
|
|
|
|
|
// Shutdown health check server
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
if err := healthServer.Shutdown(ctx); err != nil {
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Warn("health check server shutdown error", logging.Err(err))
|
2026-08-23 16:31:33 -07:00
|
|
|
}
|
2026-08-23 16:33:49 -07:00
|
|
|
logging.Info("worker shutdown complete")
|
2026-08-21 18:07:12 -07:00
|
|
|
}
|
2026-08-21 15:58:46 -07:00
|
|
|
}
|