Start and stop mongod with ps

master
bel 2020-04-13 00:02:17 +00:00
parent 3a8af0551e
commit b43c8b1f7a
5 changed files with 65 additions and 8 deletions

View File

@ -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")

View File

@ -1,6 +1,8 @@
package main
import "local/cheqbooq/server"
import (
"local/cheqbooq/server"
)
func main() {
if s, err := server.New(); err != nil {

View File

@ -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 {

10
storage/mongo_test.go Normal file
View File

@ -0,0 +1,10 @@
package storage
import "testing"
func TestMongoNew(t *testing.T) {
_, err := NewMongo("mongodb://localhost:27017")
if err != nil {
t.Fatal(err)
}
}

View File

@ -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
}