117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"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"
|
|
)
|
|
|
|
func main() {
|
|
// 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(doneChan, stopChan)
|
|
logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf))
|
|
|
|
repoIdx = 0
|
|
go arthur.UnixSockListener(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
|
|
for _, repo := range config.RepoMap {
|
|
repo.State.IsRunning = false
|
|
}
|
|
runAsManyAsPossible()
|
|
|
|
runLoop:
|
|
for {
|
|
select {
|
|
case <-stopSig:
|
|
close(stopChan)
|
|
close(stopLisChan)
|
|
break runLoop
|
|
|
|
case <-reloadSig:
|
|
stopLisChan <- struct{}{}
|
|
loadConfig()
|
|
|
|
case done := <-doneChan:
|
|
sync.SyncCompleted(config.RepoMap[done.Name], done.Exit)
|
|
numJobsRunning--
|
|
|
|
case conn := <-connChan:
|
|
// TODO: may want to split this into GetCommand and something else
|
|
// to make it more clear tha GetAndRunCommand returns true if
|
|
// it starts a sync
|
|
if arthur.GetAndRunCommand(conn) {
|
|
numJobsRunning--
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|