add LastSuccessfulAttemptStartTime to config

This commit is contained in:
Max Erenberg 2023-04-04 01:51:52 -04:00 committed by Mirror
parent f889c897b0
commit 65e152ad2f
5 changed files with 42 additions and 22 deletions

View File

@ -44,14 +44,27 @@ func SendAndLog(conn net.Conn, msg string) {
conn.Write([]byte(msg))
}
var location *time.Location
func unixTimeToStr(unixTime int64) string {
if unixTime == 0 {
return "unknown"
}
if location == nil {
var err error
// Force arthur to send back time information in America/Toronto time
location, err = time.LoadLocation("America/Toronto")
if err != nil {
logger.ErrLog(err)
location = time.UTC
}
}
// for other ways to format the time see: https://pkg.go.dev/time#pkg-constants
return time.Unix(unixTime, 0).In(location).Format(time.RFC1123)
}
// Writes the status of the repos to the accepted connection
func SendStatus(conn net.Conn) {
// Force arthur to send back time information in America/Toronto time
location, err := time.LoadLocation("America/Toronto")
if err != nil {
logger.ErrLog(err)
}
status := tabwriter.NewWriter(conn, 5, 5, 5, ' ', 0)
fmt.Fprintf(status, "Repository\tLast Synced\tNext Expected Sync\tLast Exit\tRunning\n")
@ -63,18 +76,17 @@ func SendStatus(conn net.Conn) {
sort.Strings(keys)
// print out the state of each repo in the config (last and next sync time + if it is currently running)
// for other ways to format the time see: https://pkg.go.dev/time#pkg-constants
for _, name := range keys {
repo := config.RepoMap[name]
lastSync := repo.State.LastAttemptStartTime
nextSync := lastSync + int64(repo.Frequency)
lastExit := config.StatusToString(repo.State.LastAttemptExit)
lastExit := repo.State.LastAttemptExit
fmt.Fprintf(status, "%s\t%s\t%s\t%s\t%t\n",
name,
time.Unix(lastSync, 0).In(location).Format(time.RFC1123),
time.Unix(nextSync, 0).In(location).Format(time.RFC1123),
lastExit,
unixTimeToStr(lastSync),
unixTimeToStr(nextSync),
config.StatusToString(lastExit),
repo.State.IsRunning,
)
}

View File

@ -126,11 +126,11 @@ func TestForceSync(t *testing.T) {
MaxTime: 30,
Logger: logger.NewLogger("nux", "/tmp/merlin_force_sync_test_log", false),
StateFile: "/tmp/merlin_force_sync_test_state",
DoneChan: doneChan,
State: &config.RepoState{
IsRunning: false,
LastAttemptStartTime: 0,
LastAttemptExit: config.NOT_RUN_YET,
DoneChan: doneChan,
},
}
config.Repos = nil

View File

@ -165,8 +165,10 @@ type RepoState struct {
IsRunning bool `ini:"is_running"`
// the Unix epoch timestamp at which this repo last attempted a job
LastAttemptStartTime int64 `ini:"last_attempt_time"`
// whether the last attempt was successful or not
// the status of the last attempt
LastAttemptExit int `ini:"last_attempt_exit"`
// the Unix epoch timestamp at which this repo last successfully completed a job
LastSuccessfulAttemptStartTime int64 `ini:"last_successful_attempt_time"`
// 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:"-"`

View File

@ -67,12 +67,12 @@ func TestLoadConfig(t *testing.T) {
Logger: Repos[0].Logger,
RsyncLogFile: "test_files/rsync/eelinux-rsync.log",
ZfssyncLogFile: "test_files/zfssync/eelinux-zfssync.log",
DoneChan: doneChan,
StopChan: stopChan,
State: &RepoState{
IsRunning: false,
LastAttemptStartTime: 1600000000,
LastAttemptExit: 1,
DoneChan: doneChan,
StopChan: stopChan,
},
}
expectedRepo2 := Repo{
@ -90,12 +90,12 @@ func TestLoadConfig(t *testing.T) {
Logger: Repos[1].Logger,
RsyncLogFile: "test_files/rsync/yoland-rsync.log",
ZfssyncLogFile: "test_files/zfssync/yoland-zfssync.log",
DoneChan: doneChan,
StopChan: stopChan,
State: &RepoState{
IsRunning: false,
LastAttemptStartTime: 0,
LastAttemptExit: 2,
DoneChan: doneChan,
StopChan: stopChan,
},
}

View File

@ -31,17 +31,23 @@ func SyncIfPossible(repo *config.Repo, force bool) bool {
func SyncCompleted(repo *config.Repo, exit int) {
repoState := repo.State
syncTook := time.Now().Unix() - repoState.LastAttemptStartTime
nextSync := repo.Frequency - int(syncTook)
if nextSync < 0 {
nextSync = 0
syncTook := int(time.Now().Unix() - repoState.LastAttemptStartTime)
timeUntilNextSync := repo.Frequency - syncTook
if timeUntilNextSync < 0 {
timeUntilNextSync = 0
}
repoState.IsRunning = false
repoState.LastAttemptExit = exit
if exit == config.SUCCESS {
repoState.LastSuccessfulAttemptStartTime = repoState.LastAttemptStartTime
}
repo.SaveState()
exitStr := config.StatusToString(exit)
repo.Logger.Info(fmt.Sprintf("Sync %s after running for %d seconds, will run again in %d seconds", exitStr, syncTook, nextSync))
repo.Logger.Info(fmt.Sprintf(
"Sync %s after running for %d seconds, will run again in %d seconds",
exitStr, syncTook, timeUntilNextSync,
))
go postRepoSync(repo, exit)
}