func runService(isDebug bool) { defer utils.Recover_and_log() logging.Debug("runService") err = svc.Run(command.PrimaryService.Name(), &serviceHandler{}) if err != nil { logging.Errorf("runService: failed: %v\r\n", err) } }
func main() { defer utils.Recover_and_log() logging.Debug("hickwall main ------- sub packages init process finished") app := cli.NewApp() app.Name = "hickwall" app.Usage = "collect metrics effortlessly." app.Version = fmt.Sprintf("%s - %s", Version, Build) app.Commands = []cli.Command{ //TODO: configuration test, reload // { // Name: "config", // ShortName: "", // Usage: "config", // Subcommands: []cli.Command{ // { // Name: "test", // ShortName: "", // Usage: "test", // Action: command.CmdConfigTest, // }, // { // Name: "reload", // ShortName: "", // Usage: "reload", // Action: command.CmdConfigReload, // }, // }, // }, { Name: "service", ShortName: "s", Usage: "service", Subcommands: []cli.Command{ { Name: "status", ShortName: "s", Usage: "status", Action: command.CmdServiceStatus, }, { Name: "statuscode", Usage: "statuscode(internal use only.)", Action: command.CmdServiceStatusCode, }, { Name: "install", ShortName: "i", Usage: "install service", Action: command.CmdServiceInstall, }, { Name: "remove", ShortName: "d", Usage: "remove service", Action: command.CmdServiceRemove, }, { Name: "start", ShortName: "g", Usage: "start service.", Action: command.CmdServiceStart, }, { Name: "stop", ShortName: "x", Usage: "stop service.", Action: command.CmdServiceStop, }, { Name: "restart", ShortName: "n", Usage: "restart service", Action: command.CmdServiceRestart, }, }, }, { Name: "version", ShortName: "v", Usage: "show version info", Action: func(c *cli.Context) { fmt.Printf("%s version: %s\n", app.Name, app.Version) }, }, { Name: "daemon", ShortName: "d", Usage: "run as daemon", Action: func(c *cli.Context) { run(false, false) }, }, { Name: "config", Usage: "show config info", Action: command.CmdShowConfig, }, } if len(os.Args) >= 2 { logging.Debug("executing commands") app.Run(os.Args) } else { isIntSess, err := servicelib.IsAnInteractiveSession() if err != nil { logging.Errorf("failed to determine if we are running in an interactive session or not: %v", err) return } if !isIntSess { logging.Debug("running as service") run(false, true) return } //print help here. app.Run(os.Args) } return }
func runAsPrimaryService(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { logging.Debug("runAsPrimaryService started") defer utils.Recover_and_log() const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} //http://localhost:6060/debug/pprof/ // utils.HttpPprofServe(6060) // after := time.After(time.Duration(8) * time.Minute) // f, _ := os.Create("d:\\cpu-" + strconv.Itoa(pid) + ".pprof") // pprof.StartCPUProfile(f) // defer pprof.StopCPUProfile() // cfg := profile.Config{ // MemProfile: true, // ProfilePath: "./pprofs/", // store profiles in current directory // NoShutdownHook: true, // do not hook SIGINT // } // p := profile.Start(&cfg) // // defer p.Stop() // utils.StartCPUProfile() // defer utils.StopCPUProfile() // go func() { // for { // <-time.After(time.Second * time.Duration(15)) // debug.FreeOSMemory() // } // }() err := hickwall.Start() if err != nil { logging.Critical("Failed To Start hickwall: %v", err) return } else { defer hickwall.Stop() } logging.Debug("service event handling loop started ") // major loop for signal processing. loop: for { select { case c := <-r: switch c.Cmd { case svc.Interrogate: changes <- c.CurrentStatus // testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4 time.Sleep(100 * time.Millisecond) changes <- c.CurrentStatus case svc.Stop, svc.Shutdown: break loop default: logging.Errorf("unexpected control request #%d", c) } } } changes <- svc.Status{State: svc.StopPending} logging.Debug("runAsPrimaryService stopped") return }