mongo2rethink/main.go

102 lines
1.8 KiB
Go
Raw Permalink Normal View History

2016-11-10 17:26:17 +01:00
// llmongo.go
2016-11-17 10:15:59 +01:00
package main
2016-11-10 17:26:17 +01:00
import (
"flag"
"fmt"
"log"
"os"
"sync"
"time"
)
const (
_Version = "v1.0.0"
_Producer = 0
_Consumer = 1
_Remover = 2
)
var (
loop bool
done chan bool
consume chan produced
wg sync.WaitGroup
status int
count *Counter
)
func main() {
flag.Usage = usage
flag.Parse()
if opts.Version {
fmt.Println(os.Args[0], _Version)
os.Exit(0)
}
if opts.Hostname == "" {
var err error
opts.Hostname, err = os.Hostname()
if err != nil {
fmt.Println("Hostname error: ", err.Error())
}
}
setTerm()
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Log file error: ", err.Error())
os.Exit(-4)
}
defer fs.Close()
log.SetOutput(fs)
pid.PIDFile = opts.Pidfile
pid.Write(true)
defer pid.Remove()
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
opts.Month = start.Format("0601")
dbs.connectMongo()
2016-11-17 10:09:23 +01:00
defer dbs.Mongo.Close()
2016-11-10 17:26:17 +01:00
dbs.poolRethink()
2016-11-17 10:09:23 +01:00
defer dbs.Rethink.Close()
2016-11-10 17:26:17 +01:00
if opts.Timeout > 0 {
time.AfterFunc(opts.Timeout, exit)
}
count = NewCounter()
consume = make(chan produced)
loop = true
done = make(chan bool)
go producer()
go consumer()
<-done
fmt.Printf("Done\n")
close(done)
fmt.Println("Waiting WG")
wg.Wait()
count.SetTime(time.Since(start))
fmt.Printf("Stop %v - user: %d - login: %d - insert: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetInsert(), count.GetErr(), count.GetRem(), count.GetDup())
log.Printf("Stop %v - user: %d - login: %d - insert: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetInsert(), count.GetErr(), count.GetRem(), count.GetDup())
}