113 lines
2.1 KiB
Go
113 lines
2.1 KiB
Go
// llmongo.go
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
type Options struct {
|
|
RedisTTL time.Duration
|
|
CurrentPath string
|
|
Exe string
|
|
LogFile string
|
|
Timeout bool
|
|
Debug bool
|
|
Version bool
|
|
}
|
|
|
|
const (
|
|
_VERSION = "v2.0.0"
|
|
)
|
|
|
|
var (
|
|
opts = Options{
|
|
RedisTTL: time.Hour * 11688, // 16 mesi
|
|
LogFile: "log/llmongo.log",
|
|
}
|
|
|
|
loop = true
|
|
ttl = time.Second * 55
|
|
done = make(chan bool)
|
|
msgs = make(chan string)
|
|
|
|
count = 0
|
|
errCount = 0
|
|
)
|
|
|
|
func usage() {
|
|
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <ttl> -l <logfile> -T -D -v\n")
|
|
os.Exit(0)
|
|
}
|
|
|
|
func init() {
|
|
var err error
|
|
opts.CurrentPath, err = filepath.Abs(filepath.Dir(os.Args[0]))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
opts.LogFile = path.Join(opts.CurrentPath, opts.LogFile)
|
|
pid.PIDFile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
|
|
opts.Exe = path.Base(os.Args[0])
|
|
|
|
flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb")
|
|
flag.StringVar(&dbs.RedisUri, "r", dbs.RedisUri, "Redis")
|
|
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
|
|
flag.DurationVar(&opts.RedisTTL, "t", opts.RedisTTL, "Redis TTL")
|
|
flag.BoolVar(&opts.Version, "v", false, "Version")
|
|
flag.BoolVar(&opts.Timeout, "T", false, "Timeout")
|
|
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
|
}
|
|
|
|
func stopLoop() {
|
|
loop = false
|
|
}
|
|
|
|
func main() {
|
|
flag.Usage = usage
|
|
flag.Parse()
|
|
|
|
if opts.Version {
|
|
fmt.Println(os.Args[0], _VERSION)
|
|
os.Exit(0)
|
|
}
|
|
|
|
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.Write(true)
|
|
defer pid.Remove()
|
|
|
|
start := time.Now()
|
|
fmt.Printf("Start: %+v\n", opts)
|
|
log.Printf("Start: %+v\n", opts)
|
|
|
|
dbs.poolRedis()
|
|
defer dbs.rdb.Close()
|
|
|
|
dbs.connectMongo()
|
|
defer dbs.mdb.Close()
|
|
|
|
if opts.Timeout {
|
|
time.AfterFunc(ttl, stopLoop)
|
|
}
|
|
|
|
go producer()
|
|
go consumer()
|
|
<-done
|
|
|
|
fmt.Printf("Stop %v - login: %d - errors: %d\n\r", time.Since(start), count, errCount)
|
|
log.Printf("Stop %v - login: %d - errors: %d\n\r", time.Since(start), count, errCount)
|
|
}
|