Files
isp-gate-service/accounting/snapshot.go
T
2019-11-21 11:39:20 +03:00

58 lines
971 B
Go

package accounting
import (
log "github.com/integration-system/isp-log"
"isp-gate-service/log_code"
"isp-gate-service/model"
"sync"
"time"
)
var snapshot = &snapshotTask{
process: false,
timeout: make(chan time.Time),
close: make(chan bool),
}
type snapshotTask struct {
mx sync.Mutex
process bool
timeout <-chan time.Time
close chan bool
}
func (s *snapshotTask) Start(timeout time.Duration) {
s.Stop()
s.mx.Lock()
s.process = true
s.timeout = time.After(timeout)
s.mx.Unlock()
for {
select {
case <-s.close:
return
case <-s.timeout:
if err := s.saveSnapshot(); err != nil {
log.Error(log_code.ErrorSnapshotAccounting, err)
}
s.timeout = time.After(timeout)
}
}
}
func (s *snapshotTask) Stop() {
s.mx.Lock()
if s.process {
s.process = false
s.close <- true
}
s.mx.Unlock()
}
func (s *snapshotTask) saveSnapshot() error {
snapshotList := takeSnapshot()
return model.SnapshotRep.Update(snapshotList)
}