Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,35 @@ func TestModuleWorkerInheritsEnv(t *testing.T) {
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=inherit_this")
}

func TestWorkerRetriesBootFailureByDefault(t *testing.T) {
marker := filepath.Join(t.TempDir(), "booted")
tester := caddytest.NewTester(t)
initServer(t, tester, `
{
skip_install_trust
admin localhost:2999

frankenphp {
worker {
file ../testdata/worker-fail-first-boot.php
num 1
env BOOT_MARKER `+marker+`
}
}
}

http://localhost:`+testPort+` {
route {
php {
root ../testdata
}
}
}
`, "caddyfile")

tester.AssertGetResponse("http://localhost:"+testPort+"/worker-fail-first-boot.php", http.StatusOK, "booted after a failure")
}

func TestNamedModuleWorkers(t *testing.T) {
var wg sync.WaitGroup
testPortNum, _ := strconv.Atoi(testPort)
Expand Down
9 changes: 6 additions & 3 deletions caddy/workerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type workerConfig struct {
// The path to match against the worker
MatchPath []string `json:"match_path,omitempty"`
// MaxConsecutiveFailures sets the maximum number of consecutive failures before panicking (defaults to 6, set to -1 to never panick)
MaxConsecutiveFailures int `json:"max_consecutive_failures,omitempty"`
MaxConsecutiveFailures *int `json:"max_consecutive_failures,omitempty"`

options []frankenphp.WorkerOption
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func unmarshalWorker(d *caddyfile.Dispenser) (workerConfig, error) {
return wc, d.Errf("max_consecutive_failures must be >= -1")
}

wc.MaxConsecutiveFailures = v
wc.MaxConsecutiveFailures = new(v)
default:
return wc, wrongSubDirectiveError("worker", "name, file, num, env, watch, match, max_consecutive_failures, max_threads", v)
}
Expand All @@ -159,10 +159,13 @@ func (wc *workerConfig) toWorkerOptions() ([]frankenphp.WorkerOption, error) {
opts := []frankenphp.WorkerOption{
frankenphp.WithWorkerEnv(wc.Env),
frankenphp.WithWorkerWatchMode(wc.Watch),
frankenphp.WithWorkerMaxFailures(wc.MaxConsecutiveFailures),
frankenphp.WithWorkerMaxThreads(wc.MaxThreads),
}

if wc.MaxConsecutiveFailures != nil {
opts = append(opts, frankenphp.WithWorkerMaxFailures(*wc.MaxConsecutiveFailures))
}

// options collected while provisioning the module, e.g. the Mercure hub
opts = append(opts, wc.options...)

Expand Down
14 changes: 14 additions & 0 deletions testdata/worker-fail-first-boot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// fails the first boot only, before reaching frankenphp_handle_request()
$marker = $_SERVER['BOOT_MARKER'];
if (!file_exists($marker)) {
touch($marker);
exit(1);
}

do {
$ok = frankenphp_handle_request(function (): void {
echo 'booted after a failure';
});
} while ($ok);
Loading