Created
March 30, 2014 21:51
-
-
Save wkharold/9880465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** job.go ***/ | |
// run executes the command associated with a job according to its schedule and | |
// records the results until it is told to stop. | |
func (j *job) run() { | |
j.history.Value = fmt.Sprintf("%s:started\n", time.Now().String()) | |
j.history = j.history.Next() | |
for { | |
now := time.Now() | |
e, err := cronexpr.Parse(j.defn.schedule) | |
if err != nil { | |
glog.Errorf("Can't parse %s [%s]", j.defn.schedule, err) | |
return | |
} | |
select { | |
case <-time.After(e.Next(now).Sub(now)): | |
glog.V(3).Infof("running `%s`", j.defn.cmd) | |
var out bytes.Buffer | |
k := exec.Command("/bin/bash", "-c", j.defn.cmd) | |
k.Stdout = &out | |
if err := k.Run(); err != nil { | |
glog.Errorf("%s failed: %v", j.defn.cmd, err) | |
continue | |
} | |
glog.V(3).Infof("%s returned: %s", j.defn.name, out.String()) | |
j.history.Value = fmt.Sprintf("%s:%s", time.Now().String(), out.String()) | |
j.history = j.history.Next() | |
case <-j.done: | |
glog.V(3).Infof("completed") | |
j.history.Value = fmt.Sprintf("%s:completed\n", time.Now().String()) | |
j.history = j.history.Next() | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment