even better one time run

This commit is contained in:
Radu Ioan Fericean
2012-08-10 18:26:47 +03:00
parent 6de835e895
commit a7033f88c8
3 changed files with 40 additions and 8 deletions

View File

@@ -73,9 +73,15 @@ func loadActionTimings(storage timespans.StorageGetter) {
sched.queue = timespans.ActionTimingPriotityList{}
for key, ats := range actionTimings {
toBeSaved := false
for _, at := range ats {
for i, at := range ats {
toBeSaved = toBeSaved || at.CheckForASAP()
sched.queue = append(sched.queue, at)
if at.IsOneTimeRun() {
go at.Execute()
// remove it from list
ats = append(ats[:i], ats[i+1:]...)
} else {
sched.queue = append(sched.queue, at)
}
}
if toBeSaved {
storage.SetActionTimings(key, ats)

View File

@@ -228,20 +228,27 @@ func (at *ActionTiming) Execute() (err error) {
return
}
// checks for *asap string as start time and replaces it wit an actual time in the newar future
// returns true if the *asap string was found
func (at *ActionTiming) CheckForASAP() bool {
if at.Timing.StartTime == ASAP {
oneMinute, _ := time.ParseDuration(ASAP_DELAY)
timeToRun := time.Now().Add(oneMinute)
timeTokens := strings.Split(timeToRun.Format(time.Stamp), " ")
at.Timing.Years = Years{timeToRun.Year()}
at.Timing.Months = Months{timeToRun.Month()}
at.Timing.MonthDays = MonthDays{timeToRun.Day()}
timeTokens := strings.Split(time.Now().Add(oneMinute).Format(time.Stamp), " ")
at.Timing.StartTime = timeTokens[len(timeTokens)-1]
return true
}
return false
}
// returns true if only the starting time was is filled in the Timing field
func (at *ActionTiming) IsOneTimeRun() bool {
return len(at.Timing.Years) == 0 &&
len(at.Timing.Months) == 0 &&
len(at.Timing.MonthDays) == 0 &&
len(at.Timing.WeekDays) == 0 &&
len(at.Timing.StartTime) != 0
}
// Structure to store actions according to weight
type ActionTimingPriotityList []*ActionTiming

View File

@@ -348,13 +348,32 @@ func TestActionTimingFirstOfTheYear(t *testing.T) {
}
}
func TestActionTimingIsOneTimeRunNoInterval(t *testing.T) {
func TestActionTimingCheckForASAP(t *testing.T) {
at := &ActionTiming{Timing: &Interval{StartTime: ASAP}}
if !at.CheckForASAP() {
t.Errorf("%v should be asap!", at)
}
}
func TestActionTimingIsOneTimeRun(t *testing.T) {
at := &ActionTiming{Timing: &Interval{StartTime: ASAP}}
if !at.CheckForASAP() {
t.Errorf("%v should be asap!", at)
}
if !at.IsOneTimeRun() {
t.Errorf("%v should be one time run!", at)
}
}
func TestActionTimingOneTimeRun(t *testing.T) {
at := &ActionTiming{Timing: &Interval{StartTime: ASAP}}
at.CheckForASAP()
nextRun := at.GetNextStartTime()
if nextRun.IsZero() {
t.Error("next time failed for asap")
}
}
func TestActionTimingLogFunction(t *testing.T) {
a := &Action{
ActionType: "LOG",