Start and stop mongod with ps
parent
3a8af0551e
commit
b43c8b1f7a
|
|
@ -3,6 +3,8 @@ package config
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/args"
|
"local/args"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -17,6 +19,9 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() {
|
func New() {
|
||||||
|
if strings.Contains(fmt.Sprint(os.Args), "-test") {
|
||||||
|
return
|
||||||
|
}
|
||||||
as := args.NewArgSet()
|
as := args.NewArgSet()
|
||||||
as.Append(args.INT, "p", "port to listen on", 52222)
|
as.Append(args.INT, "p", "port to listen on", 52222)
|
||||||
as.Append(args.STRING, "d", "dir with public files", "./public")
|
as.Append(args.STRING, "d", "dir with public files", "./public")
|
||||||
|
|
|
||||||
4
main.go
4
main.go
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "local/cheqbooq/server"
|
import (
|
||||||
|
"local/cheqbooq/server"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if s, err := server.New(); err != nil {
|
if s, err := server.New(); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"local/cheqbooq/config"
|
"local/cheqbooq/config"
|
||||||
"strings"
|
"log"
|
||||||
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
|
@ -15,12 +15,41 @@ type Mongo struct {
|
||||||
client *mongo.Client
|
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)
|
ctx, can := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer can()
|
defer can()
|
||||||
|
|
||||||
opt := options.Client()
|
opt := options.Client()
|
||||||
opt.ApplyURI(fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://")))
|
opt.ApplyURI(addr)
|
||||||
|
|
||||||
client, err := mongo.Connect(ctx, opt)
|
client, err := mongo.Connect(ctx, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -37,15 +66,15 @@ func (m *Mongo) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mongo) Account() *mongo.Collection {
|
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 {
|
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 {
|
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 {
|
func (m *Mongo) Find(c *mongo.Collection, where interface{}, next func() interface{}) error {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestMongoNew(t *testing.T) {
|
||||||
|
_, err := NewMongo("mongodb://localhost:27017")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,18 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"local/cheqbooq/config"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
|
mongo *Mongo
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() (*Storage, error) {
|
func New() (*Storage, error) {
|
||||||
|
mongo, err := NewMongo(fmt.Sprintf("mongodb://%s", strings.TrimPrefix(config.StoreAddr, "mongodb://")))
|
||||||
|
return &Storage{
|
||||||
|
mongo: mongo,
|
||||||
|
}, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue