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
@@ -6,6 +6,15 @@
// The two streams are intentionally separate: each one carries a single
// command kind, which keeps the consumer-side logic in Runtime Manager
// simple and avoids a `kind` discriminator inside the message body.
//
// Envelope shape per `rtmanager/api/runtime-jobs-asyncapi.yaml`:
//
// - `runtime:start_jobs` — `{game_id, image_ref, requested_at_ms}`,
// - `runtime:stop_jobs` — `{game_id, reason, requested_at_ms}`.
//
// The producer-supplied `image_ref` is resolved by the caller from the
// game's `target_engine_version` and the configured engine-image
// template; Runtime Manager never resolves engine versions itself.
package runtimemanager
import (
@@ -75,20 +84,45 @@ func NewPublisher(cfg Config) (*Publisher, error) {
}, nil
}
// PublishStartJob appends one start-job event for gameID to the
// configured start-jobs stream.
func (publisher *Publisher) PublishStartJob(ctx context.Context, gameID string) error {
return publisher.publish(ctx, "publish start job", publisher.startJobsStream, gameID)
// PublishStartJob appends one start-job event for gameID with the
// resolved imageRef to the configured start-jobs stream.
func (publisher *Publisher) PublishStartJob(ctx context.Context, gameID, imageRef string) error {
const op = "publish start job"
if err := publisher.checkCommon(op, ctx, gameID); err != nil {
return err
}
if strings.TrimSpace(imageRef) == "" {
return fmt.Errorf("%s: image ref must not be empty", op)
}
values := map[string]any{
"game_id": gameID,
"image_ref": imageRef,
"requested_at_ms": publisher.clock().UTC().UnixMilli(),
}
return publisher.xadd(ctx, op, publisher.startJobsStream, values)
}
// PublishStopJob appends one stop-job event for gameID to the configured
// stop-jobs stream. In Lobby publishes stop jobs only from the
// orphan-container path inside the runtimejobresult worker.
func (publisher *Publisher) PublishStopJob(ctx context.Context, gameID string) error {
return publisher.publish(ctx, "publish stop job", publisher.stopJobsStream, gameID)
// PublishStopJob appends one stop-job event for gameID classified by
// reason to the configured stop-jobs stream.
func (publisher *Publisher) PublishStopJob(ctx context.Context, gameID string, reason ports.StopReason) error {
const op = "publish stop job"
if err := publisher.checkCommon(op, ctx, gameID); err != nil {
return err
}
if err := reason.Validate(); err != nil {
return fmt.Errorf("%s: %w", op, err)
}
values := map[string]any{
"game_id": gameID,
"reason": reason.String(),
"requested_at_ms": publisher.clock().UTC().UnixMilli(),
}
return publisher.xadd(ctx, op, publisher.stopJobsStream, values)
}
func (publisher *Publisher) publish(ctx context.Context, op, stream, gameID string) error {
func (publisher *Publisher) checkCommon(op string, ctx context.Context, gameID string) error {
if publisher == nil || publisher.client == nil {
return fmt.Errorf("%s: nil publisher", op)
}
@@ -98,11 +132,10 @@ func (publisher *Publisher) publish(ctx context.Context, op, stream, gameID stri
if strings.TrimSpace(gameID) == "" {
return fmt.Errorf("%s: game id must not be empty", op)
}
return nil
}
values := map[string]any{
"game_id": gameID,
"requested_at_ms": publisher.clock().UTC().UnixMilli(),
}
func (publisher *Publisher) xadd(ctx context.Context, op, stream string, values map[string]any) error {
if _, err := publisher.client.XAdd(ctx, &redis.XAddArgs{
Stream: stream,
Values: values,
@@ -7,6 +7,7 @@ import (
"time"
"galaxy/lobby/internal/adapters/runtimemanager"
"galaxy/lobby/internal/ports"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
@@ -60,12 +61,13 @@ func TestPublishStartJobAppendsToStartStream(t *testing.T) {
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1"))
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1", "galaxy/game:v1.0.0"))
entries, err := client.XRange(context.Background(), "runtime:start_jobs", "-", "+").Result()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "game-1", entries[0].Values["game_id"])
assert.Equal(t, "galaxy/game:v1.0.0", entries[0].Values["image_ref"])
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
stop, err := client.XLen(context.Background(), "runtime:stop_jobs").Result()
@@ -73,16 +75,29 @@ func TestPublishStartJobAppendsToStartStream(t *testing.T) {
assert.Equal(t, int64(0), stop, "stop stream must remain empty")
}
func TestPublisherStartJobIncludesImageRef(t *testing.T) {
publisher, _, client := newTestPublisher(t, nil)
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1", "registry.example.com/galaxy/game:v1.4.7"))
entries, err := client.XRange(context.Background(), "runtime:start_jobs", "-", "+").Result()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "registry.example.com/galaxy/game:v1.4.7", entries[0].Values["image_ref"],
"image_ref field must be present in the start envelope")
}
func TestPublishStopJobAppendsToStopStream(t *testing.T) {
now := time.Date(2026, 4, 25, 13, 0, 0, 0, time.UTC)
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2"))
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2", ports.StopReasonOrphanCleanup))
entries, err := client.XRange(context.Background(), "runtime:stop_jobs", "-", "+").Result()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "game-2", entries[0].Values["game_id"])
assert.Equal(t, "orphan_cleanup", entries[0].Values["reason"])
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
startLen, err := client.XLen(context.Background(), "runtime:start_jobs").Result()
@@ -90,18 +105,44 @@ func TestPublishStopJobAppendsToStopStream(t *testing.T) {
assert.Equal(t, int64(0), startLen, "start stream must remain empty")
}
func TestPublisherStopJobIncludesReason(t *testing.T) {
publisher, _, client := newTestPublisher(t, nil)
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2", ports.StopReasonCancelled))
entries, err := client.XRange(context.Background(), "runtime:stop_jobs", "-", "+").Result()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "cancelled", entries[0].Values["reason"],
"reason field must be present in the stop envelope")
}
func TestPublishRejectsEmptyGameID(t *testing.T) {
publisher, _, _ := newTestPublisher(t, nil)
require.Error(t, publisher.PublishStartJob(context.Background(), ""))
require.Error(t, publisher.PublishStopJob(context.Background(), " "))
require.Error(t, publisher.PublishStartJob(context.Background(), "", "galaxy/game:v1.0.0"))
require.Error(t, publisher.PublishStopJob(context.Background(), " ", ports.StopReasonCancelled))
}
func TestPublishStartJobRejectsEmptyImageRef(t *testing.T) {
publisher, _, _ := newTestPublisher(t, nil)
require.Error(t, publisher.PublishStartJob(context.Background(), "game-1", ""))
require.Error(t, publisher.PublishStartJob(context.Background(), "game-1", " "))
}
func TestPublishStopJobRejectsUnknownReason(t *testing.T) {
publisher, _, _ := newTestPublisher(t, nil)
require.Error(t, publisher.PublishStopJob(context.Background(), "game-1", ports.StopReason("")))
require.Error(t, publisher.PublishStopJob(context.Background(), "game-1", ports.StopReason("unknown_reason")))
}
func TestPublishRejectsNilContext(t *testing.T) {
publisher, _, _ := newTestPublisher(t, nil)
require.Error(t, publisher.PublishStartJob(nilContext(), "game-1"))
require.Error(t, publisher.PublishStopJob(nilContext(), "game-1"))
require.Error(t, publisher.PublishStartJob(nilContext(), "game-1", "galaxy/game:v1.0.0"))
require.Error(t, publisher.PublishStopJob(nilContext(), "game-1", ports.StopReasonCancelled))
}
// nilContext returns an explicit untyped nil to exercise the defensive