mirror of https://github.com/dirtbags/moth.git
more work
This commit is contained in:
parent
75eef3d19f
commit
f0497f35d9
|
@ -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
|
||||
|
|
40
mothd.go
40
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")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue