mirror/merlin/merlin.go

131 lines
3.2 KiB
Go
Raw Normal View History

2021-09-17 00:31:49 -04:00
package main
import (
"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
2021-12-15 01:18:52 -05:00
// get config path from command args
var CONFIG_PATH = "merlin-config.ini"
func main() {
2021-11-20 01:04:45 -05:00
// check that merlin is run as mirror user
// check that mirror user has pid of 1001
2021-12-11 18:28:09 -05:00
// receives a Result struct when a repo stops syncing
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{})
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)
2021-12-11 18:28:09 -05:00
// signal channel to stop listening to unix socket
2021-11-14 17:22:32 -05:00
stopLisChan := make(chan struct{})
2021-11-14 17:22:32 -05:00
stopSig := make(chan os.Signal, 1)
reloadSig := make(chan os.Signal, 1)
2021-11-14 17:22:32 -05:00
signal.Notify(stopSig, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(reloadSig, syscall.SIGHUP)
unix.Umask(002)
2021-12-11 18:28:09 -05:00
numJobsRunning := 0
repoIdx := 0
2021-11-14 17:22:32 -05:00
loadConfig := func() {
2021-12-15 01:18:52 -05:00
config.LoadConfig(CONFIG_PATH, 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
repoIdx = 0
2021-12-15 01:18:52 -05:00
go arthur.StartListener(connChan, stopLisChan)
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 {
repo := config.Repos[repoIdx]
if sync.SyncIfPossible(repo) {
numJobsRunning++
}
repoIdx = (repoIdx + 1) % len(config.Repos)
if repoIdx == startIdx {
// we've come full circle
return
}
}
2021-11-14 17:22:32 -05:00
}
loadConfig()
2021-12-11 18:28:09 -05:00
// ensure that IsRunning is false otherwise repo will never sync
2021-12-15 01:18:52 -05:00
// (only on startup can we assume that repos were not previously syncing)
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 {
case <-stopSig:
2021-11-14 17:22:32 -05:00
close(stopChan)
close(stopLisChan)
break runLoop
2021-11-14 17:22:32 -05:00
case <-reloadSig:
2021-11-14 17:22:32 -05:00
stopLisChan <- struct{}{}
loadConfig()
2021-12-15 01:18:52 -05:00
// ensure that SyncCompleted can handle it if reloading config
// removes a repo that was already syncing
2021-11-14 17:22:32 -05:00
case done := <-doneChan:
2021-12-11 18:28:09 -05:00
sync.SyncCompleted(config.RepoMap[done.Name], done.Exit)
2021-09-17 00:31:49 -04:00
numJobsRunning--
2021-11-14 17:22:32 -05:00
case conn := <-connChan:
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
}
2021-12-15 01:18:52 -05:00
// None of the arthur functions close the connection so you will need to
// close it manually for the message to be sent
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
}
2021-12-11 18:28:09 -05:00
// give time for all jobs to terminate before exiting program
for {
select {
case done := <-doneChan:
2021-12-11 18:28:09 -05:00
sync.SyncCompleted(config.RepoMap[done.Name], 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
}
if numJobsRunning <= 0 {
return
}
}
2021-09-17 00:31:49 -04:00
}