genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/lib/worker/worker.go

42 lines
700 B
Go

// A little lib for cronjobs to run it in background
package worker
import "time"
// a struct which handle the job
type Worker struct {
every time.Duration
run func()
quit chan struct{}
}
// create a new Worker with timestamp run every and his function
func NewWorker(every time.Duration, f func()) (w *Worker) {
w = &Worker{
every: every,
run: f,
quit: make(chan struct{}),
}
return
}
// start the worker
// please us it as a goroutine: go w.Start()
func (w *Worker) Start() {
ticker := time.NewTicker(w.every)
for {
select {
case <-ticker.C:
w.run()
case <-w.quit:
ticker.Stop()
return
}
}
}
// stop the worker
func (w *Worker) Close() {
close(w.quit)
}