From 5ed371b58048e8bd0d7e22a0dc8581ed1315269c Mon Sep 17 00:00:00 2001 From: Carlos Lalimarmo Date: Mon, 7 Oct 2013 14:09:35 -0400 Subject: [PATCH] Export Signals, so closure-shared resources can be cleaned up on quit --- doc.go | 13 +++++++++++-- goworker.go | 2 +- signals.go | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/doc.go b/doc.go index e52e7b4..0b32093 100644 --- a/doc.go +++ b/doc.go @@ -42,7 +42,9 @@ // } // // To create workers that share a database pool or other -// resources, use a closure to share variables. +// resources, use a closure to share variables. Clean up +// shared resources using the channel provided by the +// Signals function. // // package main // @@ -50,8 +52,15 @@ // "github.com/benmanns/goworker" // ) // -// func newMyFunc(uri string) { +// func newMyFunc(uri string) func(string, args ...interface{}) error { // foo := NewFoo(uri) +// +// quit := goworker.Signals() +// go func() { +// <-quit +// foo.CleanUp() +// }() +// // return func(queue string, args ...interface{}) error { // foo.Bar(args) // return nil diff --git a/goworker.go b/goworker.go index 5406856..2be6fa0 100644 --- a/goworker.go +++ b/goworker.go @@ -26,7 +26,7 @@ func Work() error { return err } - quit := signals() + quit := Signals() pool := newRedisPool(uri, connections, connections, time.Minute) defer pool.Close() diff --git a/signals.go b/signals.go index 95697cd..8952106 100644 --- a/signals.go +++ b/signals.go @@ -44,7 +44,7 @@ import ( "syscall" ) -func signals() <-chan bool { +func Signals() <-chan bool { quit := make(chan bool) go func() {