2018-09-14 18:24:48 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-17 17:00:08 -06:00
|
|
|
"fmt"
|
2018-09-14 18:24:48 -06:00
|
|
|
"io/ioutil"
|
2018-09-17 17:00:08 -06:00
|
|
|
"log"
|
2018-09-14 18:24:48 -06:00
|
|
|
"os"
|
|
|
|
"strings"
|
2018-09-17 17:00:08 -06:00
|
|
|
"time"
|
2018-09-14 18:24:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// maintenance runs
|
|
|
|
func (ctx *Instance) Tidy() {
|
2018-09-17 17:00:08 -06:00
|
|
|
// Do they want to reset everything?
|
|
|
|
ctx.MaybeInitialize()
|
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
// Skip if we've been disabled
|
|
|
|
if _, err := os.Stat(ctx.StatePath("disabled")); err == nil {
|
|
|
|
log.Print("disabled file found, suspending maintenance")
|
|
|
|
return
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
// Skip if we've expired
|
|
|
|
untilspec, err := ioutil.ReadFile(ctx.StatePath("until"))
|
|
|
|
if err == nil {
|
|
|
|
until, err := time.Parse(time.RFC3339, string(untilspec))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unparseable date in until file: %v", until)
|
|
|
|
} else {
|
|
|
|
if until.Before(time.Now()) {
|
|
|
|
log.Print("until file time reached, suspending maintenance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-18 21:29:05 -06:00
|
|
|
// Refresh all current categories
|
|
|
|
for categoryName, mb := range ctx.Categories {
|
|
|
|
if err := mb.Refresh(); err != nil {
|
|
|
|
// Backing file vanished: remove this category
|
|
|
|
log.Printf("Removing category: %s: %s", categoryName, err)
|
|
|
|
mb.Close()
|
|
|
|
delete(ctx.Categories, categoryName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
// Any new categories?
|
|
|
|
files, err := ioutil.ReadDir(ctx.MothballPath())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error listing mothballs: %s", err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
filename := f.Name()
|
|
|
|
filepath := ctx.MothballPath(filename)
|
2018-09-17 17:00:08 -06:00
|
|
|
if !strings.HasSuffix(filename, ".mb") {
|
2018-09-14 18:24:48 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
categoryName := strings.TrimSuffix(filename, ".mb")
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
if _, ok := ctx.Categories[categoryName]; !ok {
|
|
|
|
mb, err := OpenMothball(filepath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error opening %s: %s", filepath, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
log.Printf("New category: %s", filename)
|
2018-09-14 18:24:48 -06:00
|
|
|
ctx.Categories[categoryName] = mb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.CollectPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectPoints gathers up files in points.new/ and appends their contents to points.log,
|
|
|
|
// removing each points.new/ file as it goes.
|
|
|
|
func (ctx *Instance) CollectPoints() {
|
|
|
|
logf, err := os.OpenFile(ctx.StatePath("points.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can't append to points log: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer logf.Close()
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
files, err := ioutil.ReadDir(ctx.StatePath("points.new"))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading packages: %s", err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
filename := ctx.StatePath("points.new", f.Name())
|
|
|
|
s, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can't read points file %s: %s", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
award, err := ParseAward(string(s))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Can't parse award file %s: %s", filename, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-17 21:32:24 -06:00
|
|
|
|
2018-09-17 18:02:44 -06:00
|
|
|
duplicate := false
|
|
|
|
for _, e := range ctx.PointsLog() {
|
|
|
|
if award.Same(e) {
|
|
|
|
duplicate = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 21:32:24 -06:00
|
|
|
|
2018-09-17 18:02:44 -06:00
|
|
|
if duplicate {
|
|
|
|
log.Printf("Skipping duplicate points: %s", award.String())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(logf, "%s\n", award.String())
|
|
|
|
}
|
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
logf.Sync()
|
|
|
|
if err := os.Remove(filename); err != nil {
|
|
|
|
log.Printf("Unable to remove %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// maintenance is the goroutine that runs a periodic maintenance task
|
|
|
|
func (ctx *Instance) Maintenance(maintenanceInterval time.Duration) {
|
2018-09-19 17:56:26 -06:00
|
|
|
for {
|
2018-09-14 18:24:48 -06:00
|
|
|
ctx.Tidy()
|
2018-09-19 17:56:26 -06:00
|
|
|
select {
|
|
|
|
case <-ctx.update:
|
|
|
|
log.Print("Forced update")
|
|
|
|
case <-time.After(maintenanceInterval):
|
|
|
|
log.Print("Housekeeping...")
|
|
|
|
}
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
|
|
|
}
|