-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: add durable statestore example #102
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
51fe420
add durable statestore example
sdil 5111076
rearrange check if target account id is same as the actor account id
sdil 83cc317
check if target account id is same as the actor account id
sdil 3137dcf
Merge branch 'add-statestore-example' of personal:sdil/ego into add-s…
sdil a8bd59a
move the examples folder
sdil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022-2024 Tochemey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/tochemey/ego/v3" | ||
samplepb "github.com/tochemey/ego/v3/example/pbs/sample/pb/v1" | ||
"github.com/tochemey/ego/v3/plugins/eventstore/memory" | ||
) | ||
|
||
func main() { | ||
// create the go context | ||
ctx := context.Background() | ||
// create the event store | ||
eventStore := memory.NewEventsStore() | ||
// connect the event store | ||
_ = eventStore.Connect(ctx) | ||
// create the ego engine | ||
engine := ego.NewEngine("Sample", eventStore) | ||
// start ego engine | ||
_ = engine.Start(ctx) | ||
// create a persistence id | ||
entityID := uuid.NewString() | ||
// create an entity behavior with a given id | ||
behavior := NewAccountBehavior(entityID) | ||
// create an entity | ||
_ = engine.Entity(ctx, behavior) | ||
|
||
// send some commands to the pid | ||
var command proto.Message | ||
// create an account | ||
command = &samplepb.CreateAccount{ | ||
AccountId: entityID, | ||
AccountBalance: 500.00, | ||
} | ||
// send the command to the actor. Please don't ignore the error in production grid code | ||
reply, _, _ := engine.SendCommand(ctx, entityID, command, time.Minute) | ||
account := reply.(*samplepb.Account) | ||
log.Printf("current balance on opening: %v", account.GetAccountBalance()) | ||
|
||
// send another command to credit the balance | ||
command = &samplepb.CreditAccount{ | ||
AccountId: entityID, | ||
Balance: 250, | ||
} | ||
|
||
reply, _, _ = engine.SendCommand(ctx, entityID, command, time.Minute) | ||
account = reply.(*samplepb.Account) | ||
log.Printf("current balance after a credit of 250: %v", account.GetAccountBalance()) | ||
|
||
// capture ctrl+c | ||
interruptSignal := make(chan os.Signal, 1) | ||
signal.Notify(interruptSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
<-interruptSignal | ||
|
||
// disconnect the event store | ||
_ = eventStore.Disconnect(ctx) | ||
// stop the actor system | ||
_ = engine.Stop(ctx) | ||
os.Exit(0) | ||
} | ||
|
||
// AccountBehavior implements EventSourcedBehavior | ||
type AccountBehavior struct { | ||
id string | ||
} | ||
|
||
// make sure that AccountBehavior is a true persistence behavior | ||
var _ ego.EventSourcedBehavior = &AccountBehavior{} | ||
|
||
// NewAccountBehavior creates an instance of AccountBehavior | ||
func NewAccountBehavior(id string) *AccountBehavior { | ||
return &AccountBehavior{id: id} | ||
} | ||
|
||
// ID returns the id | ||
func (a *AccountBehavior) ID() string { | ||
return a.id | ||
} | ||
|
||
// InitialState returns the initial state | ||
func (a *AccountBehavior) InitialState() ego.State { | ||
return ego.State(new(samplepb.Account)) | ||
} | ||
|
||
// HandleCommand handles every command that is sent to the persistent behavior | ||
func (a *AccountBehavior) HandleCommand(_ context.Context, command ego.Command, _ ego.State) (events []ego.Event, err error) { | ||
switch cmd := command.(type) { | ||
case *samplepb.CreateAccount: | ||
// TODO in production grid app validate the command using the prior state | ||
return []ego.Event{ | ||
&samplepb.AccountCreated{ | ||
AccountId: cmd.GetAccountId(), | ||
AccountBalance: cmd.GetAccountBalance(), | ||
}, | ||
}, nil | ||
|
||
case *samplepb.CreditAccount: | ||
// TODO in production grid app validate the command using the prior state | ||
return []ego.Event{ | ||
&samplepb.AccountCredited{ | ||
AccountId: cmd.GetAccountId(), | ||
AccountBalance: cmd.GetBalance(), | ||
}, | ||
}, nil | ||
|
||
default: | ||
return nil, errors.New("unhandled command") | ||
} | ||
} | ||
|
||
// HandleEvent handles every event emitted | ||
func (a *AccountBehavior) HandleEvent(_ context.Context, event ego.Event, priorState ego.State) (state ego.State, err error) { | ||
switch evt := event.(type) { | ||
case *samplepb.AccountCreated: | ||
return &samplepb.Account{ | ||
AccountId: evt.GetAccountId(), | ||
AccountBalance: evt.GetAccountBalance(), | ||
}, nil | ||
|
||
case *samplepb.AccountCredited: | ||
account := priorState.(*samplepb.Account) | ||
bal := account.GetAccountBalance() + evt.GetAccountBalance() | ||
return &samplepb.Account{ | ||
AccountId: evt.GetAccountId(), | ||
AccountBalance: bal, | ||
}, nil | ||
|
||
default: | ||
return nil, errors.New("unhandled event") | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly remove this EOF character by inserting a space