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
 		}
 	}
diff --git a/scouting/webserver/main.go b/scouting/webserver/main.go
index ba9d120..c5bed2c 100644
--- a/scouting/webserver/main.go
+++ b/scouting/webserver/main.go
@@ -141,17 +141,17 @@
 	// Since Go doesn't support default arguments, we use 0 and "" to
 	// indicate that we want to source the values from the config.
 
-	matchListScraper := background_task.BackgroundTask{}
+	matchListScraper := background_task.New(10 * time.Minute)
 	matchListScraper.Start(func() {
 		match_list.GetMatchList(database, 0, "", *blueAllianceConfigPtr)
 	})
 
-	rankingsScraper := background_task.BackgroundTask{}
+	rankingsScraper := background_task.New(10 * time.Minute)
 	rankingsScraper.Start(func() {
 		rankings.GetRankings(database, 0, "", *blueAllianceConfigPtr)
 	})
 
-	driverRankingParser := background_task.BackgroundTask{}
+	driverRankingParser := background_task.New(10 * time.Minute)
 	driverRankingParser.Start(func() {
 		// Specify "" as the script path here so that the default is
 		// used.
diff --git a/scouting/webserver/rankings/rankings_test.go b/scouting/webserver/rankings/rankings_test.go
index 03a4ac6..4ab3c34 100644
--- a/scouting/webserver/rankings/rankings_test.go
+++ b/scouting/webserver/rankings/rankings_test.go
@@ -38,7 +38,7 @@
 
 func TestGetRankings(t *testing.T) {
 	database := MockDatabase{}
-	scraper := background_task.BackgroundTask{}
+	scraper := background_task.New(time.Minute)
 	tbaServer := server.NewScoutingServer()
 	tbaServer.Handle("/", ServeRankings(t, http.FileServer(http.Dir("../../"))))
 	tbaServer.Start(8000)