1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 04:17:03 +00:00

Update to 6.0.9: Add PostInterrupt plugin. Read HISTORY.md

- Add `PostInterrupt` plugin, useful for customization of the
**os.Interrupt** singal, before that Iris closed the server
automatically.

```go
iris.Plugins.PostInterrupt(func(s *Framework){
// when os.Interrupt signal is fired the body of this function will be
fired,
// you're responsible for closing the server with s.Close()

// if that event is not registered then the framework
// will close the server for you.

/* Do  any custom cleanup and finally call the s.Close()
remember you have the iris.Plugins.PreClose(func(s *Framework)) event
too
so you can split your logic in two logically places.
*/

})

```
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-01-11 16:23:38 +02:00
parent c5f95ba78a
commit 5ad7c6e01f
5 changed files with 117 additions and 18 deletions

View File

@@ -89,6 +89,20 @@ type (
}
// PostListenFunc implements the simple function listener for the PostListen(*Framework)
PostListenFunc func(*Framework)
// pluginPostInterrupt implements the PostInterrupt(*Framework) method
pluginPostInterrupt interface {
// PostInterrupt it's being called only one time, when os.Interrupt system event catched
// graceful shutdown can be done here
//
// Read more here: https://github.com/kataras/iris/blob/master/HISTORY.md#608---609
PostInterrupt(*Framework)
}
// PostInterruptFunc implements the simple function listener for the PostInterrupt(*Framework)
//
// Read more here: https://github.com/kataras/iris/blob/master/HISTORY.md#608---609
PostInterruptFunc func(*Framework)
// pluginPreClose implements the PreClose(*Framework) method
pluginPreClose interface {
// PreClose it's being called only one time, BEFORE the Iris .Close method
@@ -139,6 +153,9 @@ type (
PostListen(PostListenFunc)
DoPostListen(*Framework)
PostListenFired() bool
PostInterrupt(PostInterruptFunc)
DoPostInterrupt(*Framework)
PostInterruptFired() bool
PreClose(PreCloseFunc)
DoPreClose(*Framework)
PreCloseFired() bool
@@ -520,6 +537,29 @@ func (p *pluginContainer) PostListenFired() bool {
return p.Fired("postlisten") > 0
}
// PostInterrupt adds a PostInterrupt plugin-function to the plugin flow container
//
// Read more here: https://github.com/kataras/iris/blob/master/HISTORY.md#608---609
func (p *pluginContainer) PostInterrupt(fn PostInterruptFunc) {
p.Add(fn)
}
// DoPostInterrupt raise all plugins which has the PostInterrupt method
func (p *pluginContainer) DoPostInterrupt(station *Framework) {
for i := range p.activatedPlugins {
// check if this method exists on our plugin obj, these are optionaly and call it
if pluginObj, ok := p.activatedPlugins[i].(pluginPostInterrupt); ok {
pluginObj.PostInterrupt(station)
p.fire("postinterrupt")
}
}
}
// PostInterruptFired returns true if PostInterrupt event/ plugin type is fired at least one time
func (p *pluginContainer) PostInterruptFired() bool {
return p.Fired("postinterrupt") > 0
}
// PreClose adds a PreClose plugin-function to the plugin flow container
func (p *pluginContainer) PreClose(fn PreCloseFunc) {
p.Add(fn)