11 Commits

8 changed files with 82 additions and 18 deletions

View File

@@ -3,20 +3,40 @@ package camfish
import "context"
// Actor is a participant in the environment. All public methods on an actor
// must be safe for concurrent use by multiple goroutines. Additionally, any
// type which explicitly implements Actor should:
// should be safe for concurrent use by multiple goroutines except for AddFlags,
// Init, Configure, and ProcessConfig. Additionally, any type which explicitly
// implements Actor should:
//
// - Treat all public fields, values, indices, etc. as immutable
// - Satisfy Actor as a pointer, not a value
// - Not have a constructor
//
// The CAMFISH environment will use interfaces in this package to probe actors
// for methods. If an actor is supposed to fulfill one of these interfaces, this
// should be enforced at compile-time by assigning the actor to an anonymous
// global variable of that interface type. For instance, this line will ensure
// that SomeActor fulfills [Resettable]:
//
// var _ camfish.Resettable = new(SomeActor)
type Actor interface {
// Type returns the type name of the actor. The value returned from this
// is used to locate actors capable of performing a specific task, so it
// absolutely must return the same string every time. Actors implemented
// in packages besides this one (i.e. not camfish) must not return the
// string "cron".
// Type returns the "type name" of the actor. The value returned from
// this is used to locate actors capable of performing a specific task,
// so it absolutely must return the same string every time. It is
// usually best to have this be unique to each actor. Actors implemented
// in packages other than this one
// (git.tebibyte.media/sashakoshka/camfish) must not return the string
// "cron".
Type() string
}
// Named is any object with a name.
type Named interface {
// Name returns the name. This doesn't need to be the same as Type. It
// must return the same string every time. It is used to differentiate
// actors of the same type in logs.
Name() string
}
// FlagAdder is any object that can add [Flag]s to a [FlagSet]. Actors which
// implement this interface will be called upon to add flags during and only
// during the flag parsing phase.

View File

@@ -298,7 +298,7 @@ func (this *environment) runRunnable(ctx context.Context, actor Runnable) (stopE
return
} else {
// failure
log.Printf("XXX [%s] failed", typ)
log.Printf("XXX [%s] failed: %v", typ, err)
}
// restart logic

27
examples/broken/main.go Normal file
View 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
}

View File

@@ -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

4
ini.go
View File

@@ -162,7 +162,7 @@ func configFiles(program string) ([]string, error) {
userConfig, err := os.UserConfigDir()
if err != nil { return nil, err }
return []string {
filepath.Join("/etc", program),
filepath.Join(userConfig, program),
filepath.Join("/etc", program, program + ".conf"),
filepath.Join(userConfig, program, program + ".conf"),
}, nil
}

View File

@@ -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)

View File

@@ -77,7 +77,11 @@ func logActors (actors iter.Seq[Actor]) {
}
types := make(map[string] int)
for actor := range actors {
types[actor.Type()] += 1
typ := actor.Type()
if named, ok := actor.(Named); ok {
typ = fmt.Sprintf("%s/%s", typ, named.Name())
}
types[typ] += 1
}
for typ, count := range types {
if count > 1 {

View File

@@ -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)