feat: runtime manager

This commit is contained in:
Ilia Denisov
2026-04-28 20:39:18 +02:00
committed by GitHub
parent e0a99b346b
commit a7cee15115
289 changed files with 45660 additions and 2207 deletions
+21 -6
View File
@@ -14,6 +14,7 @@ import (
"time"
"galaxy/lobby/internal/domain/common"
"galaxy/lobby/internal/domain/engineimage"
"galaxy/lobby/internal/domain/game"
"galaxy/lobby/internal/logging"
"galaxy/lobby/internal/ports"
@@ -23,11 +24,12 @@ import (
// Service executes the start-game use case.
type Service struct {
games ports.GameStore
runtimeManager ports.RuntimeManager
clock func() time.Time
logger *slog.Logger
telemetry *telemetry.Runtime
games ports.GameStore
runtimeManager ports.RuntimeManager
imageResolver *engineimage.Resolver
clock func() time.Time
logger *slog.Logger
telemetry *telemetry.Runtime
}
// Dependencies groups the collaborators used by Service.
@@ -38,6 +40,11 @@ type Dependencies struct {
// RuntimeManager publishes the start job after the CAS succeeds.
RuntimeManager ports.RuntimeManager
// ImageResolver substitutes a game's TargetEngineVersion into the
// configured engine-image template to produce the `image_ref`
// published on `runtime:start_jobs`.
ImageResolver *engineimage.Resolver
// Clock supplies the wall-clock used for UpdatedAt. Defaults to
// time.Now when nil.
Clock func() time.Time
@@ -58,6 +65,8 @@ func NewService(deps Dependencies) (*Service, error) {
return nil, errors.New("new start game service: nil game store")
case deps.RuntimeManager == nil:
return nil, errors.New("new start game service: nil runtime manager")
case deps.ImageResolver == nil:
return nil, errors.New("new start game service: nil image resolver")
}
clock := deps.Clock
@@ -72,6 +81,7 @@ func NewService(deps Dependencies) (*Service, error) {
return &Service{
games: deps.Games,
runtimeManager: deps.RuntimeManager,
imageResolver: deps.ImageResolver,
clock: clock,
logger: logger.With("service", "lobby.startgame"),
telemetry: deps.Telemetry,
@@ -127,6 +137,11 @@ func (service *Service) Handle(ctx context.Context, input Input) (game.Game, err
)
}
imageRef, err := service.imageResolver.Resolve(record.TargetEngineVersion)
if err != nil {
return game.Game{}, fmt.Errorf("start game: resolve image ref: %w", err)
}
at := service.clock().UTC()
if err := service.games.UpdateStatus(ctx, ports.UpdateStatusInput{
GameID: input.GameID,
@@ -144,7 +159,7 @@ func (service *Service) Handle(ctx context.Context, input Input) (game.Game, err
string(game.TriggerCommand),
)
if err := service.runtimeManager.PublishStartJob(ctx, input.GameID.String()); err != nil {
if err := service.runtimeManager.PublishStartJob(ctx, input.GameID.String(), imageRef); err != nil {
// Status is already `starting` and the domain forbids a direct
// rollback to `ready_to_start`. We surface the publish error to
// the caller; the game stays in `starting` until either a