2018-05-02 15:45:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func allfiles(dirpath) []string {
|
|
|
|
files, err := ioutil.ReadDir(dirpath)
|
|
|
|
if (err != nil) {
|
|
|
|
log.Printf("Error reading directory %s: %s", dirpath, err)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2018-05-03 09:58:03 -06:00
|
|
|
|
2018-05-02 15:45:45 -06:00
|
|
|
// maintenance runs
|
|
|
|
func tidy() {
|
|
|
|
// Skip if we've been disabled
|
|
|
|
if exists(statePath("disabled")) {
|
|
|
|
log.Print("disabled file found, suspending maintenance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if we've expired
|
|
|
|
untilspec, _ := ioutil.ReadFile(statePath("until"))
|
|
|
|
until, err := time.Parse(time.RFC3339, string(untilspec))
|
|
|
|
if err == nil {
|
|
|
|
if until.Before(time.Now()) {
|
|
|
|
log.Print("until file time reached, suspending maintenance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Hello, I'm maintaining!")
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get current list of categories
|
|
|
|
//
|
|
|
|
newCategories := []string{}
|
|
|
|
for f := range(allfiles(mothPath("packages"))) {
|
|
|
|
filename := f.Name()
|
2018-05-02 17:00:53 -06:00
|
|
|
filepath := mothPath("packages", 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
|
|
|
|
func maintenance() {
|
|
|
|
for ;; time.Sleep(maintenanceInterval) {
|
|
|
|
tidy()
|
|
|
|
}
|
|
|
|
}
|