feat: runtime manager
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user