Skip to content

Commit

Permalink
Merge pull request #70 from City-of-Helsinki/UHF-9620
Browse files Browse the repository at this point in the history
UHF-9620: Purge assets from varnish cache
  • Loading branch information
tuutti authored Feb 23, 2024
2 parents 68dd382 + c747b60 commit 20dda60
Show file tree
Hide file tree
Showing 39 changed files with 331 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name: CI
env:
SYMFONY_DEPRECATIONS_HELPER: disabled
BROWSERTEST_OUTPUT_DIRECTORY: 'public/sites/simpletest'
COMPOSER_DEPENDENCIES: drupal/redirect drupal/helfi_tunnistamo drupal/simple_sitemap drupal/varnish_purger
jobs:
tests:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -56,7 +57,7 @@ jobs:
# phpunit. Copy .git folder manually so codecov can generate line by
# line coverage.
cp -r $GITHUB_WORKSPACE/.git $MODULE_FOLDER/
composer require drupal/redirect drupal/helfi_tunnistamo drupal/simple_sitemap -W
composer require $COMPOSER_DEPENDENCIES -W
- name: Install Drupal
working-directory: ${{ env.DRUPAL_ROOT }}
Expand Down
12 changes: 12 additions & 0 deletions config/optional/purge.logger_channels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ channels:
- 0
- 2
- 3
-
id: purger_varnish_varnish_purge_all
grants:
- 0
- 2
- 3
-
id: purger_varnish_assets
grants:
- 0
- 2
- 3
11 changes: 9 additions & 2 deletions config/optional/purge.plugins.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
purgers:
-
order_index: 2
instance_id: default
plugin_id: varnish
order_index: 2
-
order_index: 3
instance_id: varnish_purge_all
plugin_id: varnish
order_index: 3
-
instance_id: assets
plugin_id: varnish
order_index: 4
processors:
-
plugin_id: drush_purge_queue_work
status: true
-
plugin_id: cron
status: true
-
plugin_id: drush_purge_invalidate
status: true
queuers:
-
plugin_id: purge_ui_block_queuer
Expand Down
27 changes: 27 additions & 0 deletions config/optional/varnish_purger.settings.assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
langcode: en
status: true
dependencies: { }
id: assets
name: Assets
invalidationtype: regex
hostname: localhost
port: 6081
path: '/[invalidation:expression]'
request_method: BAN
scheme: http
verify: '1'
headers:
-
field: X-VC-Purge-Method
value: regex
-
field: Host
value: localhost
body: null
body_content_type: null
runtime_measurement: true
timeout: 1.0
connect_timeout: 1.0
cooldown_time: 0.0
max_requests: 100
http_errors: true
40 changes: 39 additions & 1 deletion helfi_proxy.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Contains helfi_proxy installation procedure.
*/

declare(strict_types = 1);
declare(strict_types=1);

/**
* Implements hook_install().
Expand All @@ -25,3 +25,41 @@ function helfi_proxy_install() {
function helfi_proxy_update_9001() : void {
helfi_proxy_install();
}

/**
* Enable asset purge configuration.
*/
function helfi_proxy_update_9002() : void {
if (!\Drupal::moduleHandler()->moduleExists('varnish_purger')) {
return;
}
/** @var \Drupal\Core\Config\ConfigInstallerInterface $configInstaller */
$configInstaller = \Drupal::service('config.installer');
$configInstaller->installDefaultConfig('module', 'helfi_proxy');

// Re-installing the default configuration does not install purge
// configuration for some reason.
$plugins = \Drupal::configFactory()->getEditable('purge.plugins');
$purgers = $plugins->get('purgers');

if (!array_filter($purgers, fn (array $item) => $item['instance_id'] === 'assets')) {
$purgers[] = [
'instance_id' => 'assets',
'plugin_id' => 'varnish',
'order_index' => 4,
];
$plugins->set('purgers', $purgers)
->save();
}
$loggers = \Drupal::configFactory()->getEditable('purge.logger_channels');
$channels = $loggers->get('channels');

if (!array_filter($channels, fn (array $item) => $item['id'] === 'purger_varnish_assets')) {
$channels[] = [
'id' => 'purger_varnish_assets',
'grants' => [0, 2, 3],
];
}
$loggers->set('channels', $channels)
->save();
}
7 changes: 1 addition & 6 deletions helfi_proxy.services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
helfi_proxy.valid_origin_domains:
- hel.ninja
- hel.fi
- docker.so
services:
Expand Down Expand Up @@ -45,9 +46,3 @@ services:
- '%helfi_proxy.valid_origin_domains%'
tags:
- { name: event_subscriber }

helfi_proxy.asset.css.optimizer:
public: false
class: Drupal\helfi_proxy\Asset\CssOptimizer
decorates: asset.css.optimizer
arguments: ['@config.factory', '@file_url_generator']
2 changes: 1 addition & 1 deletion src/ActiveSitePrefix.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy;

Expand Down
63 changes: 0 additions & 63 deletions src/Asset/CssOptimizer.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Cache/Context/SitePrefixCacheContext.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\Cache\Context;

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/FrontController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\Controller;

Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscriber/CorsResponseSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\EventSubscriber;

Expand Down
59 changes: 59 additions & 0 deletions src/EventSubscriber/DeploySubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_proxy\EventSubscriber;

use Drupal\helfi_api_base\EventSubscriber\DeployHookEventSubscriberBase;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface;
use Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface;
use Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Purge assets from varnish cache.
*/
final class DeploySubscriber extends DeployHookEventSubscriberBase {

/**
* Constructs a new instance.
*
* @param \Drupal\helfi_proxy\ProxyManagerInterface $proxyManager
* The proxy manager service.
* @param \Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface $invalidationFactory
* The invalidation service.
* @param \Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface $queuers
* The queuer service.
* @param \Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface $queue
* The purge queue service.
*/
public function __construct(
private readonly ProxyManagerInterface $proxyManager,
private readonly InvalidationsServiceInterface $invalidationFactory,
private readonly QueuersServiceInterface $queuers,
private readonly QueueServiceInterface $queue,
) {
}

/**
* Responds to 'helfi_api_base.post_deploy' event.
*
* @param \Symfony\Contracts\EventDispatcher\Event $event
* The event.
*/
public function onPostDeploy(Event $event) : void {
if (!$this->proxyManager->isConfigured(ProxyManagerInterface::ASSET_PATH)) {
return;
}
$assetPath = $this->proxyManager->getConfig(ProxyManagerInterface::ASSET_PATH);

$queuer = $this->queuers->get('helfi_proxy_queue_everything');
// Purge all assets from Varnish cache. For example: /liikenne-assets/*
// on Liikenne project.
$this->queue->add($queuer, [
$this->invalidationFactory->get('regex', ltrim($assetPath, '/')),
]);
}

}
2 changes: 1 addition & 1 deletion src/EventSubscriber/RobotsResponseSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\EventSubscriber;

Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscriber/TunnistamoRedirectUrlSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\EventSubscriber;

Expand Down
2 changes: 1 addition & 1 deletion src/Form/PurgeForm.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\Form;

Expand Down
12 changes: 11 additions & 1 deletion src/HelfiProxyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\helfi_proxy\EventSubscriber\DeploySubscriber;
use Drupal\helfi_proxy\EventSubscriber\TunnistamoRedirectUrlSubscriber;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -49,6 +50,15 @@ public function register(ContainerBuilder $container) : void {
->addArgument(new Reference('helfi_proxy.active_prefix'));
}

if (isset($modules['varnish_purger'])) {
$container->register('helfi_proxy.deploy_hook', DeploySubscriber::class)
->addTag('event_subscriber')
->addArgument(new Reference('helfi_proxy.proxy_manager'))
->addArgument(new Reference('purge.invalidation.factory'))
->addArgument(new Reference('purge.queuers'))
->addArgument(new Reference('purge.queue'));
}

}

}
2 changes: 1 addition & 1 deletion src/PathProcessor/SitePrefixPathProcessor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\PathProcessor;

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/DebugDataItem/Robots.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\Plugin\DebugDataItem;

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Purge/Queuer/QueueEverything.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy\Plugin\Purge\Queuer;

Expand Down
2 changes: 1 addition & 1 deletion src/ProxyManager.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\helfi_proxy;

Expand Down
Loading

0 comments on commit 20dda60

Please sign in to comment.