fixes to repo state tracking
This commit is contained in:
parent
2833a62378
commit
2e4ecb7fd2
|
@ -117,12 +117,12 @@ type State struct {
|
|||
// whether the last attempt was successful or not
|
||||
LastAttemptStatus int `ini:"last_attempt_status"`
|
||||
// whether this repo is running a job or not
|
||||
isRunning bool `ini:"is_running"`
|
||||
IsRunning bool `ini:"is_running"`
|
||||
}
|
||||
|
||||
// IsRunning returns true if the repo is currently running a sync job.
|
||||
func (repo *Repo) IsRunning() bool {
|
||||
return repo.state.isRunning
|
||||
return repo.state.IsRunning
|
||||
}
|
||||
|
||||
// RunIfScheduled starts a sync job for this repo if more than repo.Frequency
|
||||
|
@ -130,13 +130,13 @@ func (repo *Repo) IsRunning() bool {
|
|||
// It returns true iff a job is started.
|
||||
func (repo *Repo) RunIfScheduled() bool {
|
||||
// sanity check; don't run if a job is already running
|
||||
if repo.state.isRunning {
|
||||
if repo.state.IsRunning {
|
||||
return false
|
||||
}
|
||||
if time.Now().Unix()-repo.state.LastAttemptTime > int64(repo.Frequency) {
|
||||
// this should be set in the caller's thread so that the check
|
||||
// above will always work
|
||||
repo.state.isRunning = true
|
||||
repo.state.IsRunning = true
|
||||
go repo.StartSyncJob()
|
||||
return true
|
||||
}
|
||||
|
@ -197,9 +197,24 @@ func GetConfig() Config {
|
|||
LastAttemptTime: 0,
|
||||
LastAttemptRunTime: 0,
|
||||
LastAttemptStatus: NOT_RUN_YET,
|
||||
isRunning: false,
|
||||
IsRunning: false,
|
||||
}
|
||||
if err := ini.MapTo(&repo.state, cfg.StatePath+"/"+repo.Name); err != nil {
|
||||
|
||||
repo_state_file := cfg.StatePath + "/" + repo.Name
|
||||
|
||||
if _, err := os.Stat(repo_state_file); err != nil {
|
||||
// when repo_status_path does not exist then save the default config
|
||||
repo_state := ini.Empty()
|
||||
ini.ReflectFrom(repo_state, &repo.state)
|
||||
file, err := os.OpenFile(repo_state_file, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if _, err := repo_state.WriteTo(file); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if err := ini.MapTo(&repo.state, repo_state_file); err != nil {
|
||||
// when it does exist attempt to the grab its configs
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -210,3 +225,17 @@ func GetConfig() Config {
|
|||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (repo *Repo) SaveState() {
|
||||
cfg := ini.Empty()
|
||||
if err := ini.ReflectFrom(cfg, &repo.state); err != nil {
|
||||
repo.Logger.Error(err)
|
||||
}
|
||||
file, err := os.OpenFile(repo.cfg.StatePath+"/"+repo.Name, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
repo.Logger.Error(err)
|
||||
}
|
||||
if _, err := cfg.WriteTo(file); err != nil {
|
||||
repo.Logger.Error(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,30 +4,20 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
ini "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// CSCSyncStandard performs a standard rsync job.
|
||||
func (repo *Repo) CSCSyncStandard() {
|
||||
startTime := time.Now().Unix()
|
||||
status := FAILURE
|
||||
// https://medium.com/@manandharsabbir/go-lang-defer-statement-arguments-evaluated-at-defer-execution-b2c4a1687c6c
|
||||
// will defer actaully wait till end to function to set the vars?
|
||||
defer func() {
|
||||
repo.state.LastAttemptTime = startTime
|
||||
repo.state.LastAttemptRunTime = time.Now().Unix() - startTime
|
||||
repo.state.LastAttemptStatus = status
|
||||
repo.state.isRunning = false
|
||||
cfg := ini.Empty()
|
||||
if err := ini.ReflectFrom(cfg, &repo.state); err != nil {
|
||||
// log error
|
||||
}
|
||||
file, err := os.OpenFile(repo.cfg.StatePath+"/"+repo.Name, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
// log error
|
||||
}
|
||||
if _, err := cfg.WriteTo(file); err != nil {
|
||||
// log error
|
||||
}
|
||||
repo.state.IsRunning = false
|
||||
repo.SaveState()
|
||||
repo.DoneChan <- repo.Name
|
||||
}()
|
||||
|
||||
|
|
Loading…
Reference in New Issue