Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes scheduler reminders app #241

Merged
merged 11 commits into from
Nov 26, 2024
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ bld/

# VS Code
.vscode/

# JetBrains
.idea
6 changes: 3 additions & 3 deletions scheduler-actor-reminders/Dockerfile-server
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ RUN go mod download
COPY . .

# Build the server app binary
RUN CGO_ENABLED=0 go build -o player-actor ./server/player-actor.go
RUN CGO_ENABLED=0 go build -o player-actor-server ./server

# Final stage
FROM alpine:latest

WORKDIR /server

# Copy binary from the builder stage
COPY --from=builder /server/player-actor .
COPY --from=builder /server/player-actor-server .

EXPOSE 3007

# Start the server
CMD ["/server/player-actor"]
CMD ["/server/player-actor-server"]
96 changes: 21 additions & 75 deletions scheduler-actor-reminders/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package api

import (
"context"
"fmt"

"github.com/dapr/go-sdk/actor"
dapr "github.com/dapr/go-sdk/client"
"github.com/dapr/go-sdk/examples/actor/api"
)

const playerActorType = "playerActorType"
const PlayerActorType = "playerActorType"

type ClientStub struct {
ActorID string
OnActivate func(context.Context) error
GetUser func(ctx context.Context) (*GetPlayerResponse, error)
Invoke func(context.Context, string) (string, error)
RevivePlayer func(context.Context, string) error
StartReminder func(context.Context, *ReminderRequest) error
StopReminder func(context.Context, *ReminderRequest) error
ReminderCall func(string, []byte, string, string) error
}

type PlayerActor struct {
actor.ServerImplBaseCtx
DaprClient dapr.Client
Health int
func (a *ClientStub) Type() string {
return PlayerActorType
}

func (p *PlayerActor) Type() string {
return playerActorType
func (a *ClientStub) ID() string {
return a.ActorID
}

type GetPlayerRequest struct {
Expand All @@ -30,67 +34,9 @@ type GetPlayerResponse struct {
Health int
}

// GetUser retrieving the state of the PlayerActor
func (p *PlayerActor) GetUser(ctx context.Context, player *GetPlayerRequest) (*GetPlayerResponse, error) {
if player.ActorID == p.ID() {
fmt.Printf("Player Actor ID: %s has a health level of: %d\n", p.ID(), p.Health)
return &GetPlayerResponse{
ActorID: p.ID(),
Health: p.Health,
}, nil
}
return nil, nil
}

// Invoke invokes an action on the actor
func (p *PlayerActor) Invoke(ctx context.Context, req string) (string, error) {
fmt.Println("get req = ", req)
return req, nil
}

// RevivePlayer revives the actor players health back to 100
func (p *PlayerActor) RevivePlayer(ctx context.Context, id string) error {
if id == p.ID() {
fmt.Printf("Reviving player: %s\n", id)
p.Health = 100
}

return nil
}

// ReminderCall executes logic to handle what happens when the reminder is triggered
// Dapr automatically calls this method when a reminder fires for the player actor
func (p *PlayerActor) ReminderCall(reminderName string, state []byte, dueTime string, period string) {
fmt.Println("receive reminder = ", reminderName, " state = ", string(state), "duetime = ", dueTime, "period = ", period)
if reminderName == "healthReminder" {
// Increase health if below 100
if p.Health < 100 {
p.Health += 10
if p.Health > 100 {
p.Health = 100
}
fmt.Printf("Player Actor health increased. Current health: %d\n", p.Health)
}
} else if reminderName == "healthDecayReminder" {
// Decrease health
p.Health -= 5
if p.Health < 0 {
fmt.Println("Player Actor died...")
}
fmt.Printf("Health decreased. Current health: %d\n", p.Health)
}

}

// StartReminder registers a reminder for the actor
func (p *PlayerActor) StartReminder(ctx context.Context, req *api.ReminderRequest) error {
fmt.Println("Starting reminder:", req.ReminderName)
return p.DaprClient.RegisterActorReminder(ctx, &dapr.RegisterActorReminderRequest{
ActorType: p.Type(),
ActorID: p.ID(),
Name: req.ReminderName,
DueTime: req.Duration,
Period: req.Period,
Data: []byte(req.Data),
})
type ReminderRequest struct {
ReminderName string `json:"reminder_name"`
DueTime string `json:"due_time"`
Period string `json:"period"`
Data string `json:"data"`
}
Loading