Skip to content

Commit

Permalink
Merge pull request #37 from le-phare/cachetool-support-adapter-fastcgi
Browse files Browse the repository at this point in the history
feat(cachetool): add Cachetool support for fastcgi adapter
  • Loading branch information
pierreboissinot authored Jul 25, 2023
2 parents c7f80bc + fc5d1d4 commit 31b8327
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The defaults vars declared in this module:
lephare_assets_build_path: "../web/compiled/"
lephare_assets_web_path: "compiled/"

lephare_cachetool_adapter: web
lephare_cachetool_fastcgi: '127.0.0.1:9000'
lephare_cachetool_enable: true
lephare_cachetool_path: "{{ ansistrano_deploy_to }}/cachetool.phar"
lephare_cachetool_self_update: true
Expand Down Expand Up @@ -146,11 +148,12 @@ We use a docker image `lephare/ansible` to deploy our projects.

## Contribute

Clone or fork the repository and make your change in a branch.
Clone or fork the repository and make your change in a branch (below is an example for a "fastcgi" branch).

You can test your modification by build your own docker image. You need to edit the `docker/roles.yml` and adjust the repository url and branch name. Then you can build the image with.
You can test your modification by building your own docker image:

$ docker build -t lephare/ansible ./docker --no-cache
$ cd docker
$ docker build -f Dockerfile.dev .. -t lephare/ansible:fastcgi --no-cache

## License
MIT
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ lephare_assets_publish: true
lephare_assets_build_path: "../web/compiled/"
lephare_assets_web_path: "compiled/"

lephare_cachetool_adapter: web
lephare_cachetool_fastcgi: '127.0.0.1:9000' # only used if lephare_cachetool_adapter is fastcgi
lephare_cachetool_enable: true
lephare_cachetool_path: "{{ ansistrano_deploy_to }}/cachetool.phar"
lephare_cachetool_self_update: true
Expand Down
48 changes: 36 additions & 12 deletions docs/cachetool.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,39 @@ Generates a `.cachetool.yaml` to allow simpler command line usage.

## Variables

| Variable | Type | Default | Description |
| ------------------------------- | ------- | ---------------------------------------------- | -------------------------------- |
| lephare_cachetool_enable | boolean | `true` | Enable cachetool |
| lephare_cachetool_path | string | `"{{ ansistrano_deploy_to }}/cachetool.phar"` | Path of the cachetool executable |
| lephare_cachetool_self_update | boolean | `true` | Allows cachetool to self-update |
| lephare_cachetool_stat_clear | boolean | `true` | Clear filesystem stats |
| lephare_cachetool_opcache_reset | boolean | `true` | Clear opcache |
| lephare_cachetool_apcu_clear | boolean | `true` | Clear APCu cache |
| lephare_cachetool_scheme | string | `https` | Use HTTP or HTTPS |
| lephare_cachetool_options | string | `""` | Arbitrary option to use |
| lephare_cachetool_retries | integer | `10` | Number of retries |
| lephare_cachetool_delay | integer | `15` | Delay between retries |
| Variable | Type | Default | Description |
|---------------------------------|---------|-----------------------------------------------|----------------------------------|
| lephare_cachetool_adapter | string | `web` | web or fastcgi or cli |
| lephare_cachetool_fastcgi | string | `127.0.0.1:9000` | only if adatper is fastcgi |
| lephare_cachetool_enable | boolean | `true` | Enable cachetool |
| lephare_cachetool_path | string | `"{{ ansistrano_deploy_to }}/cachetool.phar"` | Path of the cachetool executable |
| lephare_cachetool_self_update | boolean | `true` | Allows cachetool to self-update |
| lephare_cachetool_stat_clear | boolean | `true` | Clear filesystem stats |
| lephare_cachetool_opcache_reset | boolean | `true` | Clear opcache |
| lephare_cachetool_apcu_clear | boolean | `true` | Clear APCu cache |
| lephare_cachetool_scheme | string | `https` | Use HTTP or HTTPS |
| lephare_cachetool_options | string | `""` | Arbitrary option to use |
| lephare_cachetool_retries | integer | `10` | Number of retries |
| lephare_cachetool_delay | integer | `15` | Delay between retries |


## Tips

### Speed up cache invalidation

The default cache adapter is `web`. With this configuration, cachetool create a temporary PHP script file and calls it
through the web interface.

The process goes through the full stack: name resolution, TLS, web server, FPM etc . depending on `lephare_cachetool_scheme`.

This call may fail several times until it reaches the `lephare_cachetool_retries` (default 10) limit,
with each retry occurring after `lephare_cachetool_delay` seconds (default 15), which may lead to a 404 error.

By choosing `fastcgi` adapter, cachetool interacts directly with FPM server through the FastCGI protocol.
Unless the PHP setting `opcache.memory_consumption` is insufficient for your codebase, you won't encounter
failure, retries or delays.

**NB**: this adapter is also useful when you have more than 1 server for an app.
For example, if you have two servers `foo` and `bar` behind `https://www.acme.com` and you use `web` adapter,
cachetool will only clear caches on one server behind your load balancer depending on its strategy.
However, using the fastcgi adapter ensures that caches are cleared on both `foo` and `bar` servers.
7 changes: 6 additions & 1 deletion templates/cachetool/cachetool.yaml.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
adapter: web
adapter: {{ lephare_cachetool_adapter }}
{% if 'fastcgi' == lephare_cachetool_adapter %}
fastcgi: {{ lephare_cachetool_fastcgi }}
{% endif %}
{% if 'web' == lephare_cachetool_adapter %}
{% if cachetool_version.stdout is version('7.1', '>=') %}
webClient: SymfonyHttpClient
{% else %}
Expand All @@ -9,3 +13,4 @@ webPath: "{{ lephare_document_root_path }}"
{% if lephare_http_basic_secure %}
webBasicAuth: "{{ lephare_http_basic_user }}:{{ lephare_http_basic_password }}"
{% endif %}
{% endif %}

0 comments on commit 31b8327

Please sign in to comment.