mirror/merlin/merlin.go

145 lines
3.5 KiB
Go

package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"golang.org/x/sys/unix"
"git.csclub.uwaterloo.ca/public/merlin/arthur"
"git.csclub.uwaterloo.ca/public/merlin/config"
"git.csclub.uwaterloo.ca/public/merlin/logger"
"git.csclub.uwaterloo.ca/public/merlin/sync"
)
var DEFAULT_CONFIG_PATH = "merlin-config.ini"
func main() {
// custom help message
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "USAGE: merlin [-h | --help] [--config=<config-path>]\n")
flag.PrintDefaults()
}
// parse command args
// if more are added can prob use a library
configPath := flag.String("config", DEFAULT_CONFIG_PATH, "alternate config file")
flag.Parse()
// check that merlin is run as mirror user
// check that mirror user has pid of 1001
// receives a Result struct when a repo stops syncing
doneChan := make(chan config.SyncResult)
// closed when merlin is told to stop running
stopChan := make(chan struct{})
// receives a Conn when a client makes a connection to unix socket
connChan := make(chan net.Conn)
// signal channel to stop listening to unix socket
stopLisChan := make(chan struct{})
stopSig := make(chan os.Signal, 1)
reloadSig := make(chan os.Signal, 1)
signal.Notify(stopSig, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(reloadSig, syscall.SIGHUP)
unix.Umask(002)
numJobsRunning := 0
repoIdx := 0
loadConfig := func() {
config.LoadConfig(*configPath, doneChan, stopChan)
logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf))
repoIdx = 0
go arthur.StartListener(connChan, stopLisChan)
}
// We use a round-robin strategy. It's not the most efficient, but it's simple
// (read: easy to understand) and guarantees each repo will eventually get a chance to run.
runAsManyAsPossible := func() {
startIdx := repoIdx
for numJobsRunning < config.Conf.MaxJobs {
repo := config.Repos[repoIdx]
if sync.SyncIfPossible(repo) {
numJobsRunning++
}
repoIdx = (repoIdx + 1) % len(config.Repos)
if repoIdx == startIdx {
// we've come full circle
return
}
}
}
loadConfig()
// ensure that IsRunning is false otherwise repo will never sync
// (only on startup can we assume that repos were not previously syncing)
for _, repo := range config.Repos {
repo.State.IsRunning = false
}
runAsManyAsPossible()
runLoop:
for {
select {
case <-stopSig:
close(stopChan)
close(stopLisChan)
break runLoop
case <-reloadSig:
stopLisChan <- struct{}{}
loadConfig()
// ensure that SyncCompleted can handle it if reloading config
// removes a repo that was already syncing
case done := <-doneChan:
sync.SyncCompleted(config.RepoMap[done.Name], done.Exit)
numJobsRunning--
case conn := <-connChan:
command, repoName := arthur.GetCommand(conn)
switch command {
case "status":
arthur.SendStatus(conn)
case "sync":
if arthur.ForceSync(conn, repoName) {
numJobsRunning++
}
default:
arthur.SendAndLog(conn, "Received unrecognized command: "+command)
}
// None of the arthur functions close the connection so you will need to
// close it manually for the message to be sent
conn.Close()
case <-time.After(1 * time.Minute):
}
runAsManyAsPossible()
}
// give time for all jobs to terminate before exiting program
for {
select {
case done := <-doneChan:
sync.SyncCompleted(config.RepoMap[done.Name], done.Exit)
numJobsRunning--
case <-time.After(1 * time.Second):
}
if numJobsRunning <= 0 {
return
}
}
}