Skip to content

Commit

Permalink
Fixes scheduler reminders app (#241)
Browse files Browse the repository at this point in the history
* fixes reminders

Signed-off-by: Elena Kolevska <[email protected]>

* removes unnecessary context

Signed-off-by: Elena Kolevska <[email protected]>

* cleanup

Signed-off-by: Elena Kolevska <[email protected]>

* Uses stub for actor method calls

Signed-off-by: Elena Kolevska <[email protected]>

* More cleanup

Signed-off-by: Elena Kolevska <[email protected]>

* reorganises code to follow structure in other examples

Signed-off-by: Elena Kolevska <[email protected]>

* nil check

Signed-off-by: Elena Kolevska <[email protected]>

* Apply suggestions from code review

Co-authored-by: Cassie Coyle <[email protected]>
Signed-off-by: Elena Kolevska <[email protected]>

* clean up err check

Signed-off-by: Elena Kolevska <[email protected]>

* duh

Signed-off-by: Elena Kolevska <[email protected]>

---------

Signed-off-by: Elena Kolevska <[email protected]>
Signed-off-by: Elena Kolevska <[email protected]>
Co-authored-by: Cassie Coyle <[email protected]>
  • Loading branch information
elena-kolevska and cicoyle authored Nov 26, 2024
1 parent b0653a0 commit 561ea0c
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 251 deletions.
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

0 comments on commit 561ea0c

Please sign in to comment.