moth/maintenance.go

69 lines
1.5 KiB
Go
Raw Normal View History

2018-05-02 15:45:45 -06:00
package main
import (
"log"
"io/ioutil"
"time"
2018-05-04 17:20:51 -06:00
"os"
2018-05-02 15:45:45 -06:00
"strings"
)
2018-05-04 17:20:51 -06:00
func cacheMothball(filepath string, categoryName string) {
log.Printf("I'm exploding a mothball %s %s", filepath, categoryName)
2018-05-02 15:45:45 -06:00
}
// maintenance runs
func tidy() {
// Skip if we've been disabled
2018-05-06 21:37:52 -06:00
if _, err := os.Stat(statePath("disabled")); err == nil {
2018-05-02 15:45:45 -06:00
log.Print("disabled file found, suspending maintenance")
return
}
// Skip if we've expired
2018-05-06 21:37:52 -06:00
untilspec, err := ioutil.ReadFile(statePath("until"))
2018-05-02 15:45:45 -06:00
if err == nil {
2018-05-06 21:37:52 -06:00
until, err := time.Parse(time.RFC3339, string(untilspec))
if err != nil {
log.Print("Unparseable date in until file: %s", until)
} else {
if until.Before(time.Now()) {
log.Print("until file time reached, suspending maintenance")
return
}
2018-05-02 15:45:45 -06:00
}
}
log.Print("Hello, I'm maintaining!")
// Get current list of categories
newCategories := []string{}
2018-05-06 21:37:52 -06:00
files, err := ioutil.ReadDir(modulesPath())
2018-05-04 17:20:51 -06:00
if err != nil {
log.Printf("Error reading packages: %s", err)
}
for _, f := range files {
2018-05-02 15:45:45 -06:00
filename := f.Name()
2018-05-06 21:37:52 -06:00
filepath := modulesPath(filename)
2018-05-02 15:45:45 -06:00
if ! strings.HasSuffix(filename, ".mb") {
continue
}
categoryName := strings.TrimSuffix(filename, ".mb")
newCategories = append(newCategories, categoryName)
// Uncompress into cache directory
cacheMothball(filepath, categoryName)
}
categories = newCategories
2018-05-03 09:58:03 -06:00
collectPoints()
2018-05-02 15:45:45 -06:00
}
// maintenance is the goroutine that runs a periodic maintenance task
2018-05-06 21:37:52 -06:00
func maintenance(maintenanceInterval time.Duration) {
2018-05-02 15:45:45 -06:00
for ;; time.Sleep(maintenanceInterval) {
tidy()
}
}