36 lines
866 B
Go
36 lines
866 B
Go
// 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")
|
|
}
|
|
}
|
|
}
|