You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

107 lines
2.7 KiB

  1. // Copyright 2012 Xing Xing <mikespook@gmail.com>.
  2. // All rights reserved.
  3. // Use of this source code is governed by a commercial
  4. // license that can be found in the LICENSE file.
  5. package main
  6. import (
  7. "os"
  8. "flag"
  9. "time"
  10. "github.com/mikespook/golib/log"
  11. "github.com/mikespook/golib/pid"
  12. "github.com/mikespook/golib/prof"
  13. "github.com/mikespook/golib/signal"
  14. "github.com/mikespook/gearman-go/worker"
  15. )
  16. var (
  17. pidfile = flag.String("pid", "/run/seedworker.pid",
  18. "PID file to write pid")
  19. proffile = flag.String("prof", "", "Profiling file")
  20. dumpfile = flag.String("dump", "", "Heap dumping file")
  21. dumptime = flag.Int("dumptime", 5, "Heap dumping time interval")
  22. joblimit = flag.Int("job-limit", worker.Unlimited,
  23. "Maximum number of concurrently executing job." +
  24. " Zero is unlimited.")
  25. basedir = flag.String("basedir", "", "Working directory of the php scripts.")
  26. timeout = flag.Uint("timeout", 30, "Executing time out.")
  27. gearmand = flag.String("gearmand", "127.0.0.1:4730", "Address and port of gearmand")
  28. )
  29. func init() {
  30. flag.Parse()
  31. initLog()
  32. if *basedir == "" {
  33. *basedir = "./script/"
  34. }
  35. }
  36. func main() {
  37. log.Message("Starting ... ")
  38. defer func() {
  39. time.Sleep(time.Second)
  40. log.Message("Shutdown complate!")
  41. }()
  42. // init profiling file
  43. if *proffile != "" {
  44. log.Debugf("Open a profiling file: %s", *proffile)
  45. if err := prof.Start(*proffile); err != nil {
  46. log.Error(err)
  47. } else {
  48. defer prof.Stop()
  49. }
  50. }
  51. // init heap dumping file
  52. if *dumpfile != "" {
  53. log.Debugf("Open a heap dumping file: %s", *dumpfile)
  54. if err := prof.NewDump(*dumpfile); err != nil {
  55. log.Error(err)
  56. } else {
  57. defer prof.CloseDump()
  58. go func() {
  59. for prof.Dumping {
  60. time.Sleep(time.Duration(*dumptime) * time.Second)
  61. prof.Dump()
  62. }
  63. }()
  64. }
  65. }
  66. // init pid file
  67. log.Debugf("Open a pid file: %s", *pidfile)
  68. if pidFile, err := pid.New(*pidfile); err != nil {
  69. log.Error(err)
  70. } else {
  71. defer pidFile.Close()
  72. }
  73. w := worker.New(*joblimit)
  74. if err := w.AddServer(*gearmand); err != nil {
  75. log.Error(err)
  76. return
  77. }
  78. if err := w.AddFunc("exec", execShell, uint32(*timeout)); err != nil {
  79. log.Error(err)
  80. return
  81. }
  82. if err := w.AddFunc("execphp", execPHP, uint32(*timeout)); err != nil {
  83. log.Error(err)
  84. return
  85. }
  86. defer w.Close()
  87. go w.Work()
  88. // signal handler
  89. sh := signal.NewHandler()
  90. sh.Bind(os.Interrupt, func() bool {return true})
  91. sh.Loop()
  92. }