Automatically increase the process ulimit to maximum available. fixes #168.

This commit is contained in:
spf13 2014-02-01 12:51:11 -05:00
parent ae9cc09b04
commit 3e87d7a86e

View file

@ -14,32 +14,33 @@
package commands package commands
import ( import (
"fmt" "fmt"
"github.com/mostafah/fsync" "github.com/mostafah/fsync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils" "github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher" "github.com/spf13/hugo/watcher"
"github.com/spf13/nitro" "github.com/spf13/nitro"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time" "syscall"
"time"
) )
var Config *hugolib.Config var Config *hugolib.Config
var HugoCmd = &cobra.Command{ var HugoCmd = &cobra.Command{
Use: "hugo", Use: "hugo",
Short: "Hugo is a very fast static site generator", Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go. love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`, Complete documentation is available at http://hugo.spf13.com`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
InitializeConfig() InitializeConfig()
build() build()
}, },
} }
var hugoCmdV *cobra.Command var hugoCmdV *cobra.Command
@ -47,187 +48,201 @@ var BuildWatch, Draft, UglyUrls, Verbose bool
var Source, Destination, BaseUrl, CfgFile string var Source, Destination, BaseUrl, CfgFile string
func Execute() { func Execute() {
AddCommands() AddCommands()
utils.StopOnErr(HugoCmd.Execute()) utils.StopOnErr(HugoCmd.Execute())
} }
func AddCommands() { func AddCommands() {
HugoCmd.AddCommand(serverCmd) HugoCmd.AddCommand(serverCmd)
HugoCmd.AddCommand(version) HugoCmd.AddCommand(version)
HugoCmd.AddCommand(check) HugoCmd.AddCommand(check)
HugoCmd.AddCommand(benchmark) HugoCmd.AddCommand(benchmark)
} }
func init() { func init() {
HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft") HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft")
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to") HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") HugoCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyurls", false, "if true, use /filename.html instead of /filename/") HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyurls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/") HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "base-url", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)") HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program") HugoCmd.PersistentFlags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") HugoCmd.Flags().BoolVarP(&BuildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
hugoCmdV = HugoCmd hugoCmdV = HugoCmd
} }
func InitializeConfig() { func InitializeConfig() {
Config = hugolib.SetupConfig(&CfgFile, &Source) Config = hugolib.SetupConfig(&CfgFile, &Source)
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed { if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
Config.BuildDrafts = Draft Config.BuildDrafts = Draft
} }
if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed { if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed {
Config.UglyUrls = UglyUrls Config.UglyUrls = UglyUrls
} }
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed { if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
Config.Verbose = Verbose Config.Verbose = Verbose
} }
if BaseUrl != "" { if BaseUrl != "" {
Config.BaseUrl = BaseUrl Config.BaseUrl = BaseUrl
} }
if Destination != "" { if Destination != "" {
Config.PublishDir = Destination Config.PublishDir = Destination
} }
} }
func build(watches ...bool) { func build(watches ...bool) {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir))) utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
watch := false watch := false
if len(watches) > 0 && watches[0] { if len(watches) > 0 && watches[0] {
watch = true watch = true
} }
utils.StopOnErr(buildSite(BuildWatch || watch)) utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch { if BuildWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
fmt.Println("Press ctrl+c to stop") fmt.Println("Press ctrl+c to stop")
utils.CheckErr(NewWatcher(0)) utils.CheckErr(NewWatcher(0))
} }
} }
func copyStatic() error { func copyStatic() error {
staticDir := Config.GetAbsPath(Config.StaticDir + "/") staticDir := Config.GetAbsPath(Config.StaticDir + "/")
if _, err := os.Stat(staticDir); os.IsNotExist(err) { if _, err := os.Stat(staticDir); os.IsNotExist(err) {
return nil return nil
} }
// Copy Static to Destination // Copy Static to Destination
return fsync.Sync(Config.GetAbsPath(Config.PublishDir+"/"), Config.GetAbsPath(Config.StaticDir+"/")) return fsync.Sync(Config.GetAbsPath(Config.PublishDir+"/"), Config.GetAbsPath(Config.StaticDir+"/"))
} }
func getDirList() []string { func getDirList() []string {
var a []string var a []string
walker := func(path string, fi os.FileInfo, err error) error { walker := func(path string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
fmt.Println("Walker: ", err) fmt.Println("Walker: ", err)
return nil return nil
} }
if fi.IsDir() { if fi.IsDir() {
a = append(a, path) a = append(a, path)
} }
return nil return nil
} }
filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker) filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker)
filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker) filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker)
filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker) filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker)
return a return a
} }
func buildSite(watching ...bool) (err error) { func buildSite(watching ...bool) (err error) {
startTime := time.Now() startTime := time.Now()
site := &hugolib.Site{Config: *Config} site := &hugolib.Site{Config: *Config}
if len(watching) > 0 && watching[0] { if len(watching) > 0 && watching[0] {
site.RunMode.Watching = true site.RunMode.Watching = true
} }
err = site.Build() err = site.Build()
if err != nil { if err != nil {
return return
} }
site.Stats() site.Stats()
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
return nil return nil
} }
func NewWatcher(port int) error { func NewWatcher(port int) error {
watcher, err := watcher.New(1 * time.Second) var rLimit syscall.Rlimit
var wg sync.WaitGroup err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Getting Rlimit ", err)
}
if rLimit.Cur < rLimit.Max {
rLimit.Max = 999999
rLimit.Cur = 999999
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println("Error Setting rLimit ", err)
}
}
if err != nil { watcher, err := watcher.New(1 * time.Second)
fmt.Println(err) var wg sync.WaitGroup
return err
}
defer watcher.Close() if err != nil {
fmt.Println(err)
return err
}
wg.Add(1) defer watcher.Close()
for _, d := range getDirList() { wg.Add(1)
if d != "" {
_ = watcher.Watch(d)
}
}
go func() { for _, d := range getDirList() {
for { if d != "" {
select { _ = watcher.Watch(d)
case evs := <-watcher.Event: }
if Verbose { }
fmt.Println(evs)
}
static_changed := false go func() {
dynamic_changed := false for {
select {
case evs := <-watcher.Event:
if Verbose {
fmt.Println(evs)
}
for _, ev := range evs { static_changed := false
ext := filepath.Ext(ev.Name) dynamic_changed := false
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp")
if istemp {
continue
}
// renames are always followed with Create/Modify
if ev.IsRename() {
continue
}
isstatic := strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) for _, ev := range evs {
static_changed = static_changed || isstatic ext := filepath.Ext(ev.Name)
dynamic_changed = dynamic_changed || !isstatic istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp")
if istemp {
continue
}
// renames are always followed with Create/Modify
if ev.IsRename() {
continue
}
// add new directory to watch list isstatic := strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir))
if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() { static_changed = static_changed || isstatic
if ev.IsCreate() { dynamic_changed = dynamic_changed || !isstatic
watcher.Watch(ev.Name)
}
}
}
if static_changed { // add new directory to watch list
fmt.Println("Static file changed, syncing\n") if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir))) if ev.IsCreate() {
} watcher.Watch(ev.Name)
}
}
}
if dynamic_changed { if static_changed {
fmt.Println("Change detected, rebuilding site\n") fmt.Println("Static file changed, syncing\n")
utils.StopOnErr(buildSite(true)) utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
} }
case err := <-watcher.Error:
if err != nil {
fmt.Println("error:", err)
}
}
}
}()
if port > 0 { if dynamic_changed {
go serve(port) fmt.Println("Change detected, rebuilding site\n")
} utils.StopOnErr(buildSite(true))
}
case err := <-watcher.Error:
if err != nil {
fmt.Println("error:", err)
}
}
}
}()
wg.Wait() if port > 0 {
return nil go serve(port)
}
wg.Wait()
return nil
} }