diff --git a/config/setup/markdown.go b/config/setup/markdown.go index 043ead3ad..e18c736ef 100644 --- a/config/setup/markdown.go +++ b/config/setup/markdown.go @@ -36,10 +36,8 @@ func Markdown(c *Controller) (middleware.Middleware, error) { if err := markdown.GenerateLinks(md, cfg); err != nil { return err } - // Watch file changes for links generation. - if cfg.Development { - markdown.Watch(md, cfg, 0) - } else { + // Watch file changes for links generation if not in development mode. + if !cfg.Development { markdown.Watch(md, cfg, markdown.DefaultInterval) } diff --git a/middleware/markdown/markdown.go b/middleware/markdown/markdown.go index 5855413fa..eba56001c 100644 --- a/middleware/markdown/markdown.go +++ b/middleware/markdown/markdown.go @@ -4,6 +4,7 @@ package markdown import ( "io/ioutil" + "log" "net/http" "os" "strings" @@ -119,6 +120,13 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return http.StatusNotFound, nil } + // if development is set, scan directory for file changes for links. + if m.Development { + if err := GenerateLinks(md, m); err != nil { + log.Println(err) + } + } + // if static site is generated, attempt to use it if filepath, ok := m.StaticFiles[fpath]; ok { if fs1, err := os.Stat(filepath); err == nil { diff --git a/middleware/markdown/markdown_test.go b/middleware/markdown/markdown_test.go index 69af55b79..ac91f677e 100644 --- a/middleware/markdown/markdown_test.go +++ b/middleware/markdown/markdown_test.go @@ -199,7 +199,7 @@ func getTrue() bool { } } - // attempt to trigger race condition + // attempt to trigger race conditions var w sync.WaitGroup f := func() { req, err := http.NewRequest("GET", "/log/test.md", nil) @@ -217,6 +217,16 @@ func getTrue() bool { } w.Wait() + f = func() { + GenerateLinks(md, &md.Configs[0]) + w.Done() + } + for i := 0; i < 5; i++ { + w.Add(1) + go f() + } + w.Wait() + if err = os.RemoveAll(DefaultStaticDir); err != nil { t.Errorf("Error while removing the generated static files: %v", err) } diff --git a/middleware/markdown/page.go b/middleware/markdown/page.go index 97f1c5a9a..444b39ad3 100644 --- a/middleware/markdown/page.go +++ b/middleware/markdown/page.go @@ -79,6 +79,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) { if _, err := os.Stat(fp); os.IsNotExist(err) { l.Lock() l.lastErr = err + l.generating = false l.Unlock() return } @@ -87,6 +88,9 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) { // same hash, return. if err == nil && hash == cfg.linksHash { + l.Lock() + l.generating = false + l.Unlock() return } else if err != nil { log.Println("Error:", err) diff --git a/middleware/markdown/watcher.go b/middleware/markdown/watcher.go index 5f311fe1b..b0b4d1aa1 100644 --- a/middleware/markdown/watcher.go +++ b/middleware/markdown/watcher.go @@ -20,39 +20,19 @@ func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct func TickerFunc(interval time.Duration, f func()) chan struct{} { stopChan := make(chan struct{}) - if interval > 0 { - ticker := time.NewTicker(interval) - go func() { - loop: - for { - select { - case <-ticker.C: - f() - case <-stopChan: - ticker.Stop() - break loop - } + ticker := time.NewTicker(interval) + go func() { + loop: + for { + select { + case <-ticker.C: + f() + case <-stopChan: + ticker.Stop() + break loop } - }() - } else { - go func() { - loop: - for { - m := make(chan struct{}) - go func() { - f() - m <- struct{}{} - }() - select { - case <-m: - continue loop - case <-stopChan: - break loop - } - time.Sleep(DevInterval) + } + }() - } - }() - } return stopChan }