Skip to content

Commit

Permalink
Update recover signature, improve default recover
Browse files Browse the repository at this point in the history
  • Loading branch information
sheb-gregor committed Nov 19, 2019
1 parent bd1c5b9 commit d88eb0f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
17 changes: 12 additions & 5 deletions v2/chief.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Chief interface {

type (
Locker func()
Recover func()
Recover func(name WorkerName)
Shutdown func()
EventHandler func(Event)
)
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *chief) SetRecover(recover Recover) {
}

func (c *chief) UseDefaultRecover() {
c.recover = func() {
c.recover = func(name WorkerName) {
r := recover()
if r == nil {
return
Expand All @@ -113,7 +113,13 @@ func (c *chief) UseDefaultRecover() {
err = fmt.Errorf("panic: %s\ntrace: %s", err, debug.Stack())
c.eventChan <- Event{
Level: LvlFatal,
Message: err.Error(),
Worker: name,
Message: "caught panic",
Fields: map[string]interface{}{
"worker": name,
"error": err.Error(),
"stack": string(debug.Stack()),
},
}
}
}
Expand Down Expand Up @@ -152,6 +158,8 @@ func (c *chief) Run() {

func (c *chief) Shutdown() {
c.cancel()

c.eventMutex.Unlock()
if c.shutdown != nil {
c.shutdown()
}
Expand Down Expand Up @@ -188,7 +196,6 @@ func (c *chief) run() {

func (c *chief) handleEvents(stop <-chan struct{}) {
c.eventMutex.Lock()
defer c.eventMutex.Unlock()

for {
select {
Expand Down Expand Up @@ -234,7 +241,7 @@ func (c *chief) runPool() error {
func (c *chief) runWorker(ctx Context, name WorkerName, doneCall func()) {
defer doneCall()
if c.recover != nil {
defer c.recover()
defer c.recover(name)
}

err := c.wPool.RunWorkerExec(ctx, name)
Expand Down
6 changes: 4 additions & 2 deletions v2/presets/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const ForceStopTimeout = 5 * time.Second

// Config is a parameters for `http.Server`.
type Config struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
EnableCORS bool `json:"enable_cors" yaml:"enable_cors"`
ApiRequestTimeout int `json:"api_request_timeout" yaml:"api_request_timeout"`
}

// Validate - Validate config required fields
Expand Down
14 changes: 7 additions & 7 deletions v2/presets/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

type Job struct {
period time.Duration
ticker *time.Ticker
run func() error
period time.Duration
ticker *time.Ticker
runAction func() error
}

func NewJob(period time.Duration, run func() error) *Job {
func NewJob(period time.Duration, runAction func() error) *Job {
return &Job{
period: period,
run: run,
period: period,
runAction: runAction,
}
}

Expand All @@ -28,7 +28,7 @@ func (j *Job) Run(ctx uwe.Context) error {
for {
select {
case <-j.ticker.C:
if err := j.run(); err != nil {
if err := j.runAction(); err != nil {
return err
}
case <-ctx.Done():
Expand Down

0 comments on commit d88eb0f

Please sign in to comment.