From f950dacf5ef454ed0e19905fdb3023c16680e978 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 3 May 2018 15:58:03 +0000 Subject: [PATCH] more work --- maintenance.go | 20 ++------------ mothd.go | 40 ---------------------------- points.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 58 deletions(-) create mode 100644 points.go diff --git a/maintenance.go b/maintenance.go index 8afe8af..827d49e 100644 --- a/maintenance.go +++ b/maintenance.go @@ -16,6 +16,7 @@ func allfiles(dirpath) []string { return files } + // maintenance runs func tidy() { // Skip if we've been disabled @@ -55,24 +56,7 @@ func tidy() { } categories = newCategories - // - // 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")) { - 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() + collectPoints() } // maintenance is the goroutine that runs a periodic maintenance task diff --git a/mothd.go b/mothd.go index 437214d..3ae0c26 100644 --- a/mothd.go +++ b/mothd.go @@ -17,46 +17,6 @@ var basePath = "." var maintenanceInterval = 20 * time.Second 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) { moo := req.FormValue("moo") diff --git a/points.go b/points.go new file mode 100644 index 0000000..4314d36 --- /dev/null +++ b/points.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "log" + "os" +) + +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) +} + +// collectPoints gathers up files in points.new/ and appends their contents to points.log, +// removing each points.new/ file as it goes. +func collectPoints() { + pointsLog = os.OpenFile(statePath("points.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + defer pointsLog.Close() + + 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() + err := os.Remove(filename) + if (err != nil) { + log.Printf("Unable to remove %s: %s", filename, err) + } + } +} \ No newline at end of file