Skip to content

Commit e30ff5b

Browse files
authored
feat(webhooks): add emsco_webhook, emsch_webhook_event, ems_clear_http_caches twig functions (#1477)
1 parent ac26072 commit e30ff5b

10 files changed

Lines changed: 192 additions & 18 deletions

File tree

.vitepress/nav/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const sidebar: DefaultTheme.SidebarMulti = {
4646
collapsed: true,
4747
items: [
4848
{ text: 'Login', link: '/elasticms-admin/api/login' },
49+
{ text: 'Monitoring', link: '/elasticms-admin/api/monitoring' },
4950
{ text: 'Webhook', link: '/elasticms-admin/api/webhook' },
5051
]
5152
},

elasticms-admin/api/monitoring.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/dev/client-helper-bundle/Twig/functions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ The `redirects/favicon.json.twig` template:
8787
})|json_encode|raw -}}
8888
{%- endblock request -%}
8989
```
90+
91+
## emsch_webhook_event
92+
93+
This function returns an `EMS\ClientHelperBundle\Helper\Webhook` instance for the current request's
94+
webhook.
95+
96+
If the current request is not a valid webhook request, an exception will be thrown.
97+
98+
A webhook request is always a POST request and must return JSON.
99+
100+
See [webhook](../../../elasticms-admin/api/webhook.md) for more information.
101+
102+
```twig
103+
{%- set event = emsch_webhook_event() -%}
104+
{%- if event.name == 'clear-cache' -%}
105+
{%- do ems_clear_http_caches() -%}
106+
{%- set success = true -%}
107+
{%- endif -%}
108+
{{- { success: success|default(false) }|json_encode|raw -}}
109+
```

src/dev/client-helper-bundle/routing.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,27 @@ emsch_demo_asset_in_archive:
398398
defaults: { hash: 253b903b1fb3ac30975ae9844a0352a65cdcfa3d, maxAge: 3600 }
399399
controller: 'EMS\CommonBundle\Controller\FileController::assetInArchive'
400400
```
401+
402+
## HTTP Cache Controller
403+
404+
This controller allows you to interact with the configured reverse proxy (e.g. Varnish) that serve
405+
your skeleton.
406+
407+
### Handle ElasticMS webhooks event
408+
409+
This controller's method will handle regular ElasticMS webhooks event to invalidate caches base on
410+
the event. Depending the event, part of all of the caches will be purged.
411+
412+
Example of route:
413+
414+
```yaml
415+
emsch_admin_webhook:
416+
config:
417+
path: '/_admin_webhook'
418+
method: [POST]
419+
host: 'localhost'
420+
controller: 'emsch.controller.http_cache::adminWebhook'
421+
```
422+
423+
In order to avoid unnecessary exposition you should defined a host in that route that is available
424+
from the admin but not from the Internet.

src/dev/common-bundle/twig.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,47 @@ Use php \intval function on input
631631
```twig
632632
{% set size = app.request.query.get('size', 0)|ems_int %}
633633
```
634+
635+
### ems_clear_http_caches
636+
637+
Calls the configured reverse proxy services to invalidate part or all of their caches.
638+
639+
It accepts one optional argument:
640+
641+
- **`URL or Tags` (optional)**:
642+
- If it contains an empty array (default value), a full cache purge will be sent.
643+
- If it is a string starting with `/`, a cache clear by URL will be sent.
644+
- Otherwise, a cache clear by tags will be sent.
645+
646+
#### Example of a cache clear route
647+
648+
The URL is triggered by a `clear-cache` webhook event, at the endpoint `//localhost/_clear_cache`.
649+
650+
Depending on the event data:
651+
652+
- If it contains a **`tags`** attribute (string or array of strings), a tag-based cache clear will
653+
be sent.
654+
- If it contains a **`url`** attribute (string), a URL-based cache clear will be sent.
655+
- Otherwise, a full cache purge will be sent.
656+
657+
The `emsch_clear_cache` route:
658+
659+
```yaml
660+
emsch_clear_cache:
661+
config:
662+
path: '/_clear_cache'
663+
method: [POST]
664+
host: 'localhost'
665+
template_static: template/admin/clear-cache.html.twig
666+
```
667+
668+
The `template/admin/clear-cache.html.twig` twig:
669+
670+
```twig
671+
{%- set event = emsch_webhook_event() -%}
672+
{%- if event.name == 'clear-cache' -%}
673+
{%- do ems_clear_http_caches(event.data.tags|default(event.data.url|default([]))) -%}
674+
{%- set success = true -%}
675+
{%- endif -%}
676+
{{- { success: success|default(false) }|json_encode|raw -}}
677+
```

src/dev/core-bundle/twig/core.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,17 @@ Function that generate a warning message to the user
302302
}) %}
303303
{% endif %}
304304
```
305+
306+
## emsco_webhook
307+
308+
Dispatches a custom webhook event to registered clients.
309+
This function takes two parameters:
310+
311+
- **event name** (string)
312+
- **data** (array)
313+
314+
```twig
315+
{% do emsco_webhook('custom-event', {
316+
'foo': 'bar',
317+
}) %}
318+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Monitoring
2+
3+
You can monitor an instance of elasticMS admin via the URL `/status`. This URL is declined in 3
4+
formats:
5+
6+
- HTML: `/status`
7+
- JSON: `/status.json`
8+
- XML: `/status.xml`
9+
10+
If something goes wrong with the elasticsearch cluster an HTTP call will return a `500` or a `503`
11+
HTTP return code.
12+
13+
## Extra parameters
14+
15+
By default, the `/status` URL does not return a `500` HTTP error if there is jobs in error or
16+
message stuck in the queue. But, some limits may be defined to do so:
17+
18+
- `jobs_pending_limit`: if defined, the URL will return a `500` code if the number of pending jobs
19+
is reached
20+
- `jobs_failed_limit`: if defined, the URL will return a `500` code if the number of failed jobs is
21+
reached
22+
- `bus_queue_limit`: if defined, the URL will return a `500` code if the number of messages in the
23+
bus queue is reached
24+
- `bus_failed_limit`: if defined, the URL will return a `500` code if the number of messages in the
25+
failed queue is reached

src/elasticms-admin/api/webhook.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ In case of success you'll get a JSON with those fields:
2222
- `id`: a UUID of the subscription you just created
2323
- `secret`: a secret that will be used to sign calls to the provided endpoint URL
2424

25+
Also, around 40 seconds after the registration, a
26+
[`validate_webhook_subscription`](#validate-subscription) webhook event is sent to the provided
27+
`endpointUrl`.
28+
2529
## Delete a subscription (unsubscribe)
2630

2731
This HTTP call will unsubscribe the subscription for the provided UUID:
@@ -68,6 +72,18 @@ Each webhook call's body contains a JSON with two fields:
6872

6973
## Events
7074

75+
### Validate subscription
76+
77+
This event is sent 40 seconds after the registration. The registered client must return a 200 to
78+
this call in order keep it working. This event may be resent from time to time by the backend in
79+
order to monitor the webhook connection. A `200` response is still required to keep the connection
80+
active.
81+
82+
The structure of the webhook `data` is:
83+
84+
- `secret` (string): The subscription's secret.
85+
- `events` (string[]): The list of registered events.
86+
7187
### Content Publish to
7288

7389
To register to this event, you have to specify on which environment you want by ending the string

src/elasticms-admin/commands/commands.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,27 @@ Options:
582582
--current-revision-only Translations will be updated only is the source revision is still a current revision
583583
--base-url[=BASE-URL] Base url, in order to generate a download link to the error report
584584
```
585+
586+
## Dispatch a webhook event
587+
588+
Command to trigger a custom webhook event to registered clients.
589+
590+
```bash
591+
Usage:
592+
emsco:webhook:dispatch <event-name> [<data>]
593+
594+
Arguments:
595+
event-name Name of the webhook event
596+
data Data (JSON format) [default: "{}"]
597+
598+
Options:
599+
-h, --help Display help for the given command. When no command is given display help for the list command
600+
-q, --quiet Do not output any message
601+
-V, --version Display this application version
602+
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
603+
-n, --no-interaction Do not ask any interactive question
604+
-e, --env=ENV The Environment name. [default: "dev"]
605+
--no-debug Switch off debug mode.
606+
--profile Enables profiling (requires debug).
607+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
608+
```

src/elasticms-web/parameters.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,30 @@ Bollean variable, if specified to `true` all elasticsearch query will be delegat
368368
And then you'll need to login on an admin first via the `ems:admin:login` command. By default, this
369369
variable is set to `false`.
370370

371+
### EMS_HTTP_CACHES
372+
373+
Available since release **6.10.0**.
374+
375+
This environment variable is used to define all reverse proxy services in front of your website.
376+
Currently, **Varnish** is the only supported reverse proxy.
377+
378+
It is used by the `HttpCacheManager` to invalidate cache services such as Varnish.
379+
380+
```dotenv
381+
EMS_HTTP_CACHES='[{"header":"x-varnish","url":"http://varnish.localhost/","headers":{"X-Invalidate-Token":"devsecret"}}]'
382+
```
383+
384+
This variable must contain a JSON-encoded array of objects. Each object is structured as follows:
385+
386+
- `header`: The name of the HTTP request header used by the `HttpCacheManager` to detect that the
387+
request has been forwarded by a reverse proxy. This field is required.
388+
- `url`: The internal URL of the reverse proxy that the HttpCacheManager will use to communicate
389+
with it. This field is required.
390+
- `headers` (optional): A set of HTTP headers that the HttpCacheManager will include when
391+
communicating with the reverse proxy. Default to `[]`.
392+
- `verify_ssl` (optional): Whether the certificate of the reverse proxy should be verified. Defaults
393+
to `true`.
394+
371395
## Elasticms Form Bundle variables
372396

373397
### EMSF_HASHCASH_DIFFICULTY

0 commit comments

Comments
 (0)