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

Commit

Permalink
Remove dependency on safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Fasching authored and fasmat committed May 15, 2022
1 parent d99b8b9 commit 6d1fac1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/gorilla/sessions v1.2.1
github.com/markbates/grift v1.5.0
github.com/markbates/refresh v1.12.0
github.com/markbates/safe v1.0.1
github.com/monoculum/formam v3.5.5+incompatible
github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef
github.com/sirupsen/logrus v1.8.1
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ github.com/markbates/grift v1.5.0/go.mod h1:1ssFm5gSGmzTkhi3Wfh/nqlU74J73TlAjoDM
github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI=
github.com/markbates/refresh v1.12.0 h1:vTgVPX4p77v26auBJhlgaTQ/adWAYSIcVYJvM63Nbpo=
github.com/markbates/refresh v1.12.0/go.mod h1:Vpwi1+q+2U1VxE7C0Ilj6r2/+TigRzQcLez6XM3bPLc=
github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
Expand Down
36 changes: 15 additions & 21 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gobuffalo/buffalo/plugins"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/events"
"github.com/markbates/safe"
)

var loadPlugins sync.Once
Expand All @@ -35,28 +34,23 @@ func LoadPlugins() error {
continue
}
err := func(c plugins.Command) error {
return safe.RunE(func() error {
n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name)
fn := func(e events.Event) {
b, err := json.Marshal(e)
if err != nil {
fmt.Println("error trying to marshal event", e, err)
return
}
cmd := exec.Command(c.Binary, c.UseCommand, string(b))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err)
}
}
_, err := events.NamedListen(n, events.Filter(c.ListenFor, fn))
n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name)
fn := func(e events.Event) {
b, err := json.Marshal(e)
if err != nil {
return err
fmt.Println("error trying to marshal event", e, err)
return
}
cmd := exec.Command(c.Binary, c.UseCommand, string(b))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err)
}
return nil
})
}
_, err := events.NamedListen(n, events.Filter(c.ListenFor, fn))
return err
}(c)
if err != nil {
errResult = err
Expand Down
20 changes: 18 additions & 2 deletions worker/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package worker

import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/markbates/safe"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -122,7 +122,7 @@ func (w *Simple) Perform(job Job) error {
w.wg.Add(1)
go func() {
defer w.wg.Done()
err := safe.RunE(func() error {
err := safeRun(func() error {
return h(job.Args)
})

Expand All @@ -139,6 +139,22 @@ func (w *Simple) Perform(job Job) error {
return err
}

// safeRun the function safely knowing that if it panics
// the panic will be caught and returned as an error
func safeRun(fn func() error) (err error) {
defer func() {
if ex := recover(); ex != nil {
if e, ok := ex.(error); ok {
err = e
return
}
err = errors.New(fmt.Sprint(ex))
}
}()

return fn()
}

// PerformAt performs a job at a particular time using a goroutine.
func (w *Simple) PerformAt(job Job, t time.Time) error {
return w.PerformIn(job, time.Until(t))
Expand Down

0 comments on commit 6d1fac1

Please sign in to comment.