Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3d25441e7a | |||
735d314a19 | |||
4f4c7a0627 | |||
461d0b77e9 | |||
861d6af1d7 |
@ -41,6 +41,7 @@ type environment struct {
|
||||
logDirectory string
|
||||
configFile string
|
||||
verbose bool
|
||||
crash bool
|
||||
}
|
||||
|
||||
// running stores whether the environment is currently running.
|
||||
@ -282,7 +283,12 @@ func (this *environment) runRunnable(ctx context.Context, actor Runnable) (stopE
|
||||
for {
|
||||
// run actor
|
||||
lastStart := time.Now()
|
||||
err := panicWrapCtx(ctx, actor.Run)
|
||||
var err error
|
||||
if this.flags.crash {
|
||||
err = actor.Run(ctx)
|
||||
} else {
|
||||
err = panicWrapCtx(ctx, actor.Run)
|
||||
}
|
||||
|
||||
// detect context cancellation
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
|
35
examples/panic/main.go
Normal file
35
examples/panic/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
// Example panic demonstrates how the environment can restart actors if they
|
||||
// fail.
|
||||
package main
|
||||
|
||||
import "log"
|
||||
import "time"
|
||||
import "errors"
|
||||
import "context"
|
||||
import "math/rand"
|
||||
import "git.tebibyte.media/sashakoshka/camfish"
|
||||
|
||||
func main() {
|
||||
camfish.Run("panic",
|
||||
"Example panic demonstrates how the environment can restart " +
|
||||
"actors if they fail",
|
||||
new(actor))
|
||||
}
|
||||
|
||||
// actor is an incorrectly implemented actor that panics and errs randomly.
|
||||
type actor struct { }
|
||||
var _ camfish.Runnable = new(actor)
|
||||
func (this *actor) Type() string { return "panic" }
|
||||
|
||||
func (this *actor) Run(ctx context.Context) error {
|
||||
log.Println("(i) [panic] panicking in 10 seconds")
|
||||
select {
|
||||
case <- ctx.Done(): return ctx.Err()
|
||||
case <- time.After(time.Second * 10):
|
||||
if rand.Int() % 2 == 0 {
|
||||
panic("this is a panic")
|
||||
} else {
|
||||
return errors.New("this is an error")
|
||||
}
|
||||
}
|
||||
}
|
10
phases.go
10
phases.go
@ -24,6 +24,7 @@ func (this *environment) phase10FlagParsing() bool {
|
||||
flagLogDirectory := set.Flag('l', "log-directory", "Write logs to the specified directory", cli.ValString)
|
||||
flagConfigFile := set.Flag('c', "config-file", "Use this configuration file", cli.ValString)
|
||||
flagVerbose := set.Flag('v', "verbose", "Enable verbose output/logging", nil)
|
||||
flagCrash := set.Flag(0, "crash", "Crash when an actor panics", nil)
|
||||
|
||||
// ask actors to add flags
|
||||
actors, done := this.actors.RBorrow()
|
||||
@ -60,6 +61,9 @@ func (this *environment) phase10FlagParsing() bool {
|
||||
if _, ok := flagVerbose.First(); ok {
|
||||
this.flags.verbose = true
|
||||
}
|
||||
if _, ok := flagCrash.First(); ok {
|
||||
this.flags.crash = true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@ -128,6 +132,12 @@ func (this *environment) phase30ConfigurationParsing() bool {
|
||||
if this.flags.configFile != "" {
|
||||
paths = append(paths, this.flags.configFile)
|
||||
}
|
||||
if this.Verb() {
|
||||
log.Println("(i) (30) have configuration files:")
|
||||
for _, paths := range paths {
|
||||
log.Println("(i) (30) -", paths)
|
||||
}
|
||||
}
|
||||
// parse every config and merge them all
|
||||
configs := make([]iniConfig, 0, len(paths))
|
||||
for _, path := range paths {
|
||||
|
25
util.go
25
util.go
@ -12,6 +12,19 @@ import "strings"
|
||||
import "context"
|
||||
import "sync/atomic"
|
||||
import "unicode/utf8"
|
||||
import "runtime/debug"
|
||||
|
||||
func panicErr(message any, stack []byte) (err error) {
|
||||
if panErr, ok := message.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(message))
|
||||
}
|
||||
if stack != nil {
|
||||
err = fmt.Errorf("%w: %s", err, stack)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func defaul[T comparable](value, def T) T {
|
||||
var zero T
|
||||
@ -22,11 +35,7 @@ func defaul[T comparable](value, def T) T {
|
||||
func panicWrap(f func() error) (err error) {
|
||||
defer func () {
|
||||
if pan := recover(); pan != nil {
|
||||
if panErr, ok := pan.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(pan))
|
||||
}
|
||||
err = panicErr(pan, debug.Stack())
|
||||
}
|
||||
} ()
|
||||
|
||||
@ -37,11 +46,7 @@ func panicWrap(f func() error) (err error) {
|
||||
func panicWrapCtx(ctx context.Context, f func(context.Context) error) (err error) {
|
||||
defer func () {
|
||||
if pan := recover(); pan != nil {
|
||||
if panErr, ok := pan.(error); ok {
|
||||
err = panErr
|
||||
} else {
|
||||
err = errors.New(fmt.Sprint(pan))
|
||||
}
|
||||
err = panicErr(pan, debug.Stack())
|
||||
}
|
||||
} ()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user