Coupla bug fixes

This commit is contained in:
Matthew Holt 2025-03-28 12:38:01 -06:00
parent 6825db3906
commit 08bf9e4fc0
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
2 changed files with 21 additions and 11 deletions
context.go
modules/caddyevents

@ -274,14 +274,6 @@ func (ctx Context) LoadModule(structPointer any, fieldName string) (any, error)
// we're done with the raw bytes; allow GC to deallocate
val.Set(reflect.Zero(typ))
// if the loaded module happens to be an app that can emit events, store it so the
// core can have access to emit events without an import cycle
if ee, ok := result.(eventEmitter); ok {
if _, ok := ee.(App); ok {
ctx.cfg.eventEmitter = ee
}
}
return result, nil
}
@ -445,6 +437,15 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
ctx.moduleInstances[id] = append(ctx.moduleInstances[id], val)
// if the loaded module happens to be an app that can emit events, store it so the
// core can have access to emit events without an import cycle
if ee, ok := val.(eventEmitter); ok {
if _, ok := ee.(App); ok {
log.Println("GOT EE:", ee)
ctx.cfg.eventEmitter = ee
}
}
return val, nil
}

@ -212,9 +212,18 @@ func (app *App) Emit(ctx caddy.Context, eventName string, data map[string]any) c
logger.Error("failed to create event", zap.Error(err))
}
var originModule caddy.ModuleInfo
var originModuleID caddy.ModuleID
var originModuleName string
if origin := e.Origin(); origin != nil {
originModule = origin.CaddyModule()
originModuleID = originModule.ID
originModuleName = originModule.String()
}
logger = logger.With(
zap.String("id", e.ID().String()),
zap.String("origin", e.Origin().CaddyModule().String()))
zap.String("origin", originModuleName))
// add event info to replacer, make sure it's in the context
repl, ok := ctx.Context.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
@ -235,7 +244,7 @@ func (app *App) Emit(ctx caddy.Context, eventName string, data map[string]any) c
case "event.time_unix":
return e.Timestamp().UnixMilli(), true
case "event.module":
return e.Origin().CaddyModule().ID, true
return originModuleID, true
case "event.data":
return e.Data, true
}
@ -257,7 +266,7 @@ func (app *App) Emit(ctx caddy.Context, eventName string, data map[string]any) c
// invoke handlers bound to the event by name and also all events; this for loop
// iterates twice at most: once for the event name, once for "" (all events)
for {
moduleID := e.Origin().CaddyModule().ID
moduleID := originModuleID
// implement propagation up the module tree (i.e. start with "a.b.c" then "a.b" then "a" then "")
for {