Skip to content

Commit b566c08

Browse files
authored
Merge pull request #1594 from craftcms/feature/prune-db-logs-table
Prune logs table when processed feeds
2 parents ba68b6c + 87036e3 commit b566c08

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Plugin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use craft\feedme\web\twig\Extension;
1818
use craft\feedme\web\twig\variables\FeedMeVariable;
1919
use craft\helpers\UrlHelper;
20+
use craft\services\Gc;
2021
use craft\web\twig\variables\CraftVariable;
2122
use craft\web\UrlManager;
2223
use yii\base\Event;
@@ -86,6 +87,7 @@ public function init(): void
8687
$this->_registerCpRoutes();
8788
$this->_registerTwigExtensions();
8889
$this->_registerVariables();
90+
$this->_registerGc();
8991
}
9092

9193
/**
@@ -170,4 +172,15 @@ private function _registerVariables(): void
170172
$event->sender->set('feedme', FeedMeVariable::class);
171173
});
172174
}
175+
176+
private function _registerGc(): void
177+
{
178+
Event::on(
179+
Gc::class,
180+
Gc::EVENT_RUN,
181+
function(Event $event) {
182+
$this->getLogs()->prune();
183+
}
184+
);
185+
}
173186
}

src/services/Logs.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Logs extends Component
5757

5858
public const LOG_CATEGORY = 'feed-me';
5959
public const LOG_TABLE = '{{%feedme_logs}}';
60+
public const LOG_TABLE_LIMIT = 300;
6061

6162
public const LOG_LEVEL_MAP = [
6263
Logger::LEVEL_ERROR => 'error',
@@ -124,6 +125,22 @@ public function clear(): void
124125
->execute();
125126
}
126127

128+
public function prune(): void
129+
{
130+
$ids = (new Query())
131+
->select(['id'])
132+
->from(self::LOG_TABLE)
133+
->orderBy(['log_time' => SORT_DESC])
134+
->limit(self::LOG_TABLE_LIMIT)
135+
->column();
136+
137+
if (count($ids)) {
138+
Craft::$app->getDb()->createCommand()
139+
->delete(self::LOG_TABLE, ['not', ['in', 'id', $ids]])
140+
->execute();
141+
}
142+
}
143+
127144
/**
128145
* @param null $type
129146
* @return array
@@ -138,7 +155,7 @@ public function getLogEntries($type = null): array
138155
->from(self::LOG_TABLE)
139156

140157
// Hard-coded until pagination is implemented
141-
->limit(300);
158+
->limit(self::LOG_TABLE_LIMIT);
142159

143160
if ($type) {
144161
$query->andWhere(['level' => self::logLevelInt($type)]);

src/services/Process.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function beforeProcessFeed(FeedModel $feed, array $feedData)
8181
$gc = Craft::$app->getGc();
8282
$gc->deleteAllTrashed = true;
8383
$gc->run(true);
84+
} else {
85+
Plugin::getInstance()->getLogs()->prune();
8486
}
8587

8688
$this->_data = $feedData;

0 commit comments

Comments
 (0)