aggiunta la gestione errori
dopo 20sec si killa se bloccato dopo il timeout
This commit is contained in:
parent
d7d68f4bf4
commit
b095dea871
8 changed files with 99 additions and 38 deletions
97
consumer.go
97
consumer.go
|
@ -19,25 +19,24 @@ type consumed struct {
|
||||||
empty bool
|
empty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []Ips, e string) bool {
|
|
||||||
for _, a := range s {
|
|
||||||
if a.IP == e {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func consumer() {
|
func consumer() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
prod := <-consume
|
prod := <-consume
|
||||||
|
|
||||||
var bulk = make(map[string]*mgo.Bulk)
|
status = _Consumer
|
||||||
|
|
||||||
bulk[opts.Month] = dbs.ll.Bulk()
|
var bulk = make(map[string]*mgo.Bulk)
|
||||||
bulk[opts.Month].Unordered()
|
var col = make(map[string]*mgo.Collection)
|
||||||
|
var slogin = make(map[string][]string)
|
||||||
|
|
||||||
|
if opts.Bulk {
|
||||||
|
bulk[opts.Month] = dbs.ll.Bulk()
|
||||||
|
bulk[opts.Month].Unordered()
|
||||||
|
} else {
|
||||||
|
col[opts.Month] = dbs.ll
|
||||||
|
}
|
||||||
|
|
||||||
cons := consumed{
|
cons := consumed{
|
||||||
user: prod.user,
|
user: prod.user,
|
||||||
|
@ -81,40 +80,80 @@ func consumer() {
|
||||||
|
|
||||||
if opts.Month != ml.Date.Format("0601") {
|
if opts.Month != ml.Date.Format("0601") {
|
||||||
dt := fmt.Sprintf("lastlogin_%s", ml.Date.Format("0601"))
|
dt := fmt.Sprintf("lastlogin_%s", ml.Date.Format("0601"))
|
||||||
if _, ok := bulk[dt]; !ok {
|
if opts.Bulk {
|
||||||
bulk[dt] = dbs.mdb.DB("lastlogin").C(dt).Bulk()
|
if _, ok := bulk[dt]; !ok {
|
||||||
bulk[dt].Unordered()
|
bulk[dt] = dbs.mdb.DB("lastlogin").C(dt).Bulk()
|
||||||
|
bulk[dt].Unordered()
|
||||||
|
}
|
||||||
|
bulk[dt].Insert(ml)
|
||||||
|
slogin[dt] = append(slogin[dt], login)
|
||||||
|
} else {
|
||||||
|
if _, ok := col[dt]; !ok {
|
||||||
|
col[dt] = dbs.mdb.DB("lastlogin").C(dt)
|
||||||
|
}
|
||||||
|
err = col[dt].Insert(ml)
|
||||||
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), "E11000") {
|
||||||
|
fmt.Printf("Err: %+v\n", err)
|
||||||
|
cons.error = true
|
||||||
|
count.AddErr(1)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
count.AddDuplicate(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cons.logins = append(cons.logins, login)
|
||||||
}
|
}
|
||||||
bulk[dt].Insert(ml)
|
|
||||||
} else {
|
} else {
|
||||||
// inserisce il login su Mongodb
|
// inserisce il login su Mongodb
|
||||||
|
if opts.Bulk {
|
||||||
bulk[opts.Month].Insert(ml)
|
bulk[opts.Month].Insert(ml)
|
||||||
// inserisce last timestamp su redis per consolidamento
|
slogin[opts.Month] = append(slogin[opts.Month], login)
|
||||||
|
} else {
|
||||||
|
err = col[opts.Month].Insert(ml)
|
||||||
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), "E11000") {
|
||||||
|
fmt.Printf("Err: %+v\n", err)
|
||||||
|
cons.error = true
|
||||||
|
count.AddErr(1)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
count.AddDuplicate(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cons.logins = append(cons.logins, login)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cons.logins = append(cons.logins, login)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, _ := range bulk {
|
if opts.Bulk {
|
||||||
_, err := bulk[key].Run()
|
for key, _ := range bulk {
|
||||||
if err != nil {
|
_, err := bulk[key].Run()
|
||||||
|
if err != nil {
|
||||||
fmt.Printf("Err: %+v\n", err)
|
if !strings.Contains(err.Error(), "E11000") {
|
||||||
|
fmt.Printf("Err: %+v\n", err)
|
||||||
|
cons.error = true
|
||||||
|
count.AddErr(len(slogin[key]))
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
count.AddDuplicate(strings.Count(err.Error(), "E11000"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cons.logins = append(cons.logins, slogin[key]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
count.AddLog(len(prod.logins))
|
count.AddLog(len(prod.logins))
|
||||||
|
|
||||||
if opts.MaxLogins > -1 && len(prod.logins) < opts.MaxLogins {
|
if opts.MaxLogins > -1 && len(prod.logins) < opts.MaxLogins {
|
||||||
cons.empty = true
|
cons.empty = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Debug {
|
if opts.Debug {
|
||||||
fmt.Printf("CONS: user=%s logins=%d in %v - active=%d\n", prod.user, len(prod.logins), time.Since(start), dbs.rdb.ActiveCount())
|
fmt.Printf("CONS: user=%s logins=%d in %v - active=%d\n", prod.user, len(prod.logins), time.Since(start), dbs.rdb.ActiveCount())
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Done()
|
// wg.Done()
|
||||||
// remove <- cons
|
remove <- cons
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,10 @@ func (c *Counter) AddUser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddDuplicate increment number of duplicates log
|
// AddDuplicate increment number of duplicates log
|
||||||
func (c *Counter) AddDuplicate() {
|
func (c *Counter) AddDuplicate(add int) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.dup++
|
c.dup += add
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLog increment number of log's rows managed
|
// AddLog increment number of log's rows managed
|
||||||
|
@ -67,10 +67,10 @@ func (c *Counter) AddWG() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddErr ...
|
// AddErr ...
|
||||||
func (c *Counter) AddErr() {
|
func (c *Counter) AddErr(add int) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.err++
|
c.err += add
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelWG ...
|
// DelWG ...
|
||||||
|
|
7
main.go
7
main.go
|
@ -11,7 +11,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_Version = "v3.0.0"
|
_Version = "v3.0.0"
|
||||||
|
_Producer = 0
|
||||||
|
_Consumer = 1
|
||||||
|
_Remover = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -23,6 +26,8 @@ var (
|
||||||
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
|
status int
|
||||||
|
|
||||||
count *Counter
|
count *Counter
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,8 @@ type Options struct {
|
||||||
MaxLogins int
|
MaxLogins int
|
||||||
Debug bool
|
Debug bool
|
||||||
Version bool
|
Version bool
|
||||||
|
Bulk bool
|
||||||
MaxError int
|
MaxError int
|
||||||
Xymon string
|
|
||||||
Influxdb string
|
Influxdb string
|
||||||
Hostname string
|
Hostname string
|
||||||
Month string
|
Month string
|
||||||
|
@ -57,7 +57,6 @@ func init() {
|
||||||
opts.Pidfile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
|
opts.Pidfile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
|
||||||
opts.Exe = path.Base(os.Args[0])
|
opts.Exe = path.Base(os.Args[0])
|
||||||
|
|
||||||
flag.StringVar(&opts.Xymon, "x", "", "xymon server")
|
|
||||||
flag.StringVar(&opts.Influxdb, "i", "", "influxdb server")
|
flag.StringVar(&opts.Influxdb, "i", "", "influxdb server")
|
||||||
flag.StringVar(&opts.Hostname, "H", "", "hostname")
|
flag.StringVar(&opts.Hostname, "H", "", "hostname")
|
||||||
flag.StringVar(&dbs.MongoURI, "m", dbs.MongoURI, "Mongodb")
|
flag.StringVar(&dbs.MongoURI, "m", dbs.MongoURI, "Mongodb")
|
||||||
|
@ -69,6 +68,7 @@ func init() {
|
||||||
flag.BoolVar(&opts.Version, "v", false, "Version")
|
flag.BoolVar(&opts.Version, "v", false, "Version")
|
||||||
flag.DurationVar(&opts.Timeout, "T", 0, "Running timeout")
|
flag.DurationVar(&opts.Timeout, "T", 0, "Running timeout")
|
||||||
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
||||||
|
flag.BoolVar(&opts.Bulk, "B", false, "Bulk")
|
||||||
flag.IntVar(&opts.MaxError, "E", 100, "Max Mongodb Error")
|
flag.IntVar(&opts.MaxError, "E", 100, "Max Mongodb Error")
|
||||||
flag.StringVar(&opts.Pidfile, "p", opts.Pidfile, "pid file")
|
flag.StringVar(&opts.Pidfile, "p", opts.Pidfile, "pid file")
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,9 @@ func producer() {
|
||||||
|
|
||||||
for loop {
|
for loop {
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
status = _Producer
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
// estrae un userid dalla lista degli utenti che hanno fatto login
|
// estrae un userid dalla lista degli utenti che hanno fatto login
|
||||||
user, err := redis.String(conn.Do("spop", "llindex"))
|
user, err := redis.String(conn.Do("spop", "llindex"))
|
||||||
|
|
|
@ -15,6 +15,8 @@ func remover() {
|
||||||
for {
|
for {
|
||||||
rem := <-remove
|
rem := <-remove
|
||||||
|
|
||||||
|
status = _Remover
|
||||||
|
|
||||||
// wg.Add(1)
|
// wg.Add(1)
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
14
sigterm.go
14
sigterm.go
|
@ -7,12 +7,22 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func kill() {
|
||||||
|
log.Printf("KILL %d\n", status)
|
||||||
|
fmt.Printf("KILL %d\n", status)
|
||||||
|
wg.Done()
|
||||||
|
count.DelWG()
|
||||||
|
done <- true
|
||||||
|
}
|
||||||
|
|
||||||
func exit() {
|
func exit() {
|
||||||
log.Println("EXIT ")
|
log.Printf("EXIT %d\n", status)
|
||||||
fmt.Println("EXIT ")
|
fmt.Printf("EXIT %d\n", status)
|
||||||
loop = false
|
loop = false
|
||||||
|
time.AfterFunc(time.Second*20, kill)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setTerm() {
|
func setTerm() {
|
||||||
|
|
2
xymon.go
2
xymon.go
|
@ -1,6 +1,7 @@
|
||||||
// xymon.go
|
// xymon.go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
@ -59,3 +60,4 @@ func xymonSend(msg []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue