Permalink
Browse files

stop tasks and remove from list when config file deleted

  • Loading branch information...
1 parent 47a5489 commit ae70dd4aba5c79b02be003e069d55e3e79257244 @bradfitz committed Dec 27, 2011
Showing with 32 additions and 5 deletions.
  1. +18 −2 runsit.go
  2. +14 −3 watchdir.go
View
@@ -94,7 +94,9 @@ func (b *logBuffer) String() string {
// A Task is a named daemon. A single instance of Task exists for the
// life of the runsit daemon, despite how many times the task has
-// failed and restarted.
+// failed and restarted. (the exception is if the config file for the
+// task is deleted, and then the *Task is removed from the global tasks
+// map and a new one could appear later with the same name)
type Task struct {
// Immutable:
Name string
@@ -281,8 +283,16 @@ func (t *Task) restartIfStopped() {
// run in Task.loop
func (t *Task) update(tf TaskFile) {
t.config = nil
- jc, err := jsonconfig.ReadFile(tf.ConfigFileName())
t.stop()
+
+ fileName := tf.ConfigFileName()
+ if fileName == "" {
+ t.Printf("config file deleted; stopping")
+ DeleteTask(t.Name)
+ return
+ }
+
+ jc, err := jsonconfig.ReadFile(fileName)
if err != nil {
t.configError("Bad config file: %v", err)
return
@@ -537,6 +547,12 @@ func GetTask(name string) (t *Task, ok bool) {
return
}
+func DeleteTask(name string) {
+ tasksMu.Lock()
+ defer tasksMu.Unlock()
+ delete(tasks, name)
+}
+
// GetOrMakeTask returns or create the named task.
func GetOrMakeTask(name string) *Task {
tasksMu.Lock()
View
@@ -35,6 +35,7 @@ type TaskFile interface {
Name() string
// ConfigFileName returns the filename of the JSON file to read.
+ // This returns the empty string when a file has been deleted.
// TODO: make this more abstract, a ReadSeekCloser instead?
ConfigFileName() string
}
@@ -79,6 +80,10 @@ func (w *pollingDirWatcher) poll() {
time.Sleep(15 * time.Second)
continue
}
+ deleted := map[string]bool{}
+ for n, _ := range last {
+ deleted[n] = true // assume for now
+ }
for _, fi := range fis {
name := fi.Name()
if !strings.HasSuffix(name, ".json") {
@@ -87,18 +92,24 @@ func (w *pollingDirWatcher) poll() {
if strings.HasPrefix(name, ".#") {
continue
}
+ baseName := name[:len(name)-len(".json")]
+
m := fi.ModTime()
- if em, ok := last[name]; ok && em.Equal(m) {
+ delete(deleted, baseName)
+ if em, ok := last[baseName]; ok && em.Equal(m) {
continue
}
logger.Printf("Updated config file: name = %q, modtime = %v", name, m)
- last[name] = m
+ last[baseName] = m
w.c <- diskFile{
- baseName: name[:len(name)-len(".json")],
+ baseName: baseName,
fileName: filepath.Join(w.dir, name),
fi: fi,
}
}
+ for bn, _ := range deleted {
+ w.c <- diskFile{baseName: bn}
+ }
time.Sleep(5 * time.Second)
}
}

0 comments on commit ae70dd4

Please sign in to comment.