Skip to content

Commit 0bb214d

Browse files
committed
Add quicker cleanup method script
1 parent 03adbe5 commit 0bb214d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

crontab

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Clean up older games hourly
22
0 * * * * php -d max_execution_time=2700 bin/console cron
3+
4+
# Alternatively: just remove the records from the database (faster and lighter)
5+
#0 * * * * php dbcleanup.php

dbcleanup.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
5+
if (class_exists(Dotenv\Dotenv::class) && file_exists(__DIR__ . '/.env')) {
6+
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
7+
$dotenv->load();
8+
}
9+
10+
$interval = 86400;
11+
12+
if (file_exists(__DIR__ . '/config.php')) {
13+
$config = include_once __DIR__ . '/config.php';
14+
$interval = $config['commands']['configs']['cleansessions']['clean_interval'] ?? $interval;
15+
}
16+
17+
$dsn = parse_url(getenv('DATABASE_URL'));
18+
19+
if (isset($dsn['host'])) {
20+
$pdo = new PDO('mysql:' . 'host=' . $dsn['host'] . ';port=' . $dsn['port'] . ';dbname=' . ltrim($dsn['path'], '/'), $dsn['user'], $dsn['pass']);
21+
$query = $pdo->query('DELETE FROM game WHERE updated_at < NOW() - INTERVAL ' . $interval . ' SECOND;');
22+
echo $query->rowCount() . PHP_EOL;
23+
} else {
24+
die('DATABASE_URL is not set or is not a valid DSN string!' . PHP_EOL);
25+
}

0 commit comments

Comments
 (0)