-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
515 lines (457 loc) · 16.4 KB
/
Copy pathserver.go
File metadata and controls
515 lines (457 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package server
import (
"context"
"database/sql"
"fmt"
"log/slog"
"net/http"
"regexp"
"strconv"
"sync"
"time"
"github.com/dvflw/mantle/internal/api/health"
"github.com/dvflw/mantle/internal/artifact"
"github.com/dvflw/mantle/internal/audit"
"github.com/dvflw/mantle/internal/auth"
"github.com/dvflw/mantle/internal/budget"
"github.com/dvflw/mantle/internal/config"
"github.com/dvflw/mantle/internal/engine"
"github.com/dvflw/mantle/internal/metrics"
"github.com/dvflw/mantle/internal/repo"
reposync "github.com/dvflw/mantle/internal/repo/sync"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Server is the long-running Mantle process that hosts the API,
// cron scheduler, and webhook listener.
type Server struct {
DB *sql.DB
Engine *engine.Engine
AuthStore *auth.Store
OIDCValidator *auth.OIDCValidator
Auditor audit.Emitter
BudgetStore *budget.Store
Address string
TLSCertFile string
TLSKeyFile string
Logger *slog.Logger
// GitOps webhook support — set by the caller (e.g. serve.go) after New().
// Both fields may be nil in test contexts that don't exercise /hooks/git/.
RepoStore *repo.Store
GitDriver reposync.Driver
httpServer *http.Server
cron *CronScheduler
webhooks *WebhookHandler
emailPoller *EmailTriggerPoller
worker *engine.Worker
reaper *engine.Reaper
artifactReaper *artifact.Reaper
mu sync.Mutex
running map[string]context.CancelFunc // execution ID → cancel
}
// New creates a Server with the given configuration.
func New(db *sql.DB, eng *engine.Engine, address string) *Server {
logger := slog.Default()
s := &Server{
DB: db,
Engine: eng,
Address: address,
Logger: logger,
running: make(map[string]context.CancelFunc),
}
s.cron = NewCronScheduler(s)
s.webhooks = NewWebhookHandler(s)
s.emailPoller = NewEmailTriggerPoller(s)
return s
}
// statusWriter wraps http.ResponseWriter to capture the status code.
type statusWriter struct {
http.ResponseWriter
status int
}
func (sw *statusWriter) WriteHeader(code int) {
sw.status = code
sw.ResponseWriter.WriteHeader(code)
}
// uuidPattern matches UUID-like path segments.
var uuidPattern = regexp.MustCompile(`[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`)
// normalizePath collapses UUID segments to {id} to prevent metric cardinality explosion.
func normalizePath(p string) string {
return uuidPattern.ReplaceAllString(p, "{id}")
}
// metricsMiddleware records HTTP request duration and total count.
func metricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &statusWriter{ResponseWriter: w, status: 200}
next.ServeHTTP(sw, r)
duration := time.Since(start).Seconds()
path := normalizePath(r.URL.Path)
status := strconv.Itoa(sw.status)
metrics.HTTPRequestDuration.WithLabelValues(r.Method, path, status).Observe(duration)
metrics.HTTPRequestsTotal.WithLabelValues(r.Method, path, status).Inc()
})
}
// Start starts the HTTP server, cron scheduler, and webhook handler.
// It blocks until the context is cancelled.
func (s *Server) Start(ctx context.Context) error {
// apiMux holds all routes that require authentication (and rate limiting).
// Health and metrics endpoints are registered on the top-level mux below
// so that Kubernetes probes and Prometheus scrapes bypass auth entirely.
apiMux := http.NewServeMux()
// Git push webhook endpoint — registered before the generic /hooks/ prefix
// so the more-specific path is explicit (Go's ServeMux uses longest-prefix
// match regardless of registration order, but explicit ordering is clearer).
apiMux.Handle("/hooks/git/", &GitWebhookHandler{
DB: s.DB,
Store: s.RepoStore,
Driver: s.GitDriver,
})
// Webhook endpoints.
apiMux.HandleFunc("/hooks/", s.webhooks.ServeHTTP)
// API endpoints.
apiMux.HandleFunc("POST /api/v1/run/{workflow}", s.handleRun)
apiMux.HandleFunc("POST /api/v1/cancel/{execution}", s.handleCancel)
apiMux.HandleFunc("GET /api/v1/executions", s.handleListExecutions)
apiMux.HandleFunc("GET /api/v1/executions/{id}", s.handleGetExecution)
// Workflow definition endpoints.
apiMux.HandleFunc("GET /api/v1/workflows", s.handleListWorkflows)
apiMux.HandleFunc("GET /api/v1/workflows/{name}", s.handleGetWorkflow)
apiMux.HandleFunc("GET /api/v1/workflows/{name}/versions", s.handleListWorkflowVersions)
apiMux.HandleFunc("GET /api/v1/workflows/{name}/versions/{version}", s.handleGetWorkflowVersion)
// Budget endpoints.
apiMux.HandleFunc("GET /api/v1/budgets", s.handleListBudgets)
apiMux.HandleFunc("PUT /api/v1/budgets/{provider}", s.handleSetBudget)
apiMux.HandleFunc("DELETE /api/v1/budgets/{provider}", s.handleDeleteBudget)
apiMux.HandleFunc("GET /api/v1/budgets/usage", s.handleGetUsage)
// OpenAPI spec endpoint.
apiMux.HandleFunc("GET /api/v1/openapi.json", handleOpenAPISpec)
// Start distributed engine components (worker + reaper).
if cfg := config.FromContext(ctx); cfg != nil {
claimer := &engine.Claimer{
DB: s.DB,
NodeID: cfg.Engine.NodeID,
LeaseDuration: cfg.Engine.StepLeaseDuration,
}
s.worker = &engine.Worker{
Claimer: claimer,
StepExecutor: s.Engine.MakeGlobalStepExecutor(),
PollInterval: cfg.Engine.WorkerPollInterval,
MaxBackoff: cfg.Engine.WorkerMaxBackoff,
LeaseRenewInterval: cfg.Engine.StepLeaseDuration / 3,
Logger: s.Logger,
}
go s.worker.Run(ctx)
s.Logger.Info("worker started", "node_id", cfg.Engine.NodeID)
s.reaper = &engine.Reaper{
DB: s.DB,
Interval: cfg.Engine.ReaperInterval,
Logger: s.Logger,
}
go s.reaper.Run(ctx)
s.Logger.Info("reaper started", "interval", cfg.Engine.ReaperInterval)
// Start retention cleanup if configured.
if cfg.Retention.ExecutionDays > 0 || cfg.Retention.AuditDays > 0 {
go func() {
ticker := time.NewTicker(24 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if _, err := engine.CleanupExecutions(ctx, s.DB, cfg.Retention.ExecutionDays); err != nil {
s.Logger.Error("retention cleanup failed", "error", err)
}
if _, err := engine.CleanupAuditEvents(ctx, s.DB, cfg.Retention.AuditDays); err != nil {
s.Logger.Error("audit retention cleanup failed", "error", err)
}
}
}
}()
s.Logger.Info("retention cleanup scheduled",
"execution_days", cfg.Retention.ExecutionDays,
"audit_days", cfg.Retention.AuditDays)
}
// Start artifact reaper if the artifact subsystem is configured.
if s.Engine.ArtifactStore != nil && s.Engine.Storage != nil {
retention := 24 * time.Hour // default
if cfg.Storage.Retention != "" {
if d, err := time.ParseDuration(cfg.Storage.Retention); err == nil && d > 0 {
retention = d
}
}
s.artifactReaper = &artifact.Reaper{
Store: s.Engine.ArtifactStore,
Storage: s.Engine.Storage,
Retention: retention,
Logger: s.Logger,
}
go func() {
interval := cfg.Engine.ReaperInterval
if interval <= 0 {
interval = 5 * time.Minute
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
cleaned, err := s.artifactReaper.Sweep(ctx)
if err != nil {
s.Logger.Error("artifact reaper sweep failed", "error", err)
} else if cleaned > 0 {
s.Logger.Info("artifact reaper cleaned expired artifacts", "count", cleaned)
}
}
}
}()
s.Logger.Info("artifact reaper started", "retention", retention)
}
// Periodically update queue depth metric.
go s.pollQueueDepth(ctx, cfg.Engine.ReaperInterval)
}
// Wrap the API mux with metrics middleware, auth, and rate limiting.
apiHandler := metricsMiddleware(apiMux)
if s.AuthStore != nil {
if s.Auditor != nil {
apiHandler = auth.AuthMiddleware(s.AuthStore, s.OIDCValidator, apiHandler, s.Auditor)
} else {
apiHandler = auth.AuthMiddleware(s.AuthStore, s.OIDCValidator, apiHandler)
}
}
rl := NewRateLimiter(100, 200)
apiHandler = rl.Middleware(apiHandler)
// Metrics behind auth — path labels include workflow names which are sensitive.
// Kubernetes probes bypass auth; Prometheus must be configured with an API key.
apiMux.Handle("/metrics", promhttp.Handler())
// Top-level mux: health probes bypass auth so Kubernetes can reach them
// without credentials. All other requests fall through to apiHandler.
mux := http.NewServeMux()
mux.Handle("/healthz", health.HealthzHandler())
// /readyz checks DB connectivity only; worker/reaper liveness is intentionally
// excluded to prevent flapping between poll cycles.
mux.Handle("/readyz", health.ReadyzHandler(s.DB))
mux.Handle("/", apiHandler)
handler := mux
s.httpServer = &http.Server{
Addr: s.Address,
Handler: handler,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
// Start cron scheduler.
if err := s.cron.Start(ctx); err != nil {
return fmt.Errorf("starting cron scheduler: %w", err)
}
s.Logger.Info("cron scheduler started")
// Start email trigger poller.
if err := s.emailPoller.Start(ctx); err != nil {
s.cron.Stop()
return fmt.Errorf("starting email trigger poller: %w", err)
}
s.Logger.Info("email trigger poller started")
// Start HTTP server.
errCh := make(chan error, 1)
go func() {
if s.TLSCertFile != "" && s.TLSKeyFile != "" {
s.Logger.Info("server listening with TLS", "address", s.Address)
if err := s.httpServer.ListenAndServeTLS(s.TLSCertFile, s.TLSKeyFile); err != nil && err != http.ErrServerClosed {
errCh <- err
}
} else {
slog.Warn("API server running without TLS — use a reverse proxy for production or configure tls.cert_file and tls.key_file")
s.Logger.Info("server listening", "address", s.Address)
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
errCh <- err
}
}
}()
// Wait for shutdown signal.
select {
case <-ctx.Done():
s.Logger.Info("shutting down...")
case err := <-errCh:
s.emailPoller.Stop()
return err
}
// Graceful shutdown: stop accepting new requests, wait for in-flight.
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.cron.Stop()
s.Logger.Info("cron scheduler stopped")
s.emailPoller.Stop()
s.Logger.Info("email trigger poller stopped")
if err := s.httpServer.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("server shutdown: %w", err)
}
s.Logger.Info("server stopped")
// Wait for in-flight executions.
s.waitForExecutions(shutdownCtx)
return nil
}
// executeWorkflow runs a workflow in the background, tracking it for graceful shutdown.
// The parent context is used to propagate authentication (team/user) information.
func (s *Server) executeWorkflow(parent context.Context, workflowName string, version int, inputs map[string]any) (string, error) {
// Propagate auth context from the original request so team scoping is preserved.
bgCtx := context.Background()
if u := auth.UserFromContext(parent); u != nil {
bgCtx = auth.WithUser(bgCtx, u)
}
ctx, cancel := context.WithCancel(bgCtx)
// Create execution record first to get the ID.
result, err := s.Engine.Execute(ctx, workflowName, version, inputs)
if err != nil {
cancel()
return "", err
}
// Track for graceful shutdown (in practice the execution is already done
// since Execute is synchronous in V1, but this prepares for async V1.1).
s.mu.Lock()
s.running[result.ExecutionID] = cancel
s.mu.Unlock()
go func() {
defer cancel()
defer func() {
s.mu.Lock()
delete(s.running, result.ExecutionID)
s.mu.Unlock()
}()
// Execution already completed in the Execute call above.
// In V1.1 with async execution, this is where we'd wait.
}()
return result.ExecutionID, nil
}
func (s *Server) waitForExecutions(ctx context.Context) {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
s.mu.Lock()
count := len(s.running)
s.mu.Unlock()
if count == 0 {
return
}
select {
case <-ctx.Done():
s.Logger.Warn("shutdown timeout, cancelling remaining executions", "count", count)
s.mu.Lock()
for _, cancel := range s.running {
cancel()
}
s.mu.Unlock()
return
case <-ticker.C:
}
}
}
// handleRun triggers a workflow execution via the API.
//
// @Summary Trigger a workflow execution
// @Description Triggers the latest applied version of the named workflow. Returns a 202 Accepted response with the new execution ID.
// @Tags executions
// @Produce json
// @Param workflow path string true "Workflow name"
// @Success 202 {object} RunResponse
// @Failure 400 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Security OIDCAuth
// @Router /api/v1/run/{workflow} [post]
func (s *Server) handleRun(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 1<<20) // 1MB limit
workflowName := r.PathValue("workflow")
if workflowName == "" {
http.Error(w, `{"error":"workflow name required"}`, http.StatusBadRequest)
return
}
ctx := r.Context()
version, err := getLatestVersion(ctx, s.DB, workflowName)
if err != nil || version == 0 {
writeJSONError(w, "workflow not found", http.StatusNotFound)
return
}
execID, err := s.executeWorkflow(r.Context(), workflowName, version, nil)
if err != nil {
s.Logger.Error("workflow execution failed", "workflow", workflowName, "error", err)
writeJSONError(w, "internal error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `{"execution_id":"%s","workflow":"%s","version":%d}`, execID, workflowName, version)
}
// handleCancel cancels a running execution via the API.
//
// @Summary Cancel a running execution
// @Description Sends a cancellation signal to a running execution. The execution may not stop immediately; poll the status endpoint to confirm.
// @Tags executions
// @Produce json
// @Param execution path string true "Execution ID (UUID)"
// @Success 200 {object} CancelResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Security OIDCAuth
// @Router /api/v1/cancel/{execution} [post]
func (s *Server) handleCancel(w http.ResponseWriter, r *http.Request) {
execID := r.PathValue("execution")
// Update DB status first (with team_id check) to prevent cross-tenant cancellation.
teamID := auth.TeamIDFromContext(r.Context())
result, err := s.DB.ExecContext(r.Context(),
`UPDATE workflow_executions SET status = 'cancelled', completed_at = NOW(), updated_at = NOW()
WHERE id = $1 AND status IN ('pending', 'running') AND team_id = $2`, execID, teamID)
if err != nil {
s.Logger.Error("cancel: db update failed", "execution", execID, "error", err)
writeJSONError(w, "internal error", http.StatusInternalServerError)
return
}
rows, _ := result.RowsAffected()
if rows == 0 {
writeJSONError(w, "execution not found or not cancellable", http.StatusNotFound)
return
}
// Only cancel the in-memory context after confirming team ownership via DB.
s.mu.Lock()
cancel, ok := s.running[execID]
s.mu.Unlock()
if ok {
cancel()
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"execution_id":"%s","status":"cancelled"}`, execID)
}
// pollQueueDepth periodically queries the count of pending steps and updates
// the Prometheus gauge. It runs on the same interval as the reaper.
func (s *Server) pollQueueDepth(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
var count int
err := s.DB.QueryRowContext(ctx,
`SELECT COUNT(*) FROM step_executions WHERE status = 'pending'`,
).Scan(&count)
if err != nil {
s.Logger.Error("failed to query queue depth", "error", err)
continue
}
metrics.SetQueueDepth(count)
}
}
}
func getLatestVersion(ctx context.Context, db *sql.DB, name string) (int, error) {
teamID := auth.TeamIDFromContext(ctx)
var version int
err := db.QueryRowContext(ctx,
`SELECT COALESCE(MAX(version), 0) FROM workflow_definitions WHERE name = $1 AND team_id = $2`, name, teamID,
).Scan(&version)
return version, err
}