Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Using safe for simple worker (#1628)
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni authored Mar 18, 2019
1 parent 60d0616 commit e644879
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion runtime/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime

// Version is the current version of the buffalo binary
const Version = "v0.14.1"
const Version = "v0.14.2"
6 changes: 5 additions & 1 deletion worker/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"time"

"github.com/markbates/safe"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -82,7 +83,10 @@ func (w Simple) Perform(job Job) error {
defer w.moot.Unlock()
if h, ok := w.handlers[job.Handler]; ok {
go func() {
err := h(job.Args)
err := safe.RunE(func() error {
return h(job.Args)
})

if err != nil {
w.Logger.Error(err)
}
Expand Down
25 changes: 25 additions & 0 deletions worker/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ func Test_Simple_PerformAt(t *testing.T) {
r.True(hit)
}

func Test_Simple_PerformBroken(t *testing.T) {
r := require.New(t)

var hit bool
wg := &sync.WaitGroup{}
wg.Add(1)

w := NewSimple()
w.Register("x", func(Args) error {
hit = true
wg.Done()

//Index out of bounds on purpose
println([]string{}[0])

return nil
})

w.Perform(Job{
Handler: "x",
})
wg.Wait()
r.True(hit)
}

func Test_Simple_PerformIn(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit e644879

Please sign in to comment.