Do not allow validator to be restarted
This commit is contained in:
parent
e33bd3d689
commit
6c3af1fcd8
@ -19,23 +19,30 @@ type Validator struct {
|
||||
Logger tacitus.Logger
|
||||
Products []string
|
||||
|
||||
ticker *time.Ticker
|
||||
done chan bool
|
||||
done chan struct{}
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (v *Validator) Stop() {
|
||||
if v.ticker == nil || v.done == nil {
|
||||
v.ticker.Stop()
|
||||
v.done <- true
|
||||
if v.done != nil {
|
||||
select {
|
||||
case v.done <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) Start(frequency time.Duration) {
|
||||
v.ticker = time.NewTicker(frequency)
|
||||
v.done = make(chan bool)
|
||||
if v.dirty == true {
|
||||
return
|
||||
}
|
||||
|
||||
v.dirty = true
|
||||
v.done = make(chan struct{})
|
||||
|
||||
go func() {
|
||||
v.emitProducts()
|
||||
ticker := time.NewTicker(frequency)
|
||||
|
||||
var done bool
|
||||
|
||||
@ -43,12 +50,12 @@ func (v *Validator) Start(frequency time.Duration) {
|
||||
select {
|
||||
case <-v.done:
|
||||
done = true
|
||||
case <-v.ticker.C:
|
||||
ticker.Stop()
|
||||
case <-ticker.C:
|
||||
v.emitProducts()
|
||||
}
|
||||
}
|
||||
|
||||
v.ticker = nil
|
||||
v.done = nil
|
||||
}()
|
||||
}
|
||||
|
Reference in New Issue
Block a user