Skip to content

Instantly share code, notes, and snippets.

@wkharold
Created March 30, 2014 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkharold/9880465 to your computer and use it in GitHub Desktop.
Save wkharold/9880465 to your computer and use it in GitHub Desktop.
/*** 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