Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
add recover on receiveEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Prud'homme committed Feb 27, 2019
1 parent deb8c16 commit 3391d8d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package godim
import (
"errors"
"log"
"runtime"
)

// Event an Event can be transmitted from an Emitter to any Receiver
Expand Down Expand Up @@ -122,9 +123,21 @@ func (es *EventSwitch) switchEvent(event *Event) {
rs, ok := es.receivers[event.Type]
if ok {
for _, receiver := range rs {
go receiver.ReceiveEvent(event)
go es.runObserverWithRecover(receiver, event)
}
} else {
log.Println("an event is declared with no subscribers :", event.Type)
}
}

func (es *EventSwitch) runObserverWithRecover(receiver EventReceiver, event *Event) {
defer func() {
if rec := recover(); rec != nil {
const size = 64 << 10
buffer := make([]byte, size)
buffer = buffer[:runtime.Stack(buffer, false)]
log.Printf("cron: panic running job: %v\n%s", rec, buffer)
}
}()
receiver.ReceiveEvent(event)
}

0 comments on commit 3391d8d

Please sign in to comment.