11package main
22
33import (
4+ "context"
45 "os"
6+ "time"
57
68 "github.com/hellofresh/goengine"
79 "github.com/hellofresh/goengine/mongodb"
810 "github.com/hellofresh/goengine/rabbit"
9- "gopkg.in/mgo.v2"
11+ "github.com/mongodb/mongo-go-driver/mongo"
12+ "github.com/mongodb/mongo-go-driver/mongo/options"
13+ "github.com/mongodb/mongo-go-driver/mongo/readconcern"
14+ "github.com/mongodb/mongo-go-driver/mongo/readpref"
15+ "github.com/mongodb/mongo-go-driver/mongo/writeconcern"
1016)
1117
1218func main () {
1319 var streamName goengine.StreamName = "test"
1420
1521 mongoDSN := os .Getenv ("STORAGE_DSN" )
22+
1623 goengine .Log ("Connecting to the database" , map [string ]interface {}{"dsn" : mongoDSN }, nil )
17- session , err := mgo .Dial (mongoDSN )
24+ mongoClient , err := mongo .NewClientWithOptions (
25+ mongoDSN ,
26+ options .Client ().
27+ SetAppName ("goengine" ).
28+ SetReadConcern (readconcern .Linearizable ()).
29+ SetReadPreference (readpref .Nearest ()).
30+ SetWriteConcern (writeconcern .New (writeconcern .WMajority ())),
31+ )
32+ if err != nil {
33+ goengine .Log ("Failed to create new Mongo mongoClient" , nil , err )
34+ panic (err )
35+ }
36+
37+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
38+ defer cancel ()
39+ err = mongoClient .Connect (ctx )
1840 if err != nil {
1941 goengine .Log ("Failed to connect to Mongo" , nil , err )
2042 panic (err )
2143 }
22- defer session .Close ()
2344
24- // Optional. Switch the session to a monotonic behavior.
25- session .SetMode (mgo .Monotonic , true )
45+ defer func () {
46+ ctx , cancel = context .WithTimeout (context .Background (), 5 * time .Second )
47+ defer cancel ()
48+ if err := mongoClient .Disconnect (ctx ); err != nil {
49+ goengine .Log ("Failed to close connection to Mongo" , nil , err )
50+ panic (err )
51+ }
52+ }()
2653
2754 goengine .Log ("Setting up the registry" , nil , nil )
2855 registry := goengine .NewInMemoryTypeRegistry ()
@@ -35,7 +62,7 @@ func main() {
3562 bus := rabbit .NewEventBus (brokerDSN , "events" , "events" )
3663
3764 goengine .Log ("Setting up the event store" , nil , nil )
38- es := mongodb .NewEventStore (session , registry )
65+ es := mongodb .NewEventStore (mongoClient . Database ( "event_store" ) , registry )
3966
4067 eventDispatcher := goengine .NewVersionedEventDispatchManager (bus , registry )
4168 eventDispatcher .RegisterEventHandler (& RecipeCreated {}, func (event * goengine.DomainMessage ) error {
@@ -47,7 +74,7 @@ func main() {
4774 go eventDispatcher .Listen (stopChannel , false )
4875
4976 goengine .Log ("Creating a recipe" , nil , nil )
50- aggregateRoot := CreateScenario (streamName )
77+ aggregateRoot := CreateScenario ()
5178
5279 repository := goengine .NewPublisherRepository (es , bus )
5380 repository .Save (aggregateRoot , streamName )
@@ -62,7 +89,7 @@ func main() {
6289 stopChannel <- true
6390}
6491
65- func CreateScenario (streamName goengine. StreamName ) * Recipe {
92+ func CreateScenario () * Recipe {
6693 recipe := NewRecipe ("Test Recipe" )
6794 recipe .Rate (4 )
6895 return recipe
0 commit comments