本文整理汇总了Golang中os/signal.Notify函数的典型用法代码示例。如果您正苦于以下问题:Golang Notify函数的具体用法?Golang Notify怎么用?Golang Notify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Notify函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ValidateMain
// ValidateMain is called by the generated validate command.
// ke: {"func": {"notest": true}}
func ValidateMain(path string) {
// Using process.Flags as the options means that the non-specified options are read from the
// command flags.
update := false
options := &process.Flags{
Update: &update,
Path: &path,
}
ctx, cancel, err := process.Initialise(context.Background(), options)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// The log function outputs errors and messages to the console. We pass it in to permit testing.
log := func(message string) {
fmt.Println(message)
}
// The interrupt chanel signals that Ctrl+C or other OS interrupts have happened.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
signal.Notify(interrupt, syscall.SIGTERM)
exitStatus := validateMain(ctx, cancel, log, interrupt)
wgctx.WaitAndExit(ctx, exitStatus)
}
示例2: main
func main() {
flag.Parse()
if *host == "" || *topic == "" {
fmt.Printf("pararm error,host=%s,topic=%s\n", *host, *topic)
os.Exit(0)
}
hosts := strings.Split(*host, ",")
//kafka
kafkaClient := NewKafkaClient()
err := kafkaClient.NewConsumer(hosts)
if err != nil {
os.Exit(-2)
}
defer kafkaClient.Close()
kafkaClient.GetTopicMsg(*topic)
// Trap SIGINT to trigger a shutdown.
signals := make(chan os.Signal, 2)
signal.Notify(signals, os.Interrupt)
signal.Notify(signals, os.Kill)
ConsumerLoop:
for {
select {
case msg := <-kafkaClient.Topicmap[*topic].Msgs:
log.Println("get msg ", string(msg.Value))
case <-signals:
break ConsumerLoop
}
}
}
示例3: main
func main() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
go func() {
<-sigchan
shutdownFlag = true
}()
rides = map[int64]*Ride{}
rideUpdates = make(chan *Ride, 0)
http.Handle("/live", websocket.Handler(livemap))
http.HandleFunc("/location", locationHandler)
// run main application in goroutine
go http.ListenAndServe(":8080", nil)
go updateClients()
go rideCleanup()
for !shutdownFlag {
time.Sleep(1 * time.Second)
}
shutdown()
os.Exit(1)
}
示例4: backup
//Called by main
//If main dies, we get missed signals -> kill the parent and return
func backup() {
missed_signal := 0
sigint := make(chan os.Signal, 1)
sigquit := make(chan os.Signal, 1)
sigusr := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT)
signal.Notify(sigquit, syscall.SIGQUIT)
signal.Notify(sigusr, syscall.SIGUSR1)
ppid := os.Getppid()
log.Println("Backup started by pid: ", ppid)
for {
select {
case <-sigusr:
missed_signal = 0
time.Sleep(200 * time.Millisecond)
case <-sigint:
log.Println("Backup caught SIGINT, ignore")
case <-sigquit:
log.Println("Received SIGQUIT. Shutting down backup")
os.Exit(0)
default:
if missed_signal == 3 {
killParent(ppid)
return
}
missed_signal += 1
time.Sleep(200 * time.Millisecond)
}
}
}
示例5: main
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
cleanup()
os.Exit(1)
}()
name := flag.String("name", "default", "vm name")
flag.Parse()
if *name == "" {
os.Exit(10)
}
for {
fp, err := os.OpenFile("/tmp/"+*name, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
fmt.Fprintf(fp, "%s:%d alive\n", *name, os.Getpid())
fp.Close()
time.Sleep(5 * time.Second)
}
}
示例6: Start
// Start - Will loop throught each of servers and attempt to start them up.
// In case that channel receives single error it will kill function otherwise, will listen
// forever
func (m *Manager) Start() error {
m.Info("Starting service management...")
errors := make(chan error)
signal.Notify(m.GetInterruptChan(), os.Interrupt)
signal.Notify(m.GetInterruptChan(), syscall.SIGTERM)
go m.HandleInterrupt()
for _, serv := range m.Servers {
go func(serv servers.Serverer) {
if err := serv.Start(); err != nil {
errors <- err
}
}(serv)
}
select {
case err := <-errors:
return err
case <-m.GetInterruptChan():
m.Warn("Service management interrupt signal caught. Giving it few seconds before we shut down permanently...")
close(m.GetInterruptChan())
time.Sleep(m.InterruptWaitTimeout)
m.Info("All done :( We go underground now for good...")
return nil
}
}
示例7: main
func main() {
flag.Parse()
if helpFlag {
flag.Usage()
os.Exit(0)
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
signal.Notify(c, os.Interrupt)
s := daemon.NewServer()
go func() {
<-c
if s != nil {
s.Close()
}
os.Exit(1)
}()
if s == nil {
util.Warn.Println("Failed to start Hover Server")
os.Exit(1)
}
util.Info.Printf("Hover Server listening on %s\n", listenSocket)
http.ListenAndServe(listenSocket, s.Handler())
}
示例8: signalHandle
func signalHandle() {
c := make(chan os.Signal)
signal.Notify(c)
signal.Notify(c, syscall.SIGHUP)
s := <-c
fmt.Println("get signal:", s)
}
示例9: registerSignals
func (lc *LogCourier) registerSignals() {
// *nix systems support SIGTERM so handle shutdown on that too
signal.Notify(lc.shutdown_chan, os.Interrupt, syscall.SIGTERM)
// *nix has SIGHUP for reload
signal.Notify(lc.reload_chan, syscall.SIGHUP)
}
示例10: Launch
func Launch(option *options.SysOption) {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)
signal.Notify(signalChannel, syscall.SIGTERM)
coremethods.Setup()
mustExit := false
stat := statistic.NewStatistic(*option)
manager := coresupport.NewCoreWorkerManager(*option, stat)
server := connectionserver.NewServer(*option, stat)
server.Start(manager)
manager.Start(server)
// wait
for !mustExit {
select {
case newSig := <-signalChannel:
{
if newSig != nil {
rllogger.Output(rllogger.LogInfo, "Stoping service, now wait..")
server.Stop()
manager.Stop()
}
}
case <-manager.OutSignalChannel:
{
rllogger.Output(rllogger.LogInfo, "Close manager.")
mustExit = true
}
}
}
manager.Close()
stat.Close()
close(signalChannel)
}
示例11: watchSignals
func watchSignals(ex chan struct{}, t chan Trigger) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
signal.Notify(sigCh, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGHUP)
// main control flow
for {
select {
// when exit chan closes, return
case <-ex:
return
// If a signal is caught, either shutdown or reload gracefully
case sig := <-sigCh:
switch sig {
case os.Interrupt:
fallthrough
case syscall.SIGTERM:
t <- TriggerStop
case syscall.SIGHUP:
t <- TriggerReload
}
}
}
}
示例12: main
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Printf("Could not open profile file: %s", err)
} else {
log.Printf("Logging CPU profile to %s", *cpuprofile)
pprof.StartCPUProfile(f)
}
}
database := ridc.CreateDatabase()
go ridc.StartWebInterface(database)
go ridc.StartSocketInterface(database, "3001")
cleanup := func() {
pprof.StopCPUProfile()
}
signalHandler := make(chan os.Signal, 1)
signal.Notify(signalHandler, os.Interrupt)
signal.Notify(signalHandler, syscall.SIGTERM)
go func() {
<-signalHandler
log.Printf("Shutting down...")
cleanup()
os.Exit(1)
}()
select {}
}
示例13: sigHandleDemo
func sigHandleDemo() {
sigRecv1 := make(chan os.Signal, 1)
sigs1 := []os.Signal{syscall.SIGINT, syscall.SIGQUIT}
fmt.Printf("Set notification for %s... [sigRecv1]\n", sigs1)
signal.Notify(sigRecv1, sigs1...)
sigRecv2 := make(chan os.Signal, 1)
sigs2 := []os.Signal{syscall.SIGQUIT}
fmt.Printf("Set notification for %s... [sigRecv2]\n", sigs2)
signal.Notify(sigRecv2, sigs2...)
var wg sync.WaitGroup
wg.Add(2)
go func() {
for sig := range sigRecv1 {
fmt.Printf("Received a signal from sigRecv1: %s\n", sig)
}
fmt.Printf("End. [sigRecv1]\n")
wg.Done()
}()
go func() {
for sig := range sigRecv2 {
fmt.Printf("Received a signal from sigRecv2: %s\n", sig)
}
fmt.Printf("End. [sigRecv2]\n")
wg.Done()
}()
fmt.Println("Wait for 2 seconds... ")
time.Sleep(2 * time.Second)
fmt.Printf("Stop notification...")
signal.Stop(sigRecv1)
close(sigRecv1)
fmt.Printf("done. [sigRecv1]\n")
wg.Wait()
}
示例14: run
func run(context *cli.Context) {
etcdURI, serverKey, uri := getOpts(context)
control := make(chan bool)
go loop(etcdURI, serverKey, uri, control)
go iterate(control)
sigTerm := make(chan os.Signal)
signal.Notify(sigTerm, syscall.SIGTERM)
sigHup := make(chan os.Signal)
signal.Notify(sigHup, syscall.SIGHUP)
go func() {
for {
<-sigHup
fmt.Println("SIGHUP received, deregistering")
control <- false
onNotHealthy(etcdURI, serverKey)
fmt.Println("deregistered, paused for 5 seconds")
time.Sleep(5 * time.Second)
go loop(etcdURI, serverKey, uri, control)
}
}()
<-sigTerm
fmt.Println("SIGTERM received, cleaning up")
control <- false
onNotHealthy(etcdURI, serverKey)
os.Exit(0)
}
示例15: Run
// Run starts an elector loop.
func (e *Elector) Run() error {
log.Println("Starting elector.")
if e.updates != nil {
return fmt.Errorf("Elector has already been initialized. Electors can be started once.")
}
// 0 so blocks on updates while error handler is executing
e.updates = make(chan State)
go e.reconcileStateLoop()
go e.ElectionBackend.ElectionLoop(e.updates)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
e.updates <- StateNotLeader
log.Println("Allowing 10s for StateNotLeader to process.")
time.Sleep(10 * time.Second)
}()
group := sync.WaitGroup{}
group.Add(2)
defer group.Done()
group.Wait()
return nil
}