Code Monkey home page Code Monkey logo

Comments (8)

jaronnie avatar jaronnie commented on May 27, 2024
func signalHandler() {
	// signal handler
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		s := <-c
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			// do what you want
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}

from go-zero.

jaronnie avatar jaronnie commented on May 27, 2024

demo repo: https://github.com/jaronnie/jzero/blob/main/daemon/daemon.go

from go-zero.

Issues-translate-bot avatar Issues-translate-bot commented on May 27, 2024

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


But report: https://gityub. Kom/jaron-ie/jzero/bb/main/daemon/daemon. Go

from go-zero.

fireice009 avatar fireice009 commented on May 27, 2024

go-zero already has this logic, but the function 'gracefulStop' did not execute as expected.

core/proc/signals.go

func init() {
	go func() {
		// https://golang.org/pkg/os/signal/#Notify
		signals := make(chan os.Signal, 1)
		signal.Notify(signals, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGINT)

		for {
			v := <-signals
			switch v {
			case syscall.SIGUSR1:
				dumpGoroutines(fileCreator{})
			case syscall.SIGUSR2:
				profiler := StartProfile()
				time.AfterFunc(profileDuration, profiler.Stop)
			case syscall.SIGTERM:
				stopOnSignal()
				gracefulStop(signals, syscall.SIGTERM)
			case syscall.SIGINT:
				stopOnSignal()
				gracefulStop(signals, syscall.SIGINT)
			default:
				logx.Error("Got unregistered signal:", v)
			}
		}
	}()
}

from go-zero.

kevwan avatar kevwan commented on May 27, 2024
waitForCalled := proc.AddWrapUpListener(func() {
	fmt.Println("wrap up start...")
	time.Sleep(5 * time.Second)
	fmt.Println("wrap up done")
})
waitForCalled()

from go-zero.

fireice009 avatar fireice009 commented on May 27, 2024
waitForCalled := proc.AddWrapUpListener(func() {
	fmt.Println("wrap up start...")
	time.Sleep(5 * time.Second)
	fmt.Println("wrap up done")
})
waitForCalled()

waitForCalled() will block the flow, before server.Start()

from go-zero.

kevwan avatar kevwan commented on May 27, 2024
waitForCalled := proc.AddWrapUpListener(func() {
	fmt.Println("wrap up start...")
	time.Sleep(5 * time.Second)
	fmt.Println("wrap up done")
})
waitForCalled()

waitForCalled() will block the flow, before server.Start()

Call it after server started.

from go-zero.

fireice009 avatar fireice009 commented on May 27, 2024

server.Start() will block the main thread.
gracefulStop does not wait when i run test() in a new goroutine.

from go-zero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.