mirror/merlin/merlin.go

164 lines
4.3 KiB
Go
Raw Normal View History

2021-09-17 00:31:49 -04:00
package main
import (
2022-01-06 18:41:55 -05:00
"flag"
2021-09-17 00:31:49 -04:00
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
2021-09-17 00:31:49 -04:00
"golang.org/x/sys/unix"
2021-12-11 18:28:09 -05:00
"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"
)
2021-09-17 00:31:49 -04:00
2022-01-06 18:41:55 -05:00
var DEFAULT_CONFIG_PATH = "merlin-config.ini"
2021-12-15 01:18:52 -05:00
func main() {
2022-01-06 18:41:55 -05:00
// 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 the user that program is running under (should be mirror)
if os.Getuid() == 0 {
panic("Merlin should not be run as root")
}
2021-11-20 01:04:45 -05:00
2022-06-18 02:13:23 -04:00
// receives a Result struct when a repo is done/fails syncing
2021-12-11 18:28:09 -05:00
doneChan := make(chan config.SyncResult)
// closed when merlin is told to stop running
2021-11-14 17:22:32 -05:00
stopChan := make(chan struct{})
2022-06-18 02:13:23 -04:00
2021-12-11 18:28:09 -05:00
// receives a Conn when a client makes a connection to unix socket
2021-11-14 17:22:32 -05:00
connChan := make(chan net.Conn)
2022-06-18 02:13:23 -04:00
// closed or receives a signal to stop listening to the unix socket
2021-11-14 17:22:32 -05:00
stopLisChan := make(chan struct{})
2022-06-18 02:13:23 -04:00
// gets unblocked when SIGINT or SIGTERM is sent, will begin process of stopping the program
2021-11-14 17:22:32 -05:00
stopSig := make(chan os.Signal, 1)
signal.Notify(stopSig, syscall.SIGINT, syscall.SIGTERM)
2022-06-18 02:13:23 -04:00
// gets unblocked with SIGHUP is sent, will attempt to reload the config
reloadSig := make(chan os.Signal, 1)
signal.Notify(reloadSig, syscall.SIGHUP)
unix.Umask(002)
2021-12-11 18:28:09 -05:00
repoIdx := 0
2022-06-18 02:13:23 -04:00
numJobsRunning := 0
2021-11-14 17:22:32 -05:00
loadConfig := func() {
2022-01-06 18:41:55 -05:00
config.LoadConfig(*configPath, doneChan, stopChan)
2021-12-11 18:28:09 -05:00
logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf))
2021-11-14 17:22:32 -05:00
2022-06-18 02:13:23 -04:00
// reset the round-robin index and recreate the socket listener
repoIdx = 0
2021-12-15 01:18:52 -05:00
go arthur.StartListener(connChan, stopLisChan)
2021-12-11 18:28:09 -05:00
}
2021-12-11 18:28:09 -05:00
// 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 {
2022-06-27 23:36:51 -04:00
if sync.SyncIfPossible(config.Repos[repoIdx], false) {
2021-12-11 18:28:09 -05:00
numJobsRunning++
}
repoIdx = (repoIdx + 1) % len(config.Repos)
if repoIdx == startIdx {
2022-06-18 02:13:23 -04:00
// we've tried to run every repo and have come full circle
2021-12-11 18:28:09 -05:00
return
}
}
2021-11-14 17:22:32 -05:00
}
loadConfig()
2022-06-18 02:13:23 -04:00
// ensure that IsRunning is false otherwise repo will never sync.
// only on startup can we assume that repos were not previously syncing
// since reloading the config does not stop repos with a sync in progress
2021-12-15 01:18:52 -05:00
for _, repo := range config.Repos {
2021-11-14 17:22:32 -05:00
repo.State.IsRunning = false
}
2021-11-14 17:22:32 -05:00
runAsManyAsPossible()
runLoop:
2021-09-17 00:31:49 -04:00
for {
select {
2022-06-18 02:13:23 -04:00
case <-stopSig: // caught a SIGINT or SIGTERM
// kill all syncing repos and the socket listener
2021-11-14 17:22:32 -05:00
close(stopChan)
close(stopLisChan)
break runLoop
2021-11-14 17:22:32 -05:00
2022-06-18 02:13:23 -04:00
case <-reloadSig: // caught a SIGHUP
// temporary stop the socket listener and load the config again
2021-11-14 17:22:32 -05:00
stopLisChan <- struct{}{}
loadConfig()
2022-06-18 02:13:23 -04:00
case done := <-doneChan: // a sync is done and sends its exit status
// repo could be removed from config while sync in progress
if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit)
}
2021-09-17 00:31:49 -04:00
numJobsRunning--
2021-11-14 17:22:32 -05:00
2022-06-18 02:13:23 -04:00
case conn := <-connChan: // a connection was accepted by the socket listener
2021-12-15 01:18:52 -05:00
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)
2021-12-11 18:28:09 -05:00
}
// close the received connection so that the message is sent
2021-12-15 01:18:52 -05:00
conn.Close()
2021-11-14 17:22:32 -05:00
2021-09-17 00:31:49 -04:00
case <-time.After(1 * time.Minute):
}
2021-11-14 17:22:32 -05:00
runAsManyAsPossible()
2021-09-17 00:31:49 -04:00
}
2022-06-18 02:13:23 -04:00
// allow some time for jobs to terminate before force exiting the program
go func() {
<-time.After(time.Minute)
logger.ErrLog("One minute has passed, forcefully exiting the program")
os.Exit(1)
}()
// wait on repos to get terminated
for {
select {
case done := <-doneChan:
2022-06-18 02:13:23 -04:00
if repo, check := config.RepoMap[done.Name]; check {
sync.SyncCompleted(repo, done.Exit)
}
numJobsRunning--
2021-11-14 17:22:32 -05:00
case <-time.After(1 * time.Second):
2021-11-14 17:22:32 -05:00
}
2022-06-18 02:13:23 -04:00
2021-11-14 17:22:32 -05:00
if numJobsRunning <= 0 {
2022-06-18 02:13:23 -04:00
os.Exit(0)
}
}
2021-09-17 00:31:49 -04:00
}