feat(backend/storage): added counter and last session timestamp for storage service
This commit is contained in:
parent
f330d5031f
commit
2cadf12f88
2 changed files with 52 additions and 0 deletions
|
|
@ -28,6 +28,8 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
counter := storage.NewLogCounter()
|
||||
|
||||
consumer := queue.NewMessageConsumer(
|
||||
cfg.GroupStorage,
|
||||
[]string{
|
||||
|
|
@ -37,6 +39,8 @@ func main() {
|
|||
switch msg.(type) {
|
||||
case *messages.SessionEnd:
|
||||
srv.UploadKey(strconv.FormatUint(sessionID, 10), 5)
|
||||
// Log timestamp of last processed session
|
||||
counter.Update(time.UnixMilli(msg.Meta().Timestamp))
|
||||
}
|
||||
},
|
||||
true,
|
||||
|
|
@ -48,6 +52,7 @@ func main() {
|
|||
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
cleanTick := time.Tick(time.Duration(cfg.FSCleanHRS) * time.Hour)
|
||||
counterTick := time.Tick(time.Second * 30)
|
||||
for {
|
||||
select {
|
||||
case sig := <-sigchan:
|
||||
|
|
@ -56,6 +61,8 @@ func main() {
|
|||
os.Exit(0)
|
||||
case <-cleanTick:
|
||||
go srv.CleanDir(cfg.FSDir)
|
||||
case <-counterTick:
|
||||
counter.Print()
|
||||
default:
|
||||
err := consumer.ConsumeNext()
|
||||
if err != nil {
|
||||
|
|
|
|||
45
backend/internal/storage/counter.go
Normal file
45
backend/internal/storage/counter.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type logCounter struct {
|
||||
mu sync.Mutex
|
||||
counter int
|
||||
timestamp time.Time
|
||||
lastTS time.Time
|
||||
}
|
||||
|
||||
func NewLogCounter() *logCounter {
|
||||
nlc := &logCounter{}
|
||||
nlc.init()
|
||||
return nlc
|
||||
}
|
||||
|
||||
func (c *logCounter) init() {
|
||||
c.mu.Lock()
|
||||
c.counter = 0
|
||||
c.timestamp = time.Now()
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *logCounter) Update(ts time.Time) {
|
||||
c.mu.Lock()
|
||||
c.counter++
|
||||
c.lastTS = ts
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *logCounter) Print() {
|
||||
c.mu.Lock()
|
||||
log.Printf("counter: %d, duration: %s, lastSessionTS: %s",
|
||||
c.counter,
|
||||
time.Now().Sub(c.timestamp).String(),
|
||||
c.lastTS.String(),
|
||||
)
|
||||
c.mu.Unlock()
|
||||
c.init()
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue