mirror of
https://github.com/dunglas/frankenphp.git
synced 2025-12-24 13:38:11 +08:00
feat: add build tag to skip Watcher support (#1076)
* feat: add build tag to skip Watcher support * fix * fix * cleanup
This commit is contained in:
2
.github/workflows/docker.yaml
vendored
2
.github/workflows/docker.yaml
vendored
@@ -213,7 +213,7 @@ jobs:
|
||||
run: |
|
||||
docker run --platform=${{ matrix.platform }} --rm \
|
||||
"$(jq -r '."builder-${{ matrix.variant }}"."containerimage.config.digest"' <<< "${METADATA}")" \
|
||||
sh -c 'go test ${{ matrix.race }} -v ./... && cd caddy && go test ${{ matrix.race }} -v ./...'
|
||||
sh -c 'go test -tags watcher ${{ matrix.race }} -v ./... && cd caddy && go test -tags watcher ${{ matrix.race }} -v ./...'
|
||||
env:
|
||||
METADATA: ${{ steps.build.outputs.metadata }}
|
||||
# Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
|
||||
|
||||
4
.github/workflows/tests.yaml
vendored
4
.github/workflows/tests.yaml
vendored
@@ -71,11 +71,11 @@ jobs:
|
||||
run: go build
|
||||
-
|
||||
name: Run library tests
|
||||
run: go test -race -v ./...
|
||||
run: go test -tags watcher -race -v ./...
|
||||
-
|
||||
name: Run Caddy module tests
|
||||
working-directory: caddy/
|
||||
run: go test -race -v ./...
|
||||
run: go test -tags watcher -race -v ./...
|
||||
-
|
||||
name: Build the server
|
||||
working-directory: caddy/frankenphp/
|
||||
|
||||
@@ -29,7 +29,7 @@ If docker version is lower than 23.0, build is failed by dockerignore [pattern i
|
||||
## Running the test suite
|
||||
|
||||
```console
|
||||
go test -race -v ./...
|
||||
go test -tags watcher -race -v ./...
|
||||
```
|
||||
|
||||
## Caddy module
|
||||
@@ -171,7 +171,7 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
|
||||
8. In the container, you can use GDB and the like:
|
||||
|
||||
```console
|
||||
go test -c -ldflags=-w
|
||||
go test -tags watcher -c -ldflags=-w
|
||||
gdb --args ./frankenphp.test -test.run ^MyTest$
|
||||
```
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS
|
||||
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS"
|
||||
|
||||
WORKDIR /go/src/app/caddy/frankenphp
|
||||
RUN GOBIN=/usr/local/bin go install -tags 'brotli' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
|
||||
RUN GOBIN=/usr/local/bin go install -tags 'brotli watcher' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
|
||||
setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \
|
||||
cp Caddyfile /etc/caddy/Caddyfile && \
|
||||
frankenphp version
|
||||
|
||||
@@ -122,7 +122,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS
|
||||
ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS"
|
||||
|
||||
WORKDIR /go/src/app/caddy/frankenphp
|
||||
RUN GOBIN=/usr/local/bin go install -tags 'brotli' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
|
||||
RUN GOBIN=/usr/local/bin go install -tags 'brotli watcher' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \
|
||||
setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \
|
||||
upx --best /usr/local/bin/frankenphp && \
|
||||
frankenphp version
|
||||
|
||||
@@ -249,7 +249,7 @@ fi
|
||||
|
||||
cd caddy/frankenphp/
|
||||
go env
|
||||
go build -buildmode=pie -tags "cgo netgo osusergo static_build brotli" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
|
||||
go build -buildmode=pie -tags "cgo netgo osusergo static_build brotli watcher" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
|
||||
cd ../..
|
||||
|
||||
if [ -d "${EMBED}" ]; then
|
||||
|
||||
@@ -3,15 +3,16 @@ package caddy_test
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/dunglas/frankenphp"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/dunglas/frankenphp"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddytest"
|
||||
)
|
||||
|
||||
@@ -574,31 +575,3 @@ func TestAutoWorkerConfig(t *testing.T) {
|
||||
"frankenphp_testdata_index_php_ready_workers",
|
||||
))
|
||||
}
|
||||
|
||||
func TestWorkerWithInactiveWatcher(t *testing.T) {
|
||||
tester := caddytest.NewTester(t)
|
||||
tester.InitServer(`
|
||||
{
|
||||
skip_install_trust
|
||||
admin localhost:2999
|
||||
http_port 9080
|
||||
|
||||
frankenphp {
|
||||
worker {
|
||||
file ../testdata/worker-with-watcher.php
|
||||
num 1
|
||||
watch ./**/*.php
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
localhost:9080 {
|
||||
root ../testdata
|
||||
rewrite worker-with-watcher.php
|
||||
php
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "requests:1")
|
||||
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "requests:2")
|
||||
}
|
||||
|
||||
38
caddy/watcher_test.go
Normal file
38
caddy/watcher_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
//go:build watcher
|
||||
|
||||
package caddy_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddytest"
|
||||
)
|
||||
|
||||
func TestWorkerWithInactiveWatcher(t *testing.T) {
|
||||
tester := caddytest.NewTester(t)
|
||||
tester.InitServer(`
|
||||
{
|
||||
skip_install_trust
|
||||
admin localhost:2999
|
||||
http_port 9080
|
||||
|
||||
frankenphp {
|
||||
worker {
|
||||
file ../testdata/worker-with-watcher.php
|
||||
num 1
|
||||
watch ./**/*.php
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
localhost:9080 {
|
||||
root ../testdata
|
||||
rewrite worker-with-watcher.php
|
||||
php
|
||||
}
|
||||
`, "caddyfile")
|
||||
|
||||
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "requests:1")
|
||||
tester.AssertGetResponse("http://localhost:9080", http.StatusOK, "requests:2")
|
||||
}
|
||||
@@ -78,7 +78,7 @@ WORKDIR /go/src/app
|
||||
COPY . .
|
||||
|
||||
WORKDIR /go/src/app/caddy/frankenphp
|
||||
RUN go build -buildvcs=false -tags 'brotli'
|
||||
RUN go build -buildvcs=false -tags 'brotli watcher'
|
||||
|
||||
WORKDIR /go/src/app
|
||||
CMD [ "zsh" ]
|
||||
|
||||
@@ -29,7 +29,7 @@ docker run --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -p 8080:8080 -
|
||||
## 运行测试套件
|
||||
|
||||
```console
|
||||
go test -race -v ./...
|
||||
go test -tags watcher -race -v ./...
|
||||
```
|
||||
|
||||
## Caddy 模块
|
||||
@@ -171,7 +171,7 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
|
||||
8. 在容器中,可以使用 GDB 和以下:
|
||||
|
||||
```console
|
||||
go test -c -ldflags=-w
|
||||
go test -tags watcher -c -ldflags=-w
|
||||
gdb --args ./frankenphp.test -test.run ^MyTest$
|
||||
```
|
||||
|
||||
|
||||
@@ -101,18 +101,19 @@ xcaddy build \
|
||||
Additional features can be enabled if the required C libraries are installed by
|
||||
passing additional build tags to the Go compiler:
|
||||
|
||||
| Tag | Dependencies | Description |
|
||||
|--------|--------------------------------------------|--------------------------------------------------|
|
||||
| brotli | [Brotli](https://github.com/google/brotli) | Add a Caddy module supporting Brotli compression |
|
||||
| Tag | Dependencies | Description |
|
||||
|---------|----------------------------------------------|--------------------------------|
|
||||
| brotli | [Brotli](https://github.com/google/brotli) | Brotli compression |
|
||||
| watcher | [Watcher](https://github.com/e-dant/watcher) | Restart workers on file change |
|
||||
|
||||
When using `go build` directly, pass the additional `-tags` option followed by the comma-separated list of tags:
|
||||
|
||||
```console
|
||||
go build -tags brotli
|
||||
go build -tags 'brotli watcher'
|
||||
```
|
||||
|
||||
When using `xcaddy`, set the `-tags` option in the `XCADDY_GO_BUILD_FLAGS` environment variable:
|
||||
|
||||
```console
|
||||
XCADDY_GO_BUILD_FLAGS="-tags brotli"
|
||||
XCADDY_GO_BUILD_FLAGS="-tags 'brotli watcher'"
|
||||
```
|
||||
|
||||
@@ -29,7 +29,7 @@ Si la version de Docker est inférieure à 23.0, la construction échoue à caus
|
||||
## Exécution de la suite de tests
|
||||
|
||||
```console
|
||||
go test -race -v ./...
|
||||
go test -tags watcher -race -v ./...
|
||||
```
|
||||
|
||||
## Module Caddy
|
||||
@@ -145,7 +145,7 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
|
||||
8. Dans le conteneur, vous pouvez utiliser GDB et similaires :
|
||||
|
||||
```console
|
||||
go test -c -ldflags=-w
|
||||
go test -tags watcher -c -ldflags=-w
|
||||
gdb --args ./frankenphp.test -test.run ^MyTest$
|
||||
```
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ Docker sürümü 23.0'dan düşükse, derleme dockerignore [pattern issue](https
|
||||
## Test senaryolarını çalıştırma
|
||||
|
||||
```console
|
||||
go test -race -v ./...
|
||||
go test -tags watcher -race -v ./...
|
||||
```
|
||||
|
||||
## Caddy modülü
|
||||
@@ -171,7 +171,7 @@ docker buildx bake -f docker-bake.hcl --pull --no-cache --push
|
||||
8. Konteynerde GDB ve benzerlerini kullanabilirsiniz:
|
||||
|
||||
```console
|
||||
go test -c -ldflags=-w
|
||||
go test -tags watcher -c -ldflags=-w
|
||||
gdb --args ./frankenphp.test -test.run ^MyTest$
|
||||
```
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
//go:build watcher
|
||||
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type watchPattern struct {
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
//go:build watcher
|
||||
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDisallowOnEventTypeBiggerThan3(t *testing.T) {
|
||||
|
||||
14
internal/watcher/watcher-skip.go
Normal file
14
internal/watcher/watcher-skip.go
Normal file
@@ -0,0 +1,14 @@
|
||||
//go:build !watcher
|
||||
|
||||
package watcher
|
||||
|
||||
import "go.uber.org/zap"
|
||||
|
||||
func InitWatcher(filePatterns []string, callback func(), zapLogger *zap.Logger) error {
|
||||
zapLogger.Error("watcher support is not enabled")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DrainWatcher() {
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
// clang-format off
|
||||
//go:build watcher
|
||||
// clang-format on
|
||||
#include "_cgo_export.h"
|
||||
#include "watcher-c.h"
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build watcher
|
||||
|
||||
package watcher
|
||||
|
||||
// #cgo LDFLAGS: -lwatcher -lstdc++
|
||||
@@ -8,11 +10,12 @@ package watcher
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"go.uber.org/zap"
|
||||
"runtime/cgo"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type watcher struct {
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
//go:build watcher
|
||||
|
||||
package frankenphp_test
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// we have to wait a few milliseconds for the watcher debounce to take effect
|
||||
@@ -22,10 +24,6 @@ const minTimesToPollForChanges = 3
|
||||
const maxTimesToPollForChanges = 60
|
||||
|
||||
func TestWorkersShouldReloadOnMatchingPattern(t *testing.T) {
|
||||
if isRunningInMsanMode() {
|
||||
t.Skip("Skipping watcher tests in memory sanitizer mode")
|
||||
return
|
||||
}
|
||||
watch := []string{"./testdata/**/*.txt"}
|
||||
|
||||
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
|
||||
@@ -35,11 +33,6 @@ func TestWorkersShouldReloadOnMatchingPattern(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWorkersShouldNotReloadOnExcludingPattern(t *testing.T) {
|
||||
if isRunningInMsanMode() {
|
||||
t.Skip("Skipping watcher tests in memory sanitizer mode")
|
||||
|
||||
return
|
||||
}
|
||||
watch := []string{"./testdata/**/*.php"}
|
||||
|
||||
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
|
||||
@@ -58,11 +51,6 @@ func fetchBody(method string, url string, handler func(http.ResponseWriter, *htt
|
||||
return string(body)
|
||||
}
|
||||
|
||||
func isRunningInMsanMode() bool {
|
||||
cflags := os.Getenv("CFLAGS")
|
||||
return strings.Contains(cflags, "-fsanitize=memory")
|
||||
}
|
||||
|
||||
func pollForWorkerReset(t *testing.T, handler func(http.ResponseWriter, *http.Request), limit int) bool {
|
||||
// first we make an initial request to start the request counter
|
||||
body := fetchBody("GET", "http://example.com/worker-with-watcher.php", handler)
|
||||
@@ -225,11 +225,14 @@ func restartWorkersOnFileChanges(workerOpts []workerOpt) error {
|
||||
for _, w := range workerOpts {
|
||||
directoriesToWatch = append(directoriesToWatch, w.watch...)
|
||||
}
|
||||
if len(directoriesToWatch) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
restartWorkers := func() {
|
||||
restartWorkers(workerOpts)
|
||||
}
|
||||
if err := watcher.InitWatcher(directoriesToWatch, restartWorkers, getLogger()); err != nil {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user