From b43c8b1f7ae2399e103de79b87b24c1c04e9163d Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 13 Apr 2020 00:02:17 +0000 Subject: [PATCH] Start and stop mongod with ps --- config/config.go | 5 +++++ main.go | 4 +++- storage/mongo.go | 43 ++++++++++++++++++++++++++++++++++++------- storage/mongo_test.go | 10 ++++++++++ storage/storage.go | 11 +++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 storage/mongo_test.go diff --git a/config/config.go b/config/config.go index 691a704..eddf120 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,8 @@ package config import ( "fmt" "local/args" + "os" + "strings" ) var ( @@ -17,6 +19,9 @@ func init() { } func New() { + if strings.Contains(fmt.Sprint(os.Args), "-test") { + return + } as := args.NewArgSet() as.Append(args.INT, "p", "port to listen on", 52222) as.Append(args.STRING, "d", "dir with public files", "./public") diff --git a/main.go b/main.go index aa49357..766ba85 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main -import "local/cheqbooq/server" +import ( + "local/cheqbooq/server" +) func main() { if s, err := server.New(); err != nil { diff --git a/storage/mongo.go b/storage/mongo.go index 6f0c036..ed2fec6 100644 --- a/storage/mongo.go +++ b/storage/mongo.go @@ -2,9 +2,9 @@ package storage import ( "context" - "fmt" "local/cheqbooq/config" - "strings" + "log" + "os/exec" "time" "go.mongodb.org/mongo-driver/mongo" @@ -15,12 +15,41 @@ type Mongo struct { client *mongo.Client } -func NewMongo() (*Mongo, error) { +func init() { + go func() { + kick := func() error { + cmd := exec.Command("bash", "-c", "true; until [ $(basename $PWD) == cheqbooq ]; do cd ..; done; NOFORK=1 bash ./testdata/start_mdb.sh") + b, err := cmd.CombinedOutput() + if err != nil { + log.Printf("%s", b) + } + return err + } + block := func() error { + cmd := exec.Command("bash", "-c", "true; tail --pid=$(ps aux | grep mongod | grep -v grep | awk '{print $2}') -f /dev/null") + b, err := cmd.CombinedOutput() + if err != nil { + log.Printf("%s", b) + } + return err + } + for { + if err := kick(); err != nil { + log.Println(err) + } + if err := block(); err != nil { + log.Println(err) + } + } + }() +} + +func NewMongo(addr string) (*Mongo, error) { ctx, can := context.WithTimeout(context.Background(), time.Second*5) defer can() opt := options.Client() - opt.ApplyURI(fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://"))) + opt.ApplyURI(addr) client, err := mongo.Connect(ctx, opt) if err != nil { @@ -37,15 +66,15 @@ func (m *Mongo) Close() error { } func (m *Mongo) Account() *mongo.Collection { - return m.Database(config.StoreNS).Collection("account") + return m.client.Database(config.StoreNS).Collection("account") } func (m *Mongo) Balance() *mongo.Collection { - return m.Database(config.StoreNS).Collection("balance") + return m.client.Database(config.StoreNS).Collection("balance") } func (m *Mongo) Transaction() *mongo.Collection { - return m.Database(config.StoreNS).Collection("transaction") + return m.client.Database(config.StoreNS).Collection("transaction") } func (m *Mongo) Find(c *mongo.Collection, where interface{}, next func() interface{}) error { diff --git a/storage/mongo_test.go b/storage/mongo_test.go new file mode 100644 index 0000000..76fff3c --- /dev/null +++ b/storage/mongo_test.go @@ -0,0 +1,10 @@ +package storage + +import "testing" + +func TestMongoNew(t *testing.T) { + _, err := NewMongo("mongodb://localhost:27017") + if err != nil { + t.Fatal(err) + } +} diff --git a/storage/storage.go b/storage/storage.go index 16f9ebf..8fe0fae 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -1,7 +1,18 @@ package storage +import ( + "fmt" + "local/cheqbooq/config" + "strings" +) + type Storage struct { + mongo *Mongo } func New() (*Storage, error) { + mongo, err := NewMongo(fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://"))) + return &Storage{ + mongo: mongo, + }, err }