From 23f9ad13a176c3276414e37c7dd306a91e7e0a34 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 11 Jan 2017 18:01:29 +0200 Subject: [PATCH] Catch os.Interrupt signal with iris.PostInterrupt example added in comments. https://github.com/iris-contrib/examples/tree/master/os_interrupt --- HISTORY.md | 2 ++ iris.go | 2 ++ plugin.go | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 431f0edc..a03adc91 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -25,6 +25,8 @@ iris.Plugins.PostInterrupt(func(s *Framework){ ``` +- Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt + ## 6.0.7 -> 6.0.8 - Add `iris.UseTemplateFunc(functionName string, function interface{})`. You could always set custom template funcs by using each of [template engine's](https://github.com/kataras/go-template) configuration but this function will help newcomers to start creating their custom template funcs. diff --git a/iris.go b/iris.go index b578f7b0..c870b75f 100644 --- a/iris.go +++ b/iris.go @@ -541,7 +541,9 @@ func (s *Framework) postServe() { ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt) <-ch + // catch custom plugin event for interrupt + // Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt s.Plugins.DoPostInterrupt(s) if !s.Plugins.PostInterruptFired() { // if no PostInterrupt events fired, then I assume that the user doesn't cares diff --git a/plugin.go b/plugin.go index dfb8640e..767227ce 100644 --- a/plugin.go +++ b/plugin.go @@ -96,11 +96,13 @@ type ( // graceful shutdown can be done here // // Read more here: https://github.com/kataras/iris/blob/master/HISTORY.md#608---609 + // Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt 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 + // Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt PostInterruptFunc func(*Framework) // pluginPreClose implements the PreClose(*Framework) method @@ -220,6 +222,15 @@ func (fn PostListenFunc) PostListen(station *Framework) { fn(station) } +// 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 +// Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt +func (fn PostInterruptFunc) PostInterrupt(station *Framework) { + fn(station) +} + // PreClose it's being called only one time, BEFORE the Iris .Close method // any plugin cleanup/clear memory happens here // @@ -540,6 +551,7 @@ func (p *pluginContainer) PostListenFired() bool { // 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 +// Example: https://github.com/iris-contrib/examples/tree/master/os_interrupt func (p *pluginContainer) PostInterrupt(fn PostInterruptFunc) { p.Add(fn) }