44package caddy
55
66import (
7- "crypto/sha256"
8- "encoding/binary"
97 "encoding/json"
108 "errors"
119 "fmt"
10+ "github.com/davecgh/go-spew/spew"
1211 "net/http"
1312 "path/filepath"
1413 "strconv"
@@ -53,7 +52,7 @@ func init() {
5352}
5453
5554type workerConfig struct {
56- // Name for the worker
55+ // Name for the worker. Default: the filename for FrankenPHPApp workers, filename + environment variables for FrankenPHPModule workers.
5756 Name string `json:"name,omitempty"`
5857 // FileName sets the path to the worker script.
5958 FileName string `json:"file_name,omitempty"`
@@ -63,8 +62,6 @@ type workerConfig struct {
6362 Env map [string ]string `json:"env,omitempty"`
6463 // Directories to watch for file changes
6564 Watch []string `json:"watch,omitempty"`
66- // ModuleID identifies which module created this worker
67- ModuleID uint64 `json:"module_id,omitempty"`
6865}
6966
7067type FrankenPHPApp struct {
@@ -119,20 +116,17 @@ func (f *FrankenPHPApp) Start() error {
119116 frankenphp .WithMaxWaitTime (f .MaxWaitTime ),
120117 }
121118 // Add workers from FrankenPHPApp configuration
122- for _ , w := range f .Workers {
123- opts = append (opts , frankenphp .WithWorkers (w .Name , repl .ReplaceKnown (w .FileName , "" ), w .Num , w .Env , w .Watch , w .ModuleID ))
124- }
125-
126- // Add workers from FrankenPHPModule configurations
127- for _ , w := range sharedState .Workers {
128- opts = append (opts , frankenphp .WithWorkers (w .Name , repl .ReplaceKnown (w .FileName , "" ), w .Num , w .Env , w .Watch , w .ModuleID ))
119+ for _ , w := range append (f .Workers , sharedState .Workers ... ) {
120+ opts = append (opts , frankenphp .WithWorkers (w .Name , repl .ReplaceKnown (w .FileName , "" ), w .Num , w .Env , w .Watch ))
129121 }
130122
131123 frankenphp .Shutdown ()
132124 if err := frankenphp .Init (opts ... ); err != nil {
133125 return err
134126 }
135127
128+ caddy .Log ().Warn (fmt .Sprintf ("FrankenPHPApp started with workers: %s" , spew .Sdump (append (f .Workers , sharedState .Workers ... ))))
129+
136130 return nil
137131}
138132
@@ -360,8 +354,6 @@ type FrankenPHPModule struct {
360354 ResolveRootSymlink * bool `json:"resolve_root_symlink,omitempty"`
361355 // Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
362356 Env map [string ]string `json:"env,omitempty"`
363- // ModuleID is the module ID that created this request.
364- ModuleID uint64 `json:"-"`
365357 // Workers configures the worker scripts to start.
366358 Workers []workerConfig `json:"workers,omitempty"`
367359
@@ -436,17 +428,6 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
436428 }
437429
438430 if len (f .Workers ) > 0 {
439- envString := ""
440- for k , v := range f .Env {
441- envString += k + "=" + v + ","
442- }
443- data := []byte (f .Root + envString )
444- hash := sha256 .Sum256 (data )
445- f .ModuleID = binary .LittleEndian .Uint64 (hash [:8 ])
446-
447- for i := range f .Workers {
448- f .Workers [i ].ModuleID = f .ModuleID
449- }
450431 sharedState .Workers = append (sharedState .Workers , f .Workers ... )
451432 }
452433
@@ -479,13 +460,20 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
479460 }
480461 }
481462
463+ workerNames := make ([]string , len (f .Workers ))
464+ for i , w := range f .Workers {
465+ workerNames [i ] = w .Name
466+ }
467+
468+ caddy .Log ().Info (fmt .Sprintf ("ServeHTTP module has workers: %s" , spew .Sdump (f .Workers )))
469+
482470 fr , err := frankenphp .NewRequestWithContext (
483471 r ,
484472 documentRootOption ,
485473 frankenphp .WithRequestSplitPath (f .SplitPath ),
486474 frankenphp .WithRequestPreparedEnv (env ),
487475 frankenphp .WithOriginalRequest (& origReq ),
488- frankenphp .WithModuleID ( f . ModuleID ),
476+ frankenphp .WithWorkerNames ( workerNames ),
489477 )
490478
491479 if err != nil {
@@ -637,11 +625,32 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
637625 }
638626 }
639627
628+ if wc .Name == "" {
629+ name , _ := fastabs .FastAbs (wc .FileName )
630+ if name == "" {
631+ name = wc .FileName
632+ }
633+ wc .Name = name
634+
635+ if len (wc .Env ) > 0 {
636+ envString := ""
637+ for k , v := range wc .Env {
638+ envString += k + "=" + v + ","
639+ }
640+ wc .Name += "#" + envString
641+ }
642+ }
643+ if ! strings .HasPrefix (wc .Name , "m#" ) {
644+ wc .Name = "m#" + wc .Name
645+ }
646+
640647 f .Workers = append (f .Workers , wc )
641648 }
642649 }
643650 }
644651
652+ caddy .Log ().Warn (fmt .Sprintf ("FrankenPHPModule UnmarshalCaddyfile with workers: %s" , spew .Sdump (f .Workers )))
653+
645654 return nil
646655}
647656
0 commit comments