Compare commits
4 Commits
772e9ca290
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| bfca8c6f4a | |||
| 641624ef3b | |||
| bae737e6b8 | |||
| 0077a5d115 |
27
examples/broken/main.go
Normal file
27
examples/broken/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Example broken demonstrates how the environment will forcibly kill the
|
||||
// program if an actor cannot shut down.
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "log"
|
||||
import "context"
|
||||
import "git.tebibyte.media/sashakoshka/camfish"
|
||||
|
||||
func main() {
|
||||
camfish.Run("broken",
|
||||
"Example broken demonstrates how the environment will " +
|
||||
"forcibly kill the program if an actor cannot shut down",
|
||||
new(broken))
|
||||
}
|
||||
|
||||
// broken is an incorrectly implemented actor that cannot shut down.
|
||||
type broken struct { }
|
||||
var _ camfish.Runnable = new(broken)
|
||||
func (this *broken) Type() string { return "broken" }
|
||||
|
||||
func (this *broken) Run(ctx context.Context) error {
|
||||
log.Println("(i) [broken] wait for approximately 8 minutes")
|
||||
log.Printf("(i) [broken] if impatient, run: kill -9 %d", os.Getpid())
|
||||
<- (chan struct { })(nil)
|
||||
return ctx.Err() // unreachable, of course
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func (this *httpServer) Init(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (this *httpServer) Run() error {
|
||||
log.Printf("[http] listening on %s", this.server.Addr)
|
||||
log.Printf("(i) [http-server] listening on %s", this.server.Addr)
|
||||
err := this.server.ListenAndServe()
|
||||
if errors.Is(err, http.ErrServerClosed) { return nil }
|
||||
return err
|
||||
|
||||
13
phases.go
13
phases.go
@@ -255,6 +255,19 @@ func (this *environment) phase70_5Trimming() bool {
|
||||
}
|
||||
|
||||
func (this *environment) phase80Shutdown() bool {
|
||||
ctx, done := context.WithTimeout(
|
||||
context.Background(),
|
||||
defaul(this.timing.shutdownTimeout.Load(), defaultShutdownTimeout))
|
||||
defer done()
|
||||
go func() {
|
||||
<- ctx.Done()
|
||||
if errors.Is(context.Cause(ctx), context.DeadlineExceeded) {
|
||||
log.Println("XXX (80) shutdown timeout expired, performing emergency halt")
|
||||
log.Printf("====== [%s] END =======", this.name)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
cause := context.Cause(this.ctx)
|
||||
if cause != nil {
|
||||
log.Println("XXX (80) shutting down because:", cause)
|
||||
|
||||
12
util_test.go
12
util_test.go
@@ -17,17 +17,17 @@ func TestDefaul(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestPanicWrap(test *testing.T) {
|
||||
err := panicWrap(func (ctx context.Context) error {
|
||||
err := panicWrap(func () error {
|
||||
return errors.New("test case 0")
|
||||
})
|
||||
test.Log(err)
|
||||
if err.Error() != "test case 0" { test.Fatal("not equal") }
|
||||
err = panicWrap(func (ctx context.Context) error {
|
||||
err = panicWrap(func () error {
|
||||
panic(errors.New("test case 1"))
|
||||
})
|
||||
test.Log(err)
|
||||
if err.Error() != "test case 1" { test.Fatal("not equal") }
|
||||
err = panicWrap( func (ctx context.Context) error {
|
||||
err = panicWrap( func () error {
|
||||
return nil
|
||||
})
|
||||
test.Log(err)
|
||||
@@ -35,17 +35,17 @@ func TestPanicWrap(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestPanicWrapCtx(test *testing.T) {
|
||||
err := panicWrap(context.Background(), func (ctx context.Context) error {
|
||||
err := panicWrapCtx(context.Background(), func (ctx context.Context) error {
|
||||
return errors.New("test case 0")
|
||||
})
|
||||
test.Log(err)
|
||||
if err.Error() != "test case 0" { test.Fatal("not equal") }
|
||||
err = panicWrap(context.Background(), func (ctx context.Context) error {
|
||||
err = panicWrapCtx(context.Background(), func (ctx context.Context) error {
|
||||
panic(errors.New("test case 1"))
|
||||
})
|
||||
test.Log(err)
|
||||
if err.Error() != "test case 1" { test.Fatal("not equal") }
|
||||
err = panicWrap(context.Background(), func (ctx context.Context) error {
|
||||
err = panicWrapCtx(context.Background(), func (ctx context.Context) error {
|
||||
return nil
|
||||
})
|
||||
test.Log(err)
|
||||
|
||||
Reference in New Issue
Block a user