diff --git a/.github/workflows/simulation.yaml b/.github/workflows/simulation.yaml new file mode 100644 index 00000000..773c779f --- /dev/null +++ b/.github/workflows/simulation.yaml @@ -0,0 +1,27 @@ +name: Publish Simulation Service +on: + workflow_dispatch: + +jobs: + build-and-push: + name: Build and Publish + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Get the version (release tag) + run: echo "IMAGE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./simulation/Dockerfile + push: true + tags: tfgco/maestro-simulation:${{ env.IMAGE_VERSION }} diff --git a/simulation/Dockerfile b/simulation/Dockerfile new file mode 100644 index 00000000..d9cd9c31 --- /dev/null +++ b/simulation/Dockerfile @@ -0,0 +1,18 @@ +FROM golang:1.23-alpine AS build + +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build -o room . + +FROM alpine:edge + +WORKDIR /app +COPY --from=build /app/room . + +RUN apk --no-cache add ca-certificates tzdata + +ENTRYPOINT ["/app/room"] diff --git a/simulation/app/server.go b/simulation/app/server.go new file mode 100644 index 00000000..f835803a --- /dev/null +++ b/simulation/app/server.go @@ -0,0 +1,38 @@ +package app + +import ( + "context" + "errors" + "net/http" + + ginadapter "git.topfreegames.com/maestro/test-service/internal/adapters/gin" + "git.topfreegames.com/maestro/test-service/internal/core" + "github.com/gin-gonic/gin" +) + +type Server struct { + RoomManager *core.RoomManager + httpServer *http.Server +} + +func (s *Server) Start() { + handler := ginadapter.RoomHandler{RoomManager: s.RoomManager} + + r := gin.Default() + r.PUT("/room", handler.UpdateGameRoom) + r.PUT("/status", handler.UpdateStatus) + r.PUT("/matches", handler.UpdateRunningMatches) + + s.httpServer = &http.Server{ + Addr: ":8080", + Handler: r.Handler(), + } + + if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + panic(err) + } +} + +func (s *Server) Shutdown(ctx context.Context) error { + return s.httpServer.Shutdown(ctx) +} diff --git a/simulation/app/woker.go b/simulation/app/woker.go new file mode 100644 index 00000000..496a7b89 --- /dev/null +++ b/simulation/app/woker.go @@ -0,0 +1,45 @@ +package app + +import ( + "context" + "log" + "time" + + "git.topfreegames.com/maestro/test-service/internal/core" +) + +type Worker struct { + Worker *core.Worker + + ticker *time.Ticker + quit chan bool + cancel context.CancelFunc +} + +func (w *Worker) Start() { + w.ticker = time.NewTicker(1 * time.Second) + w.quit = make(chan bool) + ctx, cancel := context.WithCancel(context.Background()) + w.cancel = cancel + + go func() { + for { + select { + case <-w.quit: + return + case <-w.ticker.C: + go w.Worker.Sync(ctx) + } + } + }() +} + +func (w *Worker) Stop(_ context.Context) { + defer w.cancel() + w.ticker.Stop() + + log.Println("Stopping worker...") + time.Sleep(2 * time.Second) // wait ongoing sync + w.quit <- true + log.Println("Worker stopped") +} diff --git a/simulation/go.mod b/simulation/go.mod new file mode 100644 index 00000000..fa7026e4 --- /dev/null +++ b/simulation/go.mod @@ -0,0 +1,47 @@ +module git.topfreegames.com/maestro/test-service + +go 1.23.2 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/knadh/koanf/parsers/yaml v0.1.0 + github.com/knadh/koanf/providers/env v1.0.0 + github.com/knadh/koanf/providers/file v1.1.2 + github.com/knadh/koanf/providers/structs v0.1.0 + github.com/knadh/koanf/v2 v2.1.2 +) + +require ( + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/fatih/structs v1.1.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect + github.com/go-viper/mapstructure/v2 v2.2.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect + github.com/knadh/koanf/maps v0.1.1 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.8.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/simulation/go.sum b/simulation/go.sum new file mode 100644 index 00000000..a9eb76f3 --- /dev/null +++ b/simulation/go.sum @@ -0,0 +1,63 @@ +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= +github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY= +github.com/knadh/koanf/providers/env v1.0.0/go.mod h1:mzFyRZueYhb37oPmC1HAv/oGEEuyvJDA98r3XAa8Gak= +github.com/knadh/koanf/providers/file v1.1.2/go.mod h1:/faSBcv2mxPVjFrXck95qeoyoZ5myJ6uxN8OOVNJJCI= +github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE= +github.com/knadh/koanf/v2 v2.1.2/go.mod h1:Gphfaen0q1Fc1HTgJgSTC4oRX9R2R5ErYMZJy8fLJBo= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/simulation/internal/adapters/gin/room.go b/simulation/internal/adapters/gin/room.go new file mode 100644 index 00000000..da3ba5c7 --- /dev/null +++ b/simulation/internal/adapters/gin/room.go @@ -0,0 +1,68 @@ +package gin + +import ( + "fmt" + + "git.topfreegames.com/maestro/test-service/internal/core" + "github.com/gin-gonic/gin" +) + +type RoomHandler struct { + RoomManager *core.RoomManager +} + +type Request struct { + Status string `form:"status"` + RunningMatches int `form:"running_matches"` +} + +func (h *RoomHandler) UpdateGameRoom(c *gin.Context) { + status, runningMatches, err := parseRequest(c) + if err != nil { + c.JSON(400, gin.H{"error": "invalid request"}) + return + } + + room := h.RoomManager.UpdateGameRoom(status, runningMatches) + + c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches}) +} + +func (h *RoomHandler) UpdateStatus(c *gin.Context) { + status, _, err := parseRequest(c) + if err != nil { + c.JSON(400, gin.H{"error": "invalid request"}) + return + } + + room := h.RoomManager.UpdateStatus(status) + + c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches}) +} + +func (h *RoomHandler) UpdateRunningMatches(c *gin.Context) { + _, runningMatches, err := parseRequest(c) + if err != nil { + c.JSON(400, gin.H{"error": "invalid request"}) + return + } + + room := h.RoomManager.UpdateRunningMatches(runningMatches) + + c.JSON(200, gin.H{"status": room.Status, "runningMatches": room.RunningMatches}) +} + +func parseRequest(c *gin.Context) (core.Status, int, error) { + var req Request + + if c.ShouldBind(&req) != nil { + return core.Unknown, 0, fmt.Errorf("invalid request") + } + + status, err := core.StatusFromString(req.Status) + if err != nil { + return core.Unknown, 0, err + } + + return status, req.RunningMatches, nil +} diff --git a/simulation/internal/core/config.go b/simulation/internal/core/config.go new file mode 100644 index 00000000..88609b61 --- /dev/null +++ b/simulation/internal/core/config.go @@ -0,0 +1,61 @@ +package core + +import ( + "log" + "strings" + "time" + + "github.com/knadh/koanf/providers/env" + "github.com/knadh/koanf/providers/structs" + "github.com/knadh/koanf/v2" +) + +type RoomConfig struct { + ID string `koanf:"id"` +} + +type SchedulerConfig struct { + Name string `koanf:"name"` +} + +type Config struct { + SyncInterval time.Duration `koanf:"syncinterval"` + InitialStatus string `koanf:"initialstatus"` + URL string `koanf:"url"` + Room RoomConfig `koanf:"room"` + Scheduler SchedulerConfig `koanf:"scheduler"` +} + +func NewDefaultConfig() *Config { + return &Config{ + SyncInterval: 5 * time.Second, + InitialStatus: "unready", + } +} + +func LoadConfig() *Config { + k := koanf.New(".") + + _ = k.Load(structs.Provider(NewDefaultConfig(), "room"), nil) + + // This is to load MAESTRO_* environment variables + if err := k.Load(env.Provider("", ".", parseEnv), nil); err != nil { + log.Fatalf("error loading config from env: %v", err) + } + + if err := k.Load(env.Provider("SIMULATION_", ".", parseEnv), nil); err != nil { + log.Fatalf("error loading config from env: %v", err) + } + + var config Config + if err := k.Unmarshal("", &config); err != nil { + log.Fatalf("error unmarshalling config: %v", err) + } + + return &config +} + +func parseEnv(s string) string { + return strings.Replace(strings.ToLower( + strings.TrimPrefix(s, "SIMULATION_")), "_", ".", -1) +} diff --git a/simulation/internal/core/entities.go b/simulation/internal/core/entities.go new file mode 100644 index 00000000..299044ac --- /dev/null +++ b/simulation/internal/core/entities.go @@ -0,0 +1,51 @@ +package core + +import "fmt" + +type Status int + +const ( + Unknown Status = iota - 1 + Pending + Unready + Ready + Occupied + Terminating + Error + Terminated + Active +) + +func (s Status) String() string { + if s == Unknown { + return "unknown" + } + + return []string{"pending", "unready", "ready", "occupied", "terminating", "error", "terminated", "active"}[s] +} + +func StatusFromString(value string) (Status, error) { + statuses := map[string]Status{ + "pending": Pending, + "unready": Unready, + "ready": Ready, + "occupied": Occupied, + "terminating": Terminating, + "error": Error, + "terminated": Terminated, + "active": Active, + } + + if status, ok := statuses[value]; ok { + return status, nil + } + + return Unknown, fmt.Errorf("invalid value for Status: %s", value) +} + +type Room struct { + ID string + Scheduler string + Status Status + RunningMatches int +} diff --git a/simulation/internal/core/room.go b/simulation/internal/core/room.go new file mode 100644 index 00000000..9229977f --- /dev/null +++ b/simulation/internal/core/room.go @@ -0,0 +1,24 @@ +package core + +type RoomManager struct { + room *Room +} + +func NewRoomManager(room *Room) *RoomManager { + return &RoomManager{room: room} +} + +func (m *RoomManager) UpdateGameRoom(status Status, runningMatches int) *Room { + m.room.Status = status + m.room.RunningMatches = runningMatches + + return m.room +} + +func (m *RoomManager) UpdateStatus(status Status) *Room { + return m.UpdateGameRoom(status, m.room.RunningMatches) +} + +func (m *RoomManager) UpdateRunningMatches(runningMatches int) *Room { + return m.UpdateGameRoom(m.room.Status, runningMatches) +} diff --git a/simulation/internal/core/worker.go b/simulation/internal/core/worker.go new file mode 100644 index 00000000..2c26cc84 --- /dev/null +++ b/simulation/internal/core/worker.go @@ -0,0 +1,47 @@ +package core + +import ( + "bytes" + "context" + "fmt" + "log" + "net/http" + "time" +) + +type Worker struct { + room *Room // No problem with concurrent reads, next loop will have updated values + http *http.Client + config *Config +} + +func NewWorker(room *Room, config *Config) *Worker { + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.IdleConnTimeout = 10 * time.Second + + return &Worker{ + config: config, + room: room, + http: &http.Client{ + Timeout: 1 * time.Second, + Transport: transport, + }, + } +} + +func (w *Worker) Sync(ctx context.Context) { + jbody := []byte(fmt.Sprintf(`{"status": "%s", "runningMatches": %d, "timestamp": %d}`, w.room.Status.String(), w.room.RunningMatches, time.Now().Unix())) + body := bytes.NewReader(jbody) + + request, err := http.NewRequestWithContext(ctx, http.MethodPut, fmt.Sprintf("%s/scheduler/%s/rooms/%s/ping", w.config.URL, w.room.Scheduler, w.room.ID), body) + if err != nil { + log.Printf("failed to create request: %s", err.Error()) + return + } + + _, err = w.http.Do(request) + if err != nil { + log.Printf("failed to send request: %s", err.Error()) + return + } +} diff --git a/simulation/main.go b/simulation/main.go new file mode 100644 index 00000000..e4551585 --- /dev/null +++ b/simulation/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" + "time" + + "git.topfreegames.com/maestro/test-service/app" + "git.topfreegames.com/maestro/test-service/internal/core" +) + +func main() { + config := core.LoadConfig() + + roomStatus, err := core.StatusFromString(config.InitialStatus) + if err != nil { + log.Fatalf("error parsing initial status: %v", err) + } + + room := &core.Room{ + ID: config.Room.ID, + Scheduler: config.Scheduler.Name, + Status: roomStatus, + RunningMatches: 0, + } + + roomManager := core.NewRoomManager(room) + worker := core.NewWorker(room, config) + + server := &app.Server{RoomManager: roomManager} + go server.Start() + + wrk := &app.Worker{Worker: worker} + go wrk.Start() + + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := server.Shutdown(ctx); err != nil { + log.Fatal("Server shutdown: ", err) + } + + wrk.Stop(ctx) + + select { + case <-ctx.Done(): + log.Println("Server shutdown timed out.") + } +}