@@ -5,7 +5,7 @@ package caddy
55
66import (
77 "crypto/sha256"
8- "encoding/hex "
8+ "encoding/binary "
99 "encoding/json"
1010 "errors"
1111 "fmt"
@@ -32,8 +32,11 @@ const defaultDocumentRoot = "public"
3232
3333var iniError = errors .New ("'php_ini' must be in the format: php_ini \" <key>\" \" <value>\" " )
3434
35- // moduleWorkers is a package-level variable to store workers that can be accessed by both FrankenPHPModule and FrankenPHPApp
36- var moduleWorkers []workerConfig
35+ // sharedState is a package-level variable to store information that can be accessed by both FrankenPHPModule and FrankenPHPApp
36+ var sharedState struct {
37+ ModuleIDs []uint64
38+ Workers []workerConfig
39+ }
3740
3841func init () {
3942 caddy .RegisterModule (FrankenPHPApp {})
@@ -61,7 +64,7 @@ type workerConfig struct {
6164 // Directories to watch for file changes
6265 Watch []string `json:"watch,omitempty"`
6366 // ModuleID identifies which module created this worker
64- ModuleID string `json:"module_id,omitempty"`
67+ ModuleID uint64 `json:"module_id,omitempty"`
6568}
6669
6770type FrankenPHPApp struct {
@@ -121,7 +124,7 @@ func (f *FrankenPHPApp) Start() error {
121124 }
122125
123126 // Add workers from FrankenPHPModule configurations
124- for _ , w := range moduleWorkers {
127+ for _ , w := range sharedState . Workers {
125128 opts = append (opts , frankenphp .WithWorkers (w .Name , repl .ReplaceKnown (w .FileName , "" ), w .Num , w .Env , w .Watch , w .ModuleID ))
126129 }
127130
@@ -143,13 +146,13 @@ func (f *FrankenPHPApp) Stop() error {
143146 frankenphp .DrainWorkers ()
144147 }
145148
146- // reset configuration so it doesn't bleed into later tests
149+ // reset the configuration so it doesn't bleed into later tests
147150 f .Workers = nil
148151 f .NumThreads = 0
149152 f .MaxWaitTime = 0
150153
151154 // reset moduleWorkers
152- moduleWorkers = nil
155+ sharedState . Workers = nil
153156
154157 return nil
155158}
@@ -358,7 +361,7 @@ type FrankenPHPModule struct {
358361 // Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
359362 Env map [string ]string `json:"env,omitempty"`
360363 // ModuleID is the module ID that created this request.
361- ModuleID string `json:"-"`
364+ ModuleID uint64 `json:"-"`
362365 // Workers configures the worker scripts to start.
363366 Workers []workerConfig `json:"workers,omitempty"`
364367
@@ -432,15 +435,20 @@ func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
432435 }
433436 }
434437
435- data := []byte (f .Root + strings .Join (f .SplitPath , "," ) + time .Now ().String ())
436- hash := sha256 .Sum256 (data )
437- f .ModuleID = hex .EncodeToString (hash [:8 ])
438-
439438 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+
440447 for _ , w := range f .Workers {
441448 w .ModuleID = f .ModuleID
442449 }
443- moduleWorkers = append (moduleWorkers , f .Workers ... )
450+ sharedState .Workers = append (sharedState .Workers , f .Workers ... )
451+ f .logger .Warn (fmt .Sprintf ("Initialized FrankenPHP module %d with %d workers" , f .ModuleID , len (f .Workers )))
444452 }
445453
446454 return nil
0 commit comments