Esempio n. 1
0
func settingsWatcher() error {

	var err error

	/*
		// Watching settings file for changes.
		// Was not properly returning events on OSX.
		// https://github.com/howeyc/fsnotify/issues/34

		watcher, err := fsnotify.NewWatcher()

		if err == nil {
			defer watcher.Close()

			go func() {
				for {
					select {
					case ev := <-watcher.Event:
						if ev == nil {
							return
						}
						if ev.IsModify() {
							log.Printf("Trying to reload settings file %s...\n", ev.Name)
							y, err := loadSettings(ev.Name)
							if err != nil {
								log.Printf("Error loading settings file %s: %q\n", ev.Name, err)
							} else {
								settings = y
							}
						} else if ev.IsDelete() {
							watcher.RemoveWatch(ev.Name)
							watcher.Watch(ev.Name)
						}
					case err := <-watcher.Error:
						log.Printf("Watcher error: %q\n", err)
					}
				}
			}()

			watcher.Watch(settingsFile)
		}
	*/

	// (Stupid) time based file modification watcher.
	watch, err = watcher.New()

	if err == nil {
		go func() {
			defer watch.Close()
			for {
				select {
				case ev := <-watch.Event:
					if ev.IsModify() {
						y, err := loadSettings(ev.Name)
						if err != nil {
							log.Printf("Error loading settings file %s: %q\n", ev.Name, err)
						} else {
							log.Printf("Reloading settings file %s.\n", ev.Name)
							settings = y
						}
					}
				}
			}
		}()

	}

	return err
}
Esempio n. 2
0
func (host *Host) fileWatcher() error {

	var err error

	/*
		// File watcher.
		host.Watcher, err = fsnotify.NewWatcher()

		if err == nil {

			go func() {

				for {

					select {

					case ev := <-host.Watcher.Event:

						fmt.Printf("%s: got ev: %v\n", host.Name, ev)

						if ev == nil {
							return
						}

						if ev.IsModify() {
							// Is settings file?
							if ev.Name == host.DocumentRoot+PS+settingsFile {
								log.Printf("%s: Reloading host settings %s...\n", host.Name, ev.Name)
								err := host.loadSettings()

								if err != nil {
									log.Printf("%s: Could not reload host settings: %s\n", host.Name, host.DocumentRoot+PS+settingsFile)
								}
							}

							// Is a template?
							if strings.HasPrefix(ev.Name, host.TemplateRoot) == true {

								if strings.HasSuffix(ev.Name, ".tpl") == true {
									log.Printf("%s: Reloading template %s", host.Name, ev.Name)
									host.loadTemplate(ev.Name)

									if err != nil {
										log.Printf("%s: Could not reload template %s: %s", host.Name, ev.Name, err.Error())
									}

								}
							}

						} else if ev.IsDelete() {
							// Attemping to re-add watcher.
							host.Watcher.RemoveWatch(ev.Name)
							host.Watcher.Watch(ev.Name)
						}

					}
				}

			}()

		}
	*/

	// (Stupid) file modification watcher.
	host.Watcher, err = watcher.New()

	if err == nil {

		go func() {

			for {
				select {
				case ev := <-host.Watcher.Event:

					if ev.IsModify() {
						// Is settings file?
						if ev.Name == host.DocumentRoot+PS+settingsFile {
							log.Printf("%s: Reloading host settings %s...\n", host.Name, ev.Name)
							err := host.loadSettings()

							if err != nil {
								log.Printf("%s: Could not reload host settings: %s\n", host.Name, host.DocumentRoot+PS+settingsFile)
							}
						}

						// Is a template?
						if strings.HasPrefix(ev.Name, host.TemplateRoot) == true {
							if strings.HasSuffix(ev.Name, ".tpl") == true {
								log.Printf("%s: Reloading template %s", host.Name, ev.Name)
								host.loadTemplate(ev.Name)
								if err != nil {
									log.Printf("%s: Could not reload template %s: %s", host.Name, ev.Name, err.Error())
								}
							}
						}
					}
				}
			}
		}()
	}

	return err

}