still working on maintenance functions

This commit is contained in:
Neale Pickett 2018-05-02 23:00:53 +00:00
parent e9295d67bf
commit 75eef3d19f
2 changed files with 60 additions and 6 deletions

View File

@ -42,7 +42,7 @@ func tidy() {
newCategories := []string{} newCategories := []string{}
for f := range(allfiles(mothPath("packages"))) { for f := range(allfiles(mothPath("packages"))) {
filename := f.Name() filename := f.Name()
filepath := mothPath(path.Join("packages", filename)) filepath := mothPath("packages", filename)
if ! strings.HasSuffix(filename, ".mb") { if ! strings.HasSuffix(filename, ".mb") {
continue continue
} }
@ -58,9 +58,21 @@ func tidy() {
// //
// Collect new points // Collect new points
// //
pointsLog = os.OpenFile(statePath("points.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
for f := range allfiles(statePath("points.new")) { for f := range allfiles(statePath("points.new")) {
filename := statePath("points.new", f.Name())
s := ioutil.ReadFile(filename)
award, err := ParseAward(s)
if (err != nil) {
log.Printf("Can't parse award file %s: %s", filename, err)
continue
}
fmt.Fprintf(pointsLog, "%s\n", award.String())
log.Print(award.String())
pointsLog.Sync()
os.Remove(filename)
} }
pointsLog.Close()
} }
// maintenance is the goroutine that runs a periodic maintenance task // maintenance is the goroutine that runs a periodic maintenance task

View File

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -16,6 +17,47 @@ var basePath = "."
var maintenanceInterval = 20 * time.Second var maintenanceInterval = 20 * time.Second
var categories = []string{} var categories = []string{}
type Award struct {
when time.Time,
team string,
category string,
points int,
comment string
}
func ParseAward(s string) (*Award, error) {
ret := Award{}
parts := strings.SplitN(s, " ", 5)
if len(parts) < 4 {
return nil, Error("Malformed award string")
}
whenEpoch, err = strconv.Atoi(parts[0])
if (err != nil) {
return nil, Errorf("Malformed timestamp: %s", parts[0])
}
ret.when = time.Unix(whenEpoch, 0)
ret.team = parts[1]
ret.category = parts[2]
points, err = strconv.Atoi(parts[3])
if (err != nil) {
return nil, Errorf("Malformed points: %s", parts[3])
}
if len(parts) == 5 {
ret.comment = parts[4]
}
return &ret
}
func (a *Award) String() string {
return fmt.Sprintf("%d %s %s %d %s", a.when.Unix(), a.team, a.category, a.points, a.comment)
}
func mooHandler(w http.ResponseWriter, req *http.Request) { func mooHandler(w http.ResponseWriter, req *http.Request) {
moo := req.FormValue("moo") moo := req.FormValue("moo")
fmt.Fprintf(w, "Hello, %q. %s", html.EscapeString(req.URL.Path), html.EscapeString(moo)) fmt.Fprintf(w, "Hello, %q. %s", html.EscapeString(req.URL.Path), html.EscapeString(moo))
@ -28,12 +70,12 @@ func rootHandler(w http.ResponseWriter, req *http.Request) {
} }
} }
func mothPath(filename string) string { func mothPath(parts ...string) string {
return path.Join(basePath, filename) return path.Join(basePath, parts...)
} }
func statePath(filename string) string { func statePath(parts ...string) string {
return path.Join(basePath, "state", filename) return path.Join(basePath, "state", parts...)
} }
func exists(filename string) bool { func exists(filename string) bool {