Death - 利用信号管理Go应用程序关闭

网友投稿 818 2022-10-22

Death - 利用信号管理Go应用程序关闭

Death - 利用信号管理Go应用程序关闭

Simple library to make it easier to manage the death of your application.

Get The Library

Use gopkg.in to import death based on your logger.

VersionGo Get URLsourcedocNotes
3.xgopkg.in/vrecan/death.v3sourcedocThis removes the need for an independent logger. By default death will not log but will return an error if all the closers do not properly close. If you want to provide a logger just satisfy the deathlog.Logger interface.
2.xgopkg.in/vrecan/death.v2sourcedocThis supports loggers who do not return an error from their Error and Warn functions like logrus
1.xgopkg.in/vrecan/death.v1soucedocThis supports loggers who do return an error from their Error and Warn functions like seelog

Example

go get gopkg.in/vrecan/death.v3

Use The Library

package mainimport ( DEATH "github.com/vrecan/death" SYS "syscall")func main() { death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application //when you want to block for shutdown signals death.WaitForDeath() // this will finish when a signal of your type is sent to your application}

Close Other Objects On Shutdown

One simple feature of death is that it can also close other objects when shutdown starts

package mainimport ( "log" DEATH "github.com/vrecan/death" SYS "syscall" "io")func main() { death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application objects := make([]io.Closer, 0) objects = append(objects, &NewType{}) // this will work as long as the type implements a Close method //when you want to block for shutdown signals err := death.WaitForDeath(objects...) // this will finish when a signal of your type is sent to your application if err != nil { log.Println(err) os.Exit(1) }}type NewType struct {}func (c *NewType) Close() error { return nil}

Or close using an anonymous function

package mainimport ( DEATH "github.com/vrecan/death" SYS "syscall")func main() { death := DEATH.NewDeath(SYS.SIGINT, SYS.SIGTERM) //pass the signals you want to end your application //when you want to block for shutdown signals death.WaitForDeathWithFunc(func(){ //do whatever you want on death }) }

Release Process

Rules for release branches:

If you are releasing a new major version you need to branch off of master into a branch release-branch.v# (example release-branch.v2 for a 2.x release)If you are releasing a minor or patch update to an existing major release make sure to merge master into the release branch

Rules for tagging and publishing the release

When you are ready to publish the release make sure you...

Merge your changes into the correct release branch.Check out the release branch locally (example: git pull origin release-branch.v3)Create a new tag for the specific release version you will publish (example: git tag v3.0.1)Push the tag up to github (example: git push origin v3.0.1)Go to the release tab in githubSelect the target branch as the release branch and type in the tag name (tagname should include v so example: v3.0.1)Write a title and a well worded description on exactly what is in this changeClick publish release

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis如何使用动态语句实现批量删除(delete结合foreach)
下一篇:new Bigdecimal(double) 和 Bigdecimal.valueof()和BigDecimal(String)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~