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
754 B
Go

// Package with a lib for cronjobs to run in the background
package worker
import "time"
// Struct which handles the job
type Worker struct {
every time.Duration
run func()
quit chan struct{}
}
// Function to create a new Worker with a timestamp, run, every and it's function
func NewWorker(every time.Duration, f func()) (w *Worker) {
w = &Worker{
every: every,
run: f,
quit: make(chan struct{}),
}
return
}
// Function to start the Worker
// (please us it as a go routine with 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
}
}
}
// Function to stop the Worker
func (w *Worker) Close() {
close(w.quit)
}