Compare commits
3 Commits
772e9ca290
...
641624ef3b
Author | SHA1 | Date | |
---|---|---|---|
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 {
|
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()
|
err := this.server.ListenAndServe()
|
||||||
if errors.Is(err, http.ErrServerClosed) { return nil }
|
if errors.Is(err, http.ErrServerClosed) { return nil }
|
||||||
return err
|
return err
|
||||||
|
13
phases.go
13
phases.go
@ -255,6 +255,19 @@ func (this *environment) phase70_5Trimming() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *environment) phase80Shutdown() 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)
|
cause := context.Cause(this.ctx)
|
||||||
if cause != nil {
|
if cause != nil {
|
||||||
log.Println("XXX (80) shutting down because:", cause)
|
log.Println("XXX (80) shutting down because:", cause)
|
||||||
|
Loading…
Reference in New Issue
Block a user