scouting: Make background_task interval configurable
This patch modifies the API so that the background task gets invoked
at a user-defined interval. There are no functional changes since we
schedule everything at 10 minute intervals. That matches the previous
behaviour.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I512ddcd01408f9d1af706ca390c84368f6816638
diff --git a/scouting/background_task/background_task.go b/scouting/background_task/background_task.go
index 2d0c4a5..4fdb034 100644
--- a/scouting/background_task/background_task.go
+++ b/scouting/background_task/background_task.go
@@ -4,48 +4,54 @@
"time"
)
-// A helper to run a function in the background every ~10 minutes. Technically
-// can be used for a lot of different things, but is primarily geared towards
-// scraping thebluealliance.com.
-type BackgroundTask struct {
+// A helper to run a function in the background at a specified interval.
+// Can be used for a lot of different things.
+type backgroundTask struct {
doneChan chan<- bool
checkStopped chan<- bool
+ interval time.Duration
}
-func (scraper *BackgroundTask) Start(scrape func()) {
- scraper.doneChan = make(chan bool, 1)
- scraper.checkStopped = make(chan bool, 1)
+func New(interval time.Duration) backgroundTask {
+ return backgroundTask{
+ doneChan: make(chan bool, 1),
+ checkStopped: make(chan bool, 1),
+ interval: interval,
+ }
+}
+func (task *backgroundTask) Start(taskFunc func()) {
go func() {
- // Setting start time to 11 minutes prior so getRankings called instantly when Start() called
- startTime := time.Now().Add(-11 * time.Minute)
+ // Setting start time to a time prior so the function gets
+ // called instantly when Start() called
+ startTime := time.Now().Add(-task.interval - time.Minute)
for {
curTime := time.Now()
diff := curTime.Sub(startTime)
- if diff.Minutes() > 10 {
- scrape()
+ if diff > task.interval {
+ taskFunc()
startTime = curTime
}
- if len(scraper.doneChan) != 0 {
+ if len(task.doneChan) != 0 {
break
}
time.Sleep(time.Second)
}
- scraper.checkStopped <- true
+ task.checkStopped <- true
}()
}
-func (scraper *BackgroundTask) Stop() {
- scraper.doneChan <- true
+func (task *backgroundTask) Stop() {
+ task.doneChan <- true
for {
- if len(scraper.checkStopped) != 0 {
- close(scraper.doneChan)
- close(scraper.checkStopped)
+ if len(task.checkStopped) != 0 {
+ close(task.doneChan)
+ close(task.checkStopped)
break
}
}