Skip to content

Commit

Permalink
add configuration settings to enable or disable resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
WengerK committed Mar 12, 2024
1 parent 9c30fbc commit 82f7c81
Show file tree
Hide file tree
Showing 24 changed files with 593 additions and 21 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ jobs:
run: docker-compose -f docker-compose.yml run -u www-data drupal phpunit --no-coverage --group=${{ matrix.module }} --exclude-group=${{ matrix.module }}_functional --configuration=/var/www/html/phpunit.xml
continue-on-error: ${{ matrix.experimental }}


tests-functional:
name: Functional Tests

runs-on: ubuntu-latest

strategy:
matrix:
drupal_version: ['9.5', '10.0', '10.1', '10.2', '11.0']
module: [ 'timesup' ]
experimental: [ false ]
include:
- drupal_version: '11.0'
module: 'timesup'
experimental: true

steps:
- uses: actions/checkout@v4
- run: docker-compose -f docker-compose.yml pull --include-deps drupal
- name: Build the docker-compose stack
run: docker-compose -f docker-compose.yml build --pull --build-arg BASE_IMAGE_TAG=${{ matrix.drupal_version }} drupal
continue-on-error: ${{ matrix.experimental }}
- name: Up a persistant Docker Container
run: docker-compose -f docker-compose.yml up -d drupal
- name: wait on Docker to be ready, especially Apache that takes many seconds to be up
run: docker-compose exec -T drupal wait-for-it drupal:80 -t 60
- name: wait on Docker to be ready, especially MariaDB that takes many seconds to be up
run: docker-compose exec -T drupal wait-for-it db:3306 -t 60
- name: Bootstrap Drupal
run: docker-compose -f docker-compose.yml exec -T -u www-data drupal drush site-install standard --db-url="mysql://drupal:drupal@db/drupal" -y
continue-on-error: ${{ matrix.experimental }}
- name: Run tests
run: docker-compose -f docker-compose.yml exec -T -u www-data drupal phpunit --no-coverage --group=${{ matrix.module }}_functional --configuration=/var/www/html/phpunit.xml
continue-on-error: ${{ matrix.experimental }}

upgrade-status:
name: Upgrade Status
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- add tests with drupal 10.3
- add configuration settings `timesup.settings.resolvers` to enable or disable resolvers.

## [2.0.1] - 2024-03-01
### Fixed
Expand Down
25 changes: 25 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Upgrade guide

## Upgrade from 2.0.1 to 2.1.x

This guide is intended to describe the process of upgrading
Times'up from version 2.0.1 to version 2.1.x.

### Changes in configuration

Running `drush updb` and exporting the changed configuration should be sufficient
to convert to the new configuration structure.

The `timesup.settings` configuration object has been introduced. This allows you to enable only the resolvers you actively use, reducing stress on cache and database invalidation.

```yaml
resolvers:
minutely: false
hourly: true
daily: true
midnight: true
weekly: true
```
6 changes: 6 additions & 0 deletions config/install/timesup.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resolvers:
minutely: false
hourly: true
daily: true
midnight: true
weekly: true
23 changes: 23 additions & 0 deletions config/schema/timesup.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
timesup.settings:
type: config_object
label: Times'up settings
mapping:
resolvers:
type: mapping
label: 'Resolvers'
mapping:
minutely:
type: boolean
label: Enable minutely's Times'up cache resolvers
hourly:
type: boolean
label: Enable daily's Times'up cache resolvers
daily:
type: boolean
label: Enable daily's Times'up cache resolvers
midnight:
type: boolean
label: Enable midnight's Times'up cache resolvers
weekly:
type: boolean
label: Enable weekly's Times'up cache resolvers
100 changes: 100 additions & 0 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\timesup\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Times'up settings form.
*/
class SettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'timesup_admin_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$resolvers = $this->config('timesup.settings')->get('resolvers');

// Submitted form values should be nested.
$form['#tree'] = TRUE;

$form['resolvers'] = [
'#type' => 'fieldset',
'#title' => $this->t('Resolvers'),
'#description' => $this->t('Please enable resolvers you actively use to avoid cache/database invalidation stress.'),
];

$form['resolvers']['minutely'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Minutely resolver.'),
'#default_value' => !empty($resolvers['minutely']),
'#description' => $this->t('Enable this feature will invalid cache-tags <code>timesup:minutely</code> every minutes (heavy stress sensitive).'),
];

$form['resolvers']['hourly'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Hourly resolver.'),
'#default_value' => !empty($resolvers['hourly']),
'#description' => $this->t('Enable this feature will invalid cache-tags <code>timesup:hourly</code> every hour.'),
];

$form['resolvers']['daily'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Daily resolver.'),
'#default_value' => !empty($resolvers['daily']),
'#description' => $this->t('Enable this feature will invalid cache-tags <code>timesup:daily</code> every day.'),
];

$form['resolvers']['midnight'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Midnight resolver.'),
'#default_value' => !empty($resolvers['midnight']),
'#description' => $this->t('Enable this feature will invalid cache-tags <code>timesup:midnight</code> every day at 00:00:00.'),
];

$form['resolvers']['weekly'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Weekly resolver.'),
'#default_value' => !empty($resolvers['weekly']),
'#description' => $this->t('Enable this feature will invalid cache-tags <code>timesup:weekly</code> every weeks.'),
];

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$this->config('timesup.settings')
->set('resolvers', [
'minutely' => (bool) $form_state->getValue(['resolvers', 'minutely']),
'hourly' => (bool) $form_state->getValue(['resolvers', 'hourly']),
'daily' => (bool) $form_state->getValue(['resolvers', 'daily']),
'midnight' => (bool) $form_state->getValue(['resolvers', 'midnight']),
'weekly' => (bool) $form_state->getValue(['resolvers', 'weekly']),
])
->save();
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'timesup.settings',
];
}

}
7 changes: 7 additions & 0 deletions src/Periodicity/DailyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ final class DailyResolver extends PeriodicityBaseResolver {
* {@inheritdoc}
*/
public function shouldApply(): bool {
$settings = $this->configFactory->get('timesup.settings');
$resolvers = $settings->get('resolvers');

if (!isset($resolvers['daily']) || !$resolvers['daily']) {
return FALSE;
}

$last_run_per_day = $this->state->get($this->getLastRunKey());
return !($this->time->getRequestTime() - $last_run_per_day < 86400);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Periodicity/HourlyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ final class HourlyResolver extends PeriodicityBaseResolver {
* {@inheritdoc}
*/
public function shouldApply(): bool {
$settings = $this->configFactory->get('timesup.settings');
$resolvers = $settings->get('resolvers');

if (!isset($resolvers['hourly']) || !$resolvers['hourly']) {
return FALSE;
}

$last_run_per_week = $this->state->get($this->getLastRunKey());
return !($this->time->getRequestTime() - $last_run_per_week < 3600);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Periodicity/MidnightResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class MidnightResolver extends PeriodicityBaseResolver {
* {@inheritdoc}
*/
public function shouldApply(): bool {
$settings = $this->configFactory->get('timesup.settings');
$resolvers = $settings->get('resolvers');

if (!isset($resolvers['midnight']) || !$resolvers['midnight']) {
return FALSE;
}

$today_midnight = $this->getTodayMidnight();
$last_run = $this->state->get($this->getLastRunKey());
return $last_run <= $today_midnight->getTimestamp();
Expand Down
7 changes: 7 additions & 0 deletions src/Periodicity/MinutelyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ final class MinutelyResolver extends PeriodicityBaseResolver {
* {@inheritdoc}
*/
public function shouldApply(): bool {
$settings = $this->configFactory->get('timesup.settings');
$resolvers = $settings->get('resolvers');

if (!isset($resolvers['minutely']) || !$resolvers['minutely']) {
return FALSE;
}

$last_run_per_minute = $this->state->get($this->getLastRunKey());
return !($this->time->getRequestTime() - $last_run_per_minute < 60);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Periodicity/PeriodicityBaseResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
Expand All @@ -22,6 +23,11 @@ abstract class PeriodicityBaseResolver implements PeriodicityResolverInterface {
*/
protected $cacheTagsInvalidator;

/**
* The config factory.
*/
protected ConfigFactoryInterface $configFactory;

/**
* The state service.
*
Expand All @@ -48,15 +54,18 @@ abstract class PeriodicityBaseResolver implements PeriodicityResolverInterface {
*
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tags invalidator.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger channel factory service.
*/
public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator, StateInterface $state, TimeInterface $time, LoggerChannelFactoryInterface $logger_factory) {
public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator, ConfigFactoryInterface $config_factory, StateInterface $state, TimeInterface $time, LoggerChannelFactoryInterface $logger_factory) {
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->configFactory = $config_factory;
$this->state = $state;
$this->time = $time;
$this->logger = $logger_factory->get('timesup');
Expand Down
7 changes: 7 additions & 0 deletions src/Periodicity/WeeklyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ final class WeeklyResolver extends PeriodicityBaseResolver {
* {@inheritdoc}
*/
public function shouldApply(): bool {
$settings = $this->configFactory->get('timesup.settings');
$resolvers = $settings->get('resolvers');

if (!isset($resolvers['weekly']) || !$resolvers['weekly']) {
return FALSE;
}

$last_run_per_week = $this->state->get($this->getLastRunKey());
return !($this->time->getRequestTime() - $last_run_per_week < 604800);
}
Expand Down
Loading

0 comments on commit 82f7c81

Please sign in to comment.