Skip to content

Commit 247dc84

Browse files
committed
Request purging
Resolves #57
1 parent c7c915b commit 247dc84

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
- Added new “Element is being saved for the first time” and “Element is a provisional draft” webhook filters.
77
- Some webhook filters now disable other mutually-exclusive filters when active and enabled.
88
- Pending webhook requests now have a “Send now” button, for manually triggering a webhook if the queue job was lost or failed. ([#35](https://github.com/craftcms/webhooks/issues/35))
9+
- Old webhook request activity is now purged during garbage collection. ([#57](https://github.com/craftcms/webhooks/discussions/57))
910
- Added the “Initial Delay” setting. ([#53](https://github.com/craftcms/webhooks/discussions/53))
11+
- Added the “Purge Duration” setting.
1012
- Added `craft\webhooks\filters\ExclusiveFilterInterface`.
1113

1214
### Changed

src/Plugin.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use craft\events\RegisterComponentTypesEvent;
88
use craft\events\RegisterUrlRulesEvent;
99
use craft\helpers\ArrayHelper;
10+
use craft\helpers\ConfigHelper;
1011
use craft\helpers\Db;
1112
use craft\helpers\Json;
1213
use craft\helpers\Queue;
1314
use craft\helpers\StringHelper;
15+
use craft\services\Gc;
1416
use craft\web\UrlManager;
1517
use craft\webhooks\filters\DraftFilter;
1618
use craft\webhooks\filters\ProvisionalDraftFilter;
@@ -34,6 +36,7 @@
3436
use yii\base\Event;
3537
use yii\base\Exception;
3638
use yii\base\InvalidArgumentException;
39+
use yii\base\InvalidConfigException;
3740

3841
/**
3942
* Webhooks plugin
@@ -225,6 +228,25 @@ function(RegisterUrlRulesEvent $e) {
225228
$e->rules['webhooks/activity'] = 'webhooks/activity/index';
226229
}
227230
);
231+
232+
// Garbage collection
233+
Event::on(Gc::class, Gc::EVENT_RUN, function() {
234+
$settings = $this->getSettings();
235+
if ($settings->purgeDuration) {
236+
try {
237+
$seconds = ConfigHelper::durationInSeconds($settings->purgeDuration);
238+
$date = new DateTime("$seconds seconds ago");
239+
Db::delete('{{%webhookrequests}}', [
240+
'and',
241+
['status' => Plugin::STATUS_DONE],
242+
['<', 'dateCreated', Db::prepareDateForDb($date)],
243+
]);
244+
} catch (InvalidConfigException $e) {
245+
Craft::warning("Couldn't purge webhook requests: {$e->getMessage()}");
246+
}
247+
248+
}
249+
});
228250
}
229251

230252
/**

src/Settings.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class Settings extends Model
3030
*/
3131
public $retryDelay = 60;
3232

33+
/**
34+
* @var int|null The time (in seconds) that request history should be saved in the database before being
35+
* deletable via garbage collection.
36+
* @since 3.4.0
37+
*/
38+
public $purgeDuration = 604800;
39+
3340
/**
3441
* @var array Custom config options that should be applied when creating Guzzle clients.
3542
* @since 2.3.0
@@ -50,6 +57,12 @@ public function setAttributes($values, $safeOnly = true)
5057
$values['initialDelay'] = null;
5158
}
5259

60+
if (empty($values['purgeDuration'])) {
61+
$values['purgeDuration'] = null;
62+
} else if (is_numeric($values['purgeDuration'])) {
63+
$values['purgeDuration'] = (int)$values['purgeDuration'];
64+
}
65+
5366
parent::setAttributes($values, $safeOnly);
5467
}
5568

@@ -60,7 +73,7 @@ protected function defineRules(): array
6073
{
6174
return [
6275
[['maxDepth', 'maxAttempts'], 'number', 'integerOnly' => true, 'min' => 1],
63-
[['initialDelay', 'retryDelay'], 'number', 'integerOnly' => true, 'min' => 0],
76+
[['initialDelay', 'retryDelay', 'purgeDuration'], 'number', 'integerOnly' => true, 'min' => 0],
6477
];
6578
}
6679

src/migrations/Install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function safeUp(): bool
7575
$this->createIndex(null, '{{%webhooks}}', ['groupId', 'name']);
7676
$this->createIndex(null, '{{%webhooks}}', ['name'], true);
7777
$this->createIndex(null, '{{%webhookrequests}}', ['debounceKey', 'status']);
78-
$this->createIndex(null, '{{%webhookrequests}}', ['status']);
78+
$this->createIndex(null, '{{%webhookrequests}}', ['status', 'dateCreated']);
7979
$this->addForeignKey(null, '{{%webhooks}}', ['groupId'], '{{%webhookgroups}}', ['id'], 'SET NULL');
8080
$this->addForeignKey(null, '{{%webhookrequests}}', ['webhookId'], '{{%webhooks}}', ['id'], 'SET NULL');
8181

src/migrations/m210830_202902_status_index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class m210830_202902_status_index extends Migration
1414
*/
1515
public function safeUp(): bool
1616
{
17-
$this->createIndex(null, '{{%webhookrequests}}', ['status']);
17+
$this->createIndex(null, '{{%webhookrequests}}', ['status', 'dateCreated']);
1818
return true;
1919
}
2020

src/templates/_settings.twig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@
3636
name: 'retryDelay',
3737
value: settings.retryDelay,
3838
}) }}
39+
40+
{{ forms.textField({
41+
id: 'purge-duration',
42+
label: 'Purge Duration'|t('webhooks'),
43+
instructions: 'The time (in seconds) that request history should be saved in the database before being deletable via garbage collection.'|t('webhooks'),
44+
size: 5,
45+
name: 'purgeDuration',
46+
value: settings.purgeDuration,
47+
}) }}

0 commit comments

Comments
 (0)