60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package sync
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.csclub.uwaterloo.ca/public/merlin/config"
|
|
)
|
|
|
|
// start sync job for this repo if more than repo.Frequency seconds have elapsed since its last job
|
|
// and is not currently running.
|
|
// returns true iff a job is started.
|
|
func SyncIfPossible(repo *config.Repo) bool {
|
|
// Change to SyncIfPossible
|
|
if repo.State.IsRunning {
|
|
return false
|
|
}
|
|
|
|
curTime := time.Now().Unix()
|
|
if curTime-repo.State.LastAttemptStartTime > int64(repo.Frequency) {
|
|
repo.State.IsRunning = true
|
|
repo.State.LastAttemptStartTime = curTime
|
|
repo.SaveState()
|
|
repo.Logger.Info(fmt.Sprintf("Repo %s has started syncing", repo.Name))
|
|
go startSync(repo)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// update the repo state with the last attempt time and exit now that the job is done
|
|
func SyncCompleted(repo *config.Repo, exit int) {
|
|
repoState := repo.State
|
|
syncTook := time.Now().Unix() - repoState.LastAttemptStartTime
|
|
nextSync := repo.MaxTime - int(syncTook)
|
|
if nextSync < 0 {
|
|
nextSync = 0
|
|
}
|
|
|
|
repoState.IsRunning = false
|
|
repoState.LastAttemptExit = exit
|
|
repoState.LastAttemptRunTime = syncTook
|
|
|
|
var exitStr string
|
|
switch exit {
|
|
case config.SUCCESS:
|
|
exitStr = "completed"
|
|
case config.TERMINATED:
|
|
exitStr = "terminated"
|
|
default:
|
|
exitStr = "failed"
|
|
}
|
|
repo.SaveState()
|
|
repo.Logger.Info(fmt.Sprintf("Sync "+exitStr+" after running for %d seconds, will run again in %d seconds", syncTook, nextSync))
|
|
|
|
if exit == config.SUCCESS {
|
|
go zfsSync(repo)
|
|
}
|
|
}
|