backend: handle SIGUSR1 to retreat all files left in submissions directory and SIGUSR2 to dump statistics

This commit is contained in:
nemunaire 2020-01-28 19:49:46 +01:00
parent 6f17fc0760
commit a0b19f6184

View File

@ -6,9 +6,11 @@ import (
"log" "log"
"math/rand" "math/rand"
"os" "os"
"os/signal"
"path" "path"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"
"srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/libfic"
@ -43,6 +45,24 @@ func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
} }
} }
func walkAndTreat(pathname string) error {
if ds, err := ioutil.ReadDir(pathname); err != nil {
return err
} else {
for _, d := range ds {
p := path.Join(pathname, d.Name())
if d.IsDir() && d.Name() != ".tmp" && d.Mode()&os.ModeSymlink == 0 {
if err := walkAndTreat(p); err != nil {
return err
}
} else if d.Mode().IsRegular() {
treat(p)
}
}
return nil
}
}
var ChStarted = false var ChStarted = false
var lastRegeneration time.Time var lastRegeneration time.Time
var skipInitialGeneration = false var skipInitialGeneration = false
@ -127,10 +147,24 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
// Register SIGUSR1, SIGUSR2
interrupt1 := make(chan os.Signal, 1)
signal.Notify(interrupt1, syscall.SIGUSR1)
interrupt2 := make(chan os.Signal, 1)
signal.Notify(interrupt2, syscall.SIGUSR2)
watchedNotify := fsnotify.Create watchedNotify := fsnotify.Create
for { for {
select { select {
case <-interrupt1:
log.Println("SIGUSR1 received, retreating all files in queue...")
walkAndTreat(SubmissionDir)
log.Println("SIGUSR1 treated.")
case <-interrupt2:
inQueueMutex.Lock()
log.Printf("SIGUSR2 received, dumping statistics:\n parallelJobs: %d\n genTeamQueue size: %d\n genQueue: %d\n Teams in queue: %v\n Challenge started: %v\n Last regeneration: %v\n", parallelJobs, len(genTeamQueue), len(genQueue), inGenQueue, ChStarted, lastRegeneration)
inQueueMutex.Unlock()
case ev := <-watcher.Events: case ev := <-watcher.Events:
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" { if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
// Register new subdirectory // Register new subdirectory