eust-w 43326d6a5e feat(platform): land workflow, data tooling, and docs updates
Include the accumulated workflow scheduling, run history, subtree rerun, operator UI, deployment, data asset, object storage, database browser, and documentation changes currently in the worktree.
2026-04-24 12:29:46 +08:00

82 lines
2.1 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"time"
"droboticflow/internal/config"
"droboticflow/internal/retry"
"droboticflow/internal/store"
"droboticflow/internal/worker"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func connectMongo(ctx context.Context, mongoURI string, attempts int, delay time.Duration) (*mongo.Client, error) {
return retry.Do(ctx, attempts, delay, "mongodb connection", func(ctx context.Context, _ int) (*mongo.Client, error) {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
if err != nil {
return nil, err
}
if err := client.Ping(ctx, nil); err != nil {
_ = client.Disconnect(ctx)
return nil, err
}
return client, nil
})
}
func main() {
ctx := context.Background()
cfg := config.Load()
client, err := connectMongo(ctx, cfg.MongoURI, cfg.MongoConnectRetries, time.Duration(cfg.MongoConnectRetryDelayMS)*time.Millisecond)
if err != nil {
fmt.Printf("[worker] failed to connect mongo: %v\n", err)
os.Exit(1)
}
defer func() {
_ = client.Disconnect(context.Background())
}()
dataStore := store.NewMongoStore(client.Database(cfg.MongoDB))
runtime, err := worker.NewRuntime(
dataStore,
cfg.K8sNamespace,
cfg.K8sImagePullSecretName,
cfg.OperatorRunnerImage,
cfg.WorkspacePVCName,
cfg.WorkspaceMountPath,
)
if err != nil {
fmt.Printf("[worker] failed to initialize runtime: %v\n", err)
os.Exit(1)
}
go func() {
ticker := time.NewTicker(time.Duration(cfg.PollIntervalMS) * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
if err := runtime.ProcessAvailable(context.Background()); err != nil {
fmt.Printf("[worker] processAvailable failed: %v\n", err)
}
}
}()
server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%d", cfg.WorkerPort),
Handler: worker.NewHTTPHandler(dataStore, runtime),
ReadHeaderTimeout: 10 * time.Second,
}
fmt.Printf("[worker] listening on http://0.0.0.0:%d\n", cfg.WorkerPort)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Printf("[worker] failed to start: %v\n", err)
os.Exit(1)
}
}