Skip to content

Commit b761cd3

Browse files
committed
Optional redis flush
1 parent f2ba9b8 commit b761cd3

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

cmd/server/main.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ import (
2323

2424
var (
2525
viewRadius = flag.Int("radius", 15, "Radius used for visibility and for chunk size")
26-
initialTestEntities = flag.Int("test-entities", 100, "Amount of test entities (a #)")
26+
initialTestEntities = flag.Int("test-entities", 100, "Amount of test entities (a #). This will only trigger if redis database is flushed.")
2727
redisAddr = flag.String("redis-addr", "localhost:6379", "Redis address")
2828
redisUsername = flag.String("redis-username", "", "Redis username")
2929
redisPassword = flag.String("redis-password", "", "Redis password")
30+
redisFlush = flag.Bool("redis-flush", true, "If enabled, it empties all database when the server starts")
3031
jeagerEndpoint = flag.String("jaeger-endpoint", "http://localhost:14268/api/traces", "Jaeger collector endpoint")
3132
natsURL = flag.String("nats-url", "", "NATS server url")
3233
tickDuration = flag.Duration("tick", 1*time.Second, "Tick duration")
@@ -48,7 +49,9 @@ func main() {
4849
Username: *redisUsername,
4950
Password: *redisPassword,
5051
})
51-
rdb.FlushAll(context.Background())
52+
if *redisFlush == true {
53+
rdb.FlushAll(context.Background())
54+
}
5255
rdb.AddHook(redisotel.TracingHook{})
5356

5457
store := components.NewRedisStore(rdb, logger)
@@ -88,24 +91,27 @@ func main() {
8891
vision.HandleRemovedComponent(ctx, t.Current(), string(componentType), entity)
8992
})
9093

91-
go func() {
92-
for i := 0; i < *initialTestEntities; i++ {
93-
entity, err := registry.NewEntity(context.Background())
94-
if err != nil {
95-
panic(err)
96-
}
97-
err = registry.CreateComponents(context.Background(), entity,
98-
&components.Position{
99-
X: rand.Int63n(60) - 30,
100-
Y: rand.Int63n(60) - 30,
101-
},
102-
&components.Render{Char: "#", Color: 0xff7f00},
103-
)
104-
if err != nil {
105-
panic(err)
94+
if *redisFlush == true {
95+
// Only create initial test entities if database was flushed
96+
go func() {
97+
for i := 0; i < *initialTestEntities; i++ {
98+
entity, err := registry.NewEntity(context.Background())
99+
if err != nil {
100+
panic(err)
101+
}
102+
err = registry.CreateComponents(context.Background(), entity,
103+
&components.Position{
104+
X: rand.Int63n(60) - 30,
105+
Y: rand.Int63n(60) - 30,
106+
},
107+
&components.Render{Char: "#", Color: 0xff7f00},
108+
)
109+
if err != nil {
110+
panic(err)
111+
}
106112
}
107-
}
108-
}()
113+
}()
114+
}
109115

110116
grpcServer(registry, geo, vision, movement, chat, t)
111117
}

0 commit comments

Comments
 (0)