Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add build tag to skip Watcher support #1076

Merged
merged 4 commits into from
Oct 8, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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$
```

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
echo '<?php phpinfo();' > /app/public/index.php

COPY --link caddy/frankenphp/Caddyfile /etc/caddy/Caddyfile
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-bookworm, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 27 in Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-bookworm, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK CMD curl -f http://localhost:2019/metrics || exit 1
Expand Down Expand Up @@ -101,7 +101,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
echo '<?php phpinfo();' > /app/public/index.php

COPY --link caddy/frankenphp/Caddyfile /etc/caddy/Caddyfile
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-2-24-alpine, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/386)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/386" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/arm/v7)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm/v7" for current build

Check warning on line 26 in alpine.Dockerfile

View workflow job for this annotation

GitHub Actions / build (php-8-3-12-alpine, linux/arm64)

Base image platform does not match expected target platform

InvalidBaseImagePlatform: Base image mlocati/php-extension-installer was pulled with platform "linux/amd64", expected "linux/arm64" for current build

CMD ["--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
HEALTHCHECK CMD curl -f http://localhost:2019/metrics || exit 1
Expand Down Expand Up @@ -122,7 +122,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 5 additions & 32 deletions caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 changes: 38 additions & 0 deletions caddy/watcher_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
4 changes: 2 additions & 2 deletions docs/cn/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 模块
Expand Down Expand Up @@ -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$
```

Expand Down
11 changes: 6 additions & 5 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
```
4 changes: 2 additions & 2 deletions docs/fr/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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$
```

Expand Down
4 changes: 2 additions & 2 deletions docs/tr/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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ü
Expand Down Expand Up @@ -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$
```

Expand Down
5 changes: 4 additions & 1 deletion internal/watcher/watch_pattern.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//go:build watcher

package watcher

import (
"go.uber.org/zap"
"path/filepath"
"strings"

"go.uber.org/zap"
)

type watchPattern struct {
Expand Down
5 changes: 4 additions & 1 deletion internal/watcher/watch_pattern_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions internal/watcher/watcher-skip.go
Original file line number Diff line number Diff line change
@@ -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() {
}
3 changes: 3 additions & 0 deletions internal/watcher/watcher.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// clang-format off
//go:build watcher
// clang-format on
#include "_cgo_export.h"
#include "watcher-c.h"

Expand Down
5 changes: 4 additions & 1 deletion internal/watcher/watcher.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build watcher

package watcher

// #cgo LDFLAGS: -lwatcher -lstdc++
Expand All @@ -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 {
Expand Down
20 changes: 4 additions & 16 deletions frankenphp_with_watcher_test.go → watcher_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading