keep state after reload

This commit is contained in:
Max Erenberg 2023-04-04 00:58:25 -04:00 committed by Mirror
parent 35af367d62
commit f889c897b0
3 changed files with 21 additions and 17 deletions

View File

@ -154,11 +154,6 @@ type Repo struct {
ZfssyncLogFile string `ini:"-"`
// add the "-vv" flag to rsync commands and enable the Debug log (default: false)
Verbose bool `ini:"verbose"`
// the repo will write its name and status in a Result struct to DoneChan
// when it has finished a job (shared by all repos)
DoneChan chan<- SyncResult `ini:"-"`
// repos should stop syncing if StopChan is closed (shared by all repos)
StopChan chan struct{} `ini:"-"`
// a struct that stores the repo's status
State *RepoState `ini:"-"`
}
@ -172,6 +167,11 @@ type RepoState struct {
LastAttemptStartTime int64 `ini:"last_attempt_time"`
// whether the last attempt was successful or not
LastAttemptExit int `ini:"last_attempt_exit"`
// the repo will write its name and status in a Result struct to DoneChan
// when it has finished a job (shared by all repos)
DoneChan chan<- SyncResult `ini:"-"`
// repos should stop syncing if StopChan is closed (shared by all repos)
StopChan chan struct{} `ini:"-"`
}
var (
@ -229,12 +229,12 @@ func LoadConfig(configPath string, doneChan chan SyncResult, stopChan chan struc
}
var newRepos []*Repo
var check bool
for _, section := range iniInfo.Sections() {
repoName := section.Name()
if repoName == "DEFAULT" {
continue
}
oldRepo, _ := RepoMap[repoName]
// set the default values for the repo then load from file
repo := Repo{
@ -250,14 +250,13 @@ func LoadConfig(configPath string, doneChan chan SyncResult, stopChan chan struc
RsyncLogFile: filepath.Join(newConf.RsyncLogDir, repoName) + "-rsync.log",
ZfssyncLogFile: filepath.Join(newConf.ZfssyncLogDir, repoName) + "-zfssync.log",
Verbose: false,
DoneChan: doneChan,
StopChan: stopChan,
}
err := section.MapTo(&repo)
panicIfErr(err)
// checks for validity of repo configuration
if repo.Frequency, check = frequencies[repo.FrequencyStr]; !check {
var ok bool
if repo.Frequency, ok = frequencies[repo.FrequencyStr]; !ok {
panic("Missing or invalid frequency for " + repo.Name)
} else if repo.SyncType == "" {
panic("Missing sync type from " + repo.Name)
@ -299,16 +298,21 @@ func LoadConfig(configPath string, doneChan chan SyncResult, stopChan chan struc
repo.ZfssyncLogFile,
)
// re-use the old state if present, otherwise create a new state
if oldRepo != nil {
repo.State = oldRepo.State
} else {
repo.State = &RepoState{
DoneChan: doneChan,
StopChan: stopChan,
LastAttemptExit: NOT_RUN_YET,
}
}
// create the logger and load the state
repo.Logger = logger.NewLogger(repo.Name, repo.RepoLogFile, repo.Verbose)
repo.State = &RepoState{
IsRunning: false,
LastAttemptStartTime: 0,
LastAttemptExit: NOT_RUN_YET,
}
err = ini.MapTo(&repo.State, repo.StateFile)
panicIfErr(err)
repo.SaveState()
newRepos = append(newRepos, &repo)
}

View File

@ -52,7 +52,7 @@ func startRepoSync(repo *config.Repo, force bool) {
done := false
defer func() {
repo.DoneChan <- config.SyncResult{
repo.State.DoneChan <- config.SyncResult{
Name: repo.Name,
Exit: status,
}

View File

@ -83,7 +83,7 @@ func spawnProcess(repo *config.Repo, args []string) (ch <-chan *exec.Cmd) {
repo.Logger.Debug(string(out))
}
case <-repo.StopChan:
case <-repo.State.StopChan:
repo.Logger.Debug("Received signal to stop, killing process...")
killProcess()