-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Make cmd's return `*errors.WakuError` - Fixup some logging - Have `Print` operations respect `log.LogLevel` --------- Closes #155 Signed-off-by: AlexNg <contact@ngjx.org>
- Loading branch information
Showing
18 changed files
with
208 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// For async cleanup in cases of interrupts | ||
package cleanup | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/caffeine-addictt/waku/internal/log" | ||
) | ||
|
||
var ( | ||
cleanupStack []func() error | ||
cleanupRwLock sync.RWMutex | ||
errorCleanupStack []func() error | ||
errorRwLock sync.RWMutex | ||
) | ||
|
||
// Schedule adds a function to the cleanup stack | ||
// | ||
// Cleanups are ran no matter what, before error cleanup | ||
func Schedule(fn func() error) { | ||
cleanupRwLock.Lock() | ||
defer cleanupRwLock.Unlock() | ||
cleanupStack = append(cleanupStack, fn) | ||
} | ||
|
||
// ScheduleError adds a function to the error cleanup stack | ||
// | ||
// Error cleanups are ran ONLY when an error occurs, and | ||
// are ran after all regular cleanups | ||
func ScheduleError(fn func() error) { | ||
errorRwLock.Lock() | ||
defer errorRwLock.Unlock() | ||
errorCleanupStack = append(errorCleanupStack, fn) | ||
} | ||
|
||
// Runs all cleanup functions | ||
func Cleanup() { | ||
cleanupRwLock.Lock() | ||
defer cleanupRwLock.Unlock() | ||
|
||
log.Debugf("cleaning up %d items...\n", len(cleanupStack)) | ||
for i := len(cleanupStack) - 1; i >= 0; i-- { | ||
if err := cleanupStack[i](); err != nil { | ||
log.Errorf("error while cleaning up: %v\n", err) | ||
} | ||
} | ||
|
||
cleanupStack = []func() error{} | ||
} | ||
|
||
// Runs all error cleanup functions | ||
func CleanupError() { | ||
errorRwLock.Lock() | ||
defer errorRwLock.Unlock() | ||
|
||
log.Debugf("cleaning up %d items due to error...\n", len(errorCleanupStack)) | ||
for i := len(errorCleanupStack) - 1; i >= 0; i-- { | ||
if err := errorCleanupStack[i](); err != nil { | ||
log.Errorf("error while cleaning up: %v\n", err) | ||
} | ||
} | ||
|
||
errorCleanupStack = []func() error{} | ||
} | ||
|
||
// Watches for interrupts and runs all cleanup functions. | ||
func On() { | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
sig := <-sigs | ||
log.Printf("%v received, cleaning up...\n", sig) | ||
|
||
Cleanup() | ||
CleanupError() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.