Skip to content

Commit e1bbf69

Browse files
authored
Merge pull request #68 from NETWAYS/feature/per-feed-polling-rate-65
feat: configure polling interval per feed
2 parents 099235e + 6fb55dc commit e1bbf69

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

application/controllers/FeedController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function indexAction(): void
4848

4949
$this->addTitle($this->translate('Feed'), $controlWrapper);
5050

51-
[$url, $type, $name] = $this->getFeedInfo();
51+
[$url, $type, $name, $interval] = $this->getFeedInfo();
5252

5353
if ($url === null) {
5454
$this->displayError($this->translate('No such feed configured'));
@@ -66,7 +66,7 @@ public function indexAction(): void
6666

6767
try {
6868
$reader = new FeedReader($url, $this->Config(), $type);
69-
$data = $reader->fetch($name);
69+
$data = $reader->fetch($name, $interval);
7070
} catch (Exception $ex) {
7171
$this->displayError($ex->getMessage());
7272
return;
@@ -103,24 +103,24 @@ protected function getFeedInfo(): array
103103
$feed = $storage->getFeedByName($name);
104104

105105
if ($feed === null) {
106-
return [null, null, null];
106+
return [null, null, null, null];
107107
}
108108

109-
return [$feed->url, $feed->type, 'feed-' . $feed->name];
109+
return [$feed->url, $feed->type, 'feed-' . $feed->name, $feed->pollingInterval];
110110
}
111111

112112
$url = $this->params->shift('url');
113113

114114
if ($url === null or $url === '') {
115-
return [null, null, null];
115+
return [null, null, null, null];
116116
}
117117

118118
$this->assertPermission('feeds/view/arbitrary');
119119

120120
$type = $this->params->shift('type') ?? 'auto';
121121
$name = 'url-' . sha1($url . ':' . $type);
122122

123-
return [$url, FeedType::fromDisplay($type), $name];
123+
return [$url, FeedType::fromDisplay($type), $name, null];
124124
}
125125

126126
/**
@@ -140,7 +140,7 @@ public function createAction(): void
140140
$this->addTitle($this->translate('Create a new feed'));
141141

142142
$storage = StorageFactory::getStorage();
143-
$form = new FeedForm($storage, null);
143+
$form = new FeedForm($this->Config(), $storage, null);
144144

145145
$form->on(Form::ON_SUCCESS, function () {
146146
Notification::success($this->translate('Created new feed'));
@@ -181,14 +181,15 @@ public function editAction(): void
181181
$title = $this->translate('Edit feed');
182182
$this->setTitle($title);
183183

184-
$form = new FeedForm($storage, $feed);
184+
$form = new FeedForm($this->Config(), $storage, $feed);
185185

186186
$form->populate([
187187
'name' => $feed->name,
188188
'url' => $feed->url,
189189
'description' => $feed->description,
190190
'is_visible' => $feed->isVisible,
191191
'type' => $feed->type->display(),
192+
'polling_interval' => $feed->pollingInterval,
192193
]);
193194

194195
$form->on(Form::ON_SUCCESS, function () {

application/controllers/FeedsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function indexAction(): void
100100

101101
try {
102102
$reader = new FeedReader($feed->url, $this->Config(), $feed->type);
103-
$data = $reader->fetch('feed-' . $feed->name);
103+
$data = $reader->fetch('feed-' . $feed->name, $feed->pollingInterval);
104104
$feedsCounter++;
105105
} catch (Exception) {
106106
$failed[] = $feed->name;

application/forms/FeedForm.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Icinga\Module\Feeds\Forms;
44

5+
use Icinga\Application\Config;
56
use Icinga\Module\Feeds\FeedCache;
67

78
use Icinga\Module\Feeds\Parser\FeedType;
@@ -24,10 +25,11 @@ class FeedForm extends CompatForm
2425

2526
protected ?string $deleteButtonName = null;
2627

27-
// Pattern to valide feed names
28+
// Pattern to validate feed names
2829
protected const VALID_NAME = '/^[a-zA-Z0-9\-_\. ]+$/';
2930

3031
public function __construct(
32+
protected Config $config,
3133
protected StorageInterface $storage,
3234
protected ?FeedDefinition $feed,
3335
) {
@@ -98,6 +100,19 @@ protected function assemble(): void
98100
]
99101
);
100102

103+
$cacheDurationInSeconds = $this->config->get('cache', 'duration', 43200);
104+
$this->addElement(
105+
'number',
106+
'polling_interval',
107+
[
108+
'label' => $this->translate('Polling interval'),
109+
'description' => $this->translate(
110+
'How often the feed should be updated from the source (in seconds)'
111+
),
112+
'placeholder' => $cacheDurationInSeconds . " (" . $this->translate('inherited from module configuration') . ")",
113+
]
114+
);
115+
101116
$this->addElement('textarea', 'description', [
102117
'label' => $this->translate('Description'),
103118
'description' => $this->translate(
@@ -181,6 +196,7 @@ protected function onSuccess(): void
181196
$url = trim($this->getValue('url'));
182197
$isVisible = $this->getElement('is_visible')->isChecked();
183198
$type = FeedType::fromDisplay($this->getValue('type') ?? 'auto');
199+
$pollingInterval = $this->getValue('polling_interval') ?? null;
184200
$description = trim($this->getValue('description'));
185201

186202
if ($this->isCreateForm()) {
@@ -190,6 +206,7 @@ protected function onSuccess(): void
190206
$description,
191207
$isVisible,
192208
$type,
209+
$pollingInterval,
193210
);
194211

195212
if ($this->storage->getFeedByName($name) !== null) {
@@ -218,6 +235,7 @@ protected function onSuccess(): void
218235
$this->feed->type = $type;
219236
$this->feed->isVisible = $isVisible;
220237
$this->feed->description = $description;
238+
$this->feed->pollingInterval = $pollingInterval;
221239

222240
if ($isRename) {
223241
$this->storage->addFeed($this->feed);

library/Feeds/FeedReader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ protected function fetchAndParse(): ?Feed
122122
/**
123123
* fetch loads a feed either from the cache or from its URL
124124
*/
125-
public function fetch(?string $cacheKey = null): ?Feed
125+
public function fetch(?string $cacheKey = null, ?int $cacheDurationInSeconds = null): ?Feed
126126
{
127127
// We don't expect feeds to update very frequently, to avoid
128128
// 429 errors we set the default to 12 hours
129-
$cacheDurationInSeconds = $this->config->get('cache', 'duration', 43200);
129+
if ($cacheDurationInSeconds === null) {
130+
$cacheDurationInSeconds = $this->config->get('cache', 'duration', 43200);
131+
}
130132
$cache = FeedCache::instance('feeds');
131133

132134
if ($cacheKey !== null && $cacheDurationInSeconds > 0) {

library/Feeds/Storage/FeedDefinition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(
1515
public string $description = '',
1616
public bool $isVisible = true,
1717
public FeedType $type = FeedType::Auto,
18+
public ?int $pollingInterval = null,
1819
) {
1920
}
2021

@@ -29,6 +30,7 @@ public function toArray(): array
2930
'description' => trim($this->description),
3031
'is_visible' => $this->isVisible,
3132
'type' => $this->type->display(),
33+
'polling_interval' => $this->pollingInterval,
3234
];
3335
}
3436

@@ -43,6 +45,7 @@ public static function fromArray(array $data): FeedDefinition
4345
trim($data['description'] ?? ''),
4446
$data['is_visible'] ?? true,
4547
FeedType::fromDisplay(trim($data['type']) ?? 'auto'),
48+
$data['polling_interval'] ?? null,
4649
);
4750
}
4851
}

0 commit comments

Comments
 (0)