Move game log processing into SCUM companion
This commit is contained in:
@@ -27,45 +27,20 @@ func main() {
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
trajectoryStatus := &companion.TrajectoryCollectionStatus{}
|
||||
collector, cleanup := buildTrajectoryCollector(config, trajectoryStatus)
|
||||
defer cleanup()
|
||||
consoleCollector, consoleCleanup := buildConsoleLogCollector(config, client)
|
||||
defer consoleCleanup()
|
||||
|
||||
registry := companion.NewHandlerRegistry(defaultHandlerAvailability(config), companion.RuntimeAdapter{
|
||||
BoundServerID: config.Component.ServerInstanceID,
|
||||
DiagnosticsState: map[string]string{
|
||||
"trajectory": trajectoryDiagnostic(config, collector),
|
||||
"coordinates": "raw-world",
|
||||
},
|
||||
})
|
||||
projection := companion.NewSCUMPlayerLogProjection(client, config.Component.ServerInstanceID)
|
||||
collector := companion.NewConsoleLogCollector(client, noopLogStore{}, config.Component.ServerInstanceID, os.Getenv(config.Proof.MaterialEnv))
|
||||
collector.OnSemanticEvents = projection.Handle
|
||||
runtime := companion.Runtime{
|
||||
Client: client,
|
||||
Dispatcher: companion.Dispatcher{
|
||||
Client: client,
|
||||
Registry: registry,
|
||||
PollLimit: 10,
|
||||
Backoff: 2 * time.Second,
|
||||
},
|
||||
Client: client,
|
||||
Dispatcher: companion.Dispatcher{Client: client, Registry: companion.NewHandlerRegistry(companion.HandlerAvailability{BoundServerID: config.Component.ServerInstanceID, Approved: true, Capabilities: map[string]bool{"companion.diagnostics": true}}, companion.RuntimeAdapter{BoundServerID: config.Component.ServerInstanceID}), PollLimit: 10, Backoff: 2 * time.Second},
|
||||
HeartbeatEvery: time.Duration(config.Timing.HeartbeatIntervalSeconds) * time.Second,
|
||||
PollEvery: time.Duration(config.Timing.CommandPollIntervalSeconds) * time.Second,
|
||||
Backoff: 2 * time.Second,
|
||||
Health: trajectoryStatus.HealthReport,
|
||||
}
|
||||
|
||||
errorsCh := make(chan error, 3)
|
||||
errorsCh := make(chan error, 2)
|
||||
go func() { errorsCh <- runtime.Run(ctx) }()
|
||||
if collector != nil {
|
||||
go func() { errorsCh <- collector.Run(ctx, trajectoryStatus) }()
|
||||
}
|
||||
if consoleCollector != nil {
|
||||
go func() { errorsCh <- consoleCollector.Run(ctx) }()
|
||||
}
|
||||
|
||||
err = <-errorsCh
|
||||
stop()
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
go func() { errorsCh <- collector.Run(ctx) }()
|
||||
if err := <-errorsCh; err != nil && !errors.Is(err, context.Canceled) {
|
||||
log.Printf("SCUM companion stopped: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -80,58 +55,12 @@ func loadConfig() (companion.Config, error) {
|
||||
return companion.LoadConfig(file)
|
||||
}
|
||||
|
||||
func buildConsoleLogCollector(config companion.Config, client *companion.Client) (*companion.ConsoleLogCollector, func()) {
|
||||
cleanup := func() {}
|
||||
store, err := companion.OpenSCUMSQLStoreFromEnv(companion.PlatformMySQLDSNEnvironment)
|
||||
if err != nil {
|
||||
log.Printf("SCUM companion console log store unavailable: %v", err)
|
||||
return nil, cleanup
|
||||
}
|
||||
cleanup = func() { _ = store.Close() }
|
||||
secret := os.Getenv(config.Proof.MaterialEnv)
|
||||
return companion.NewConsoleLogCollector(client, store, config.Component.ServerInstanceID, secret), cleanup
|
||||
}
|
||||
type noopLogStore struct{}
|
||||
|
||||
func buildTrajectoryCollector(config companion.Config, status *companion.TrajectoryCollectionStatus) (*companion.TrajectoryCollector, func()) {
|
||||
cleanup := func() {}
|
||||
if !config.Trajectory.Enabled {
|
||||
status.Record(companion.TrajectoryCollectionReport{Status: "healthy", Reason: "trajectory collection disabled"}, nil)
|
||||
return nil, cleanup
|
||||
}
|
||||
source, err := companion.OpenSCUMSQLiteSourceFromEnv(config.Trajectory.FileEnv)
|
||||
if err != nil {
|
||||
status.Record(companion.TrajectoryCollectionReport{Status: "degraded", Reason: "trajectory source unavailable"}, err)
|
||||
return nil, cleanup
|
||||
}
|
||||
store, err := companion.OpenSCUMSQLStoreFromEnv(companion.PlatformMySQLDSNEnvironment)
|
||||
if err != nil {
|
||||
_ = source.Close()
|
||||
status.Record(companion.TrajectoryCollectionReport{Status: "degraded", Reason: "trajectory store unavailable"}, err)
|
||||
return nil, cleanup
|
||||
}
|
||||
cleanup = func() {
|
||||
_ = source.Close()
|
||||
_ = store.Close()
|
||||
}
|
||||
return companion.NewTrajectoryCollector(config, source, store), cleanup
|
||||
func (noopLogStore) EnsureSchema(context.Context) error { return nil }
|
||||
func (noopLogStore) StoreConsoleRecords(context.Context, []companion.ConsoleRecord) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func defaultHandlerAvailability(config companion.Config) companion.HandlerAvailability {
|
||||
capabilities := map[string]bool{"companion.diagnostics": true}
|
||||
for _, capability := range config.Capabilities {
|
||||
if capability == "handler.vehicle.spawn" {
|
||||
capabilities["vehicle.spawn"] = true
|
||||
}
|
||||
}
|
||||
return companion.HandlerAvailability{BoundServerID: config.Component.ServerInstanceID, Approved: true, Capabilities: capabilities}
|
||||
}
|
||||
|
||||
func trajectoryDiagnostic(config companion.Config, collector *companion.TrajectoryCollector) string {
|
||||
if !config.Trajectory.Enabled {
|
||||
return "disabled"
|
||||
}
|
||||
if collector == nil {
|
||||
return "waiting"
|
||||
}
|
||||
return "enabled"
|
||||
func (noopLogStore) StoreSemanticEventBatch(_ context.Context, batch companion.SemanticEventBatch) (int, error) {
|
||||
return len(batch.Events), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user