moth/cmd/mothd/state.go

527 lines
13 KiB
Go

package main
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/dirtbags/moth/pkg/award"
)
// DistinguishableChars are visually unambiguous glyphs.
// People with mediocre handwriting could write these down unambiguously,
// and they can be entered without holding down shift.
const DistinguishableChars = "34678abcdefhikmnpqrtwxy="
// RFC3339Space is a time layout which replaces 'T' with a space.
// This is also a valid RFC3339 format.
const RFC3339Space = "2006-01-02 15:04:05Z07:00"
// ErrAlreadyRegistered means a team cannot be registered because it was registered previously.
var ErrAlreadyRegistered = errors.New("team ID has already been registered")
// State defines the current state of a MOTH instance.
// We use the filesystem for synchronization between threads.
// The only thing State methods need to know is the path to the state directory.
type State struct {
basedir string
// Enabled tracks whether the current State system is processing updates
Enabled bool
refreshNow chan bool
eventStream chan []string
eventWriter *csv.Writer
eventWriterFile *os.File
// Caches, so we're not hammering NFS with metadata operations
teamNames map[string]string
pointsLog award.List
messages string
lock sync.RWMutex
}
// NewState returns a new State struct backed by the given Fs
func NewState(basedir string) *State {
s := &State{
basedir: basedir,
Enabled: true,
refreshNow: make(chan bool, 5),
eventStream: make(chan []string, 80),
teamNames: make(map[string]string),
}
if err := s.reopenEventLog(); err != nil {
log.Fatal(err)
}
return s
}
func (s *State) path(elem ...string) string {
elements := append([]string{s.basedir}, elem...)
return filepath.Join(elements...)
}
// updateEnabled checks a few things to see if this state directory is "enabled".
func (s *State) updateEnabled() {
nextEnabled := true
why := "`state/enabled` present, `state/hours.txt` missing"
if untilFile, err := os.Open(s.path("hours.txt")); err == nil {
defer untilFile.Close()
why = "`state/hours.txt` present"
scanner := bufio.NewScanner(untilFile)
for scanner.Scan() {
line := scanner.Text()
if len(line) < 1 {
continue
}
thisEnabled := true
switch line[0] {
case '+':
thisEnabled = true
line = line[1:]
case '-':
thisEnabled = false
line = line[1:]
case '#':
continue
default:
log.Println("Misformatted line in hours.txt file")
}
line = strings.TrimSpace(line)
until, err := time.Parse(time.RFC3339, line)
if err != nil {
until, err = time.Parse(RFC3339Space, line)
}
if err != nil {
log.Println("Suspended: Unparseable until date:", line)
continue
}
if until.Before(time.Now()) {
nextEnabled = thisEnabled
}
}
}
if _, err := os.Stat(s.path("enabled")); os.IsNotExist(err) {
nextEnabled = false
why = "`state/enabled` missing"
}
if nextEnabled != s.Enabled {
s.Enabled = nextEnabled
log.Printf("Setting enabled=%v: %s", s.Enabled, why)
if s.Enabled {
s.LogEvent("enabled", "", "", "", 0, why)
} else {
s.LogEvent("disabled", "", "", "", 0, why)
}
}
}
// TeamName returns team name given a team ID.
func (s *State) TeamName(teamID string) (string, error) {
s.lock.RLock()
name, ok := s.teamNames[teamID]
s.lock.RUnlock()
if !ok {
return "", fmt.Errorf("unregistered team ID: %s", teamID)
}
return name, nil
}
// SetTeamName writes out team name.
// This can only be done once per team.
func (s *State) SetTeamName(teamID, teamName string) error {
idsFile, err := os.Open(s.path("teamids.txt"))
if err != nil {
return fmt.Errorf("team IDs file does not exist")
}
defer idsFile.Close()
found := false
scanner := bufio.NewScanner(idsFile)
for scanner.Scan() {
if scanner.Text() == teamID {
found = true
break
}
}
if !found {
return fmt.Errorf("team ID not found in list of valid team IDs")
}
teamFilename := filepath.Join("teams", teamID)
teamFile, err := os.OpenFile(s.path(teamFilename), os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
if os.IsExist(err) {
return ErrAlreadyRegistered
} else if err != nil {
return err
}
defer teamFile.Close()
log.Printf("Setting team name [%s] in file %s", teamName, teamFilename)
fmt.Fprintln(teamFile, teamName)
teamFile.Close()
s.refreshNow <- true
return nil
}
// PointsLog retrieves the current points log.
func (s *State) PointsLog() award.List {
s.lock.RLock()
ret := make(award.List, len(s.pointsLog))
copy(ret, s.pointsLog)
s.lock.RUnlock()
return ret
}
// Messages retrieves the current messages.
func (s *State) Messages() string {
s.lock.RLock() // It's not clear to me that this actually needs to happen
defer s.lock.RUnlock()
return s.messages
}
// AwardPoints gives points to teamID in category.
// This doesn't attempt to ensure the teamID has been registered.
// It first checks to make sure these are not duplicate points.
// This is not a perfect check, you can trigger a race condition here.
// It's just a courtesy to the user.
// The update task makes sure we never have duplicate points in the log.
func (s *State) AwardPoints(teamID, category string, points int) error {
return s.awardPointsAtTime(time.Now().Unix(), teamID, category, points)
}
func (s *State) awardPointsAtTime(when int64, teamID string, category string, points int) error {
a := award.T{
When: when,
TeamID: teamID,
Category: category,
Points: points,
}
for _, e := range s.PointsLog() {
if a.Equal(e) {
return fmt.Errorf("points already awarded to this team in this category")
}
}
//fn := fmt.Sprintf("%s-%s-%d", a.TeamID, a.Category, a.Points)
fn := a.Filename()
tmpfn := filepath.Join("points.tmp", fn)
newfn := filepath.Join("points.new", fn)
if err := os.WriteFile(s.path(tmpfn), []byte(a.String()), 0644); err != nil {
return err
}
if err := os.Rename(s.path(tmpfn), newfn); err != nil {
return err
}
// State should be updated immediately
s.refreshNow <- true
return nil
}
// collectPoints gathers up files in points.new/ and appends their contents to points.log,
// removing each points.new/ file as it goes.
func (s *State) collectPoints() {
files, err := os.ReadDir(s.path("points.new"))
if err != nil {
log.Print(err)
return
}
for _, f := range files {
filename := filepath.Join("points.new", f.Name())
awardstr, err := os.ReadFile(s.path(filename))
if err != nil {
log.Print("Opening new points: ", err)
continue
}
awd, err := award.Parse(string(awardstr))
if err != nil {
log.Print("Can't parse award file ", filename, ": ", err)
continue
}
duplicate := false
s.lock.RLock()
for _, e := range s.pointsLog {
if awd.Equal(e) {
duplicate = true
break
}
}
s.lock.RUnlock()
if duplicate {
log.Print("Skipping duplicate points: ", awd.String())
} else {
log.Print("Award: ", awd.String())
logf, err := os.OpenFile(s.path("points.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Print("Can't append to points log: ", err)
return
}
fmt.Fprintln(logf, awd.String())
logf.Close()
// Stick this on the cache too
s.lock.Lock()
s.pointsLog = append(s.pointsLog, awd)
s.lock.Unlock()
}
if err := os.Remove(s.path(filename)); err != nil {
log.Print("Unable to remove new points file: ", err)
}
}
}
func (s *State) maybeInitialize() {
// Are we supposed to re-initialize?
if _, err := os.Stat(s.path("initialized")); !os.IsNotExist(err) {
return
}
now := time.Now().UTC().Format(time.RFC3339)
log.Print("initialized file missing, re-initializing")
// Remove any extant control and state files
os.Remove(s.path("enabled"))
os.Remove(s.path("hours.txt"))
os.Remove(s.path("points.log"))
os.Remove(s.path("messages.html"))
os.Remove(s.path("mothd.log"))
os.RemoveAll(s.path("points.tmp"))
os.RemoveAll(s.path("points.new"))
os.RemoveAll(s.path("teams"))
// Open log file
if err := s.reopenEventLog(); err != nil {
log.Fatal(err)
}
s.LogEvent("init", "", "", "", 0)
// Make sure various subdirectories exist
os.Mkdir(s.path("points.tmp"), 0755)
os.Mkdir(s.path("points.new"), 0755)
os.Mkdir(s.path("teams"), 0755)
// Preseed available team ids if file doesn't exist
if f, err := os.OpenFile(s.path("teamids.txt"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644); err == nil {
id := make([]byte, 8)
for i := 0; i < 100; i++ {
for i := range id {
char := rand.Intn(len(DistinguishableChars))
id[i] = DistinguishableChars[char]
}
fmt.Fprintln(f, string(id))
}
f.Close()
}
// Create some files
if f, err := os.Create(s.path("initialized")); err == nil {
fmt.Fprintln(f, "initialized: remove to re-initialize the contest.")
fmt.Fprintln(f)
fmt.Fprintln(f, "This instance was initialized at", now)
f.Close()
}
if f, err := os.Create(s.path("enabled")); err == nil {
fmt.Fprintln(f, "enabled: remove or rename to suspend the contest.")
f.Close()
}
if f, err := os.Create(s.path("hours.txt")); err == nil {
fmt.Fprintln(f, "# hours.txt: when the contest is enabled")
fmt.Fprintln(f, "#")
fmt.Fprintln(f, "# Enable: + timestamp")
fmt.Fprintln(f, "# Disable: - timestamp")
fmt.Fprintln(f, "#")
fmt.Fprintln(f, "# You can have multiple start/stop times.")
fmt.Fprintln(f, "# Whatever time is the most recent, wins.")
fmt.Fprintln(f, "# Times in the future are ignored.")
fmt.Fprintln(f)
fmt.Fprintln(f, "+", now)
fmt.Fprintln(f, "- 2519-10-31T00:00:00Z")
f.Close()
}
if f, err := os.Create(s.path("messages.html")); err == nil {
fmt.Fprintln(f, "<!-- messages.html: put client broadcast messages here. -->")
f.Close()
}
if f, err := os.Create(s.path("points.log")); err == nil {
f.Close()
}
}
// LogEvent writes to the event log
func (s *State) LogEvent(event, participantID, teamID, cat string, points int, extra ...string) {
s.eventStream <- append(
[]string{
strconv.FormatInt(time.Now().Unix(), 10),
event,
participantID,
teamID,
cat,
strconv.Itoa(points),
},
extra...,
)
}
func (s *State) reopenEventLog() error {
if s.eventWriter != nil {
s.eventWriter.Flush()
}
if s.eventWriterFile != nil {
if err := s.eventWriterFile.Close(); err != nil {
// We're going to soldier on if Close returns error
log.Print(err)
}
}
eventWriterFile, err := os.OpenFile(s.path("events.csv"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
s.eventWriterFile = eventWriterFile
s.eventWriter = csv.NewWriter(s.eventWriterFile)
return nil
}
func (s *State) updateCaches() {
s.lock.Lock()
defer s.lock.Unlock()
if f, err := os.Open(s.path("points.log")); err != nil {
log.Println(err)
} else {
defer f.Close()
pointsLog := make(award.List, 0, 200)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
cur, err := award.Parse(line)
if err != nil {
log.Printf("Skipping malformed award line %s: %s", line, err)
continue
}
pointsLog = append(pointsLog, cur)
}
s.pointsLog = pointsLog
}
{
// The compiler recognizes this as an optimization case
for k := range s.teamNames {
delete(s.teamNames, k)
}
if dirents, err := os.ReadDir(s.path("teams")); err != nil {
log.Printf("Reading team ids: %v", err)
} else {
for _, dirent := range dirents {
teamID := dirent.Name()
if teamNameBytes, err := os.ReadFile(s.path("teams", teamID)); err != nil {
log.Printf("Reading team %s: %v", teamID, err)
} else {
teamName := strings.TrimSpace(string(teamNameBytes))
s.teamNames[teamID] = teamName
}
}
}
}
if bMessages, err := os.ReadFile(s.path("messages.html")); err == nil {
s.messages = string(bMessages)
}
}
func (s *State) refresh() {
s.maybeInitialize()
s.updateEnabled()
if s.Enabled {
s.collectPoints()
}
s.updateCaches()
}
// Maintain performs housekeeping on a State struct.
func (s *State) Maintain(updateInterval time.Duration) {
ticker := time.NewTicker(updateInterval)
s.refresh()
for {
select {
case msg := <-s.eventStream:
s.eventWriter.Write(msg)
s.eventWriter.Flush()
s.eventWriterFile.Sync()
case <-ticker.C:
s.refresh()
case <-s.refreshNow:
s.refresh()
}
}
}
// DevelState is a StateProvider for use by development servers
type DevelState struct {
StateProvider
}
// NewDevelState returns a new state object that can be used by the development server.
//
// The main thing this provides is the ability to register a team with any team ID.
// If a team ID is provided that wasn't recognized by the underlying StateProvider,
// it is associated with a team named "<devel:$ID>".
//
// This makes it possible to use the server without having to register a team.
func NewDevelState(sp StateProvider) *DevelState {
return &DevelState{sp}
}
// TeamName returns a valid team name for any teamID
//
// If one's registered, it will use it.
// Otherwise, it returns "<devel:$ID>"
func (ds *DevelState) TeamName(teamID string) (string, error) {
if name, err := ds.StateProvider.TeamName(teamID); err == nil {
return name, nil
}
return fmt.Sprintf("«devel:%s»", teamID), nil
}
// SetTeamName associates a team name with any teamID
//
// If the underlying StateProvider returns any sort of error,
// this returns ErrAlreadyRegistered,
// so the user can join a pre-existing team for whatever ID the provide.
func (ds *DevelState) SetTeamName(teamID, teamName string) error {
if err := ds.StateProvider.SetTeamName(teamID, teamName); err != nil {
return ErrAlreadyRegistered
}
return nil
}