Skip to content

Commit

Permalink
log connection and final stats
Browse files Browse the repository at this point in the history
  • Loading branch information
robbles committed Sep 5, 2015
1 parent e5cc718 commit 871a271
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ func main() {
}
defer c.Close()

logger.debug.Println("Connected to redis server at", url)
logger.info.Println("Connected to redis server at", url)
if dryRun {
logger.info.Println("Dry-run mode: destructive commands skipped")
}

done := make(chan func())
scan_stats := make(chan int, 0)
keys_stats := make(chan int, 0)
expired_stats := make(chan int, 0)
go stats(scan_stats, keys_stats, expired_stats)
go stats(done, scan_stats, keys_stats, expired_stats)

var scan struct {
cursor int
batch []string
total int
cursor int
batch []string
total int
complete bool
}
scan.cursor = 0
scan.total = 0
Expand All @@ -92,26 +94,30 @@ func main() {

if limit >= 0 && scan.total >= limit {
logger.info.Println("Reached limit of", limit, "keys")
return
scan.complete = true
break
}

if processKey(c, key) {
expired_stats <- 1
}
}

if scan.cursor == 0 {
return
}

logger.debug.Println("Next cursor is", scan.cursor)
scan_stats <- 1
keys_stats <- len(scan.batch)

if scan.cursor == 0 || scan.complete {
break
}
logger.debug.Println("Next cursor is", scan.cursor)

if delay > 0 {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}

// Read a callback from stats and call it to print final results
(<-done)()
}

func processKey(c redis.Conn, key string) (expired bool) {
Expand Down Expand Up @@ -195,12 +201,17 @@ func matchTTL(ttl int, ttlMin int) bool {
return false
}

func stats(scans chan int, keys chan int, expired chan int) {
func stats(done chan func(), scans chan int, keys chan int, expired chan int) {
timer := time.Tick(1 * time.Second)
scan_count := 0
keys_count := 0
expired_count := 0

printStats := func() {
logger.info.Printf("Stats: scans=%d keys=%d expires=%d\n",
scan_count, keys_count, expired_count)
}

for {
select {
case n := <-scans:
Expand All @@ -213,8 +224,10 @@ func stats(scans chan int, keys chan int, expired chan int) {
expired_count += n

case <-timer:
logger.info.Printf("Stats: scans=%d keys=%d expires=%d\n",
scan_count, keys_count, expired_count)
printStats()

case done <- printStats:
break
}
}
}

0 comments on commit 871a271

Please sign in to comment.