Add 2-day retention pruning for abdm_hiu_workflows
This commit is contained in:
parent
a55c6899f0
commit
d85b3f5998
80
app/Commands/AbdmHiuPrune.php
Normal file
80
app/Commands/AbdmHiuPrune.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace App\Commands;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
class AbdmHiuPrune extends BaseCommand
|
||||
{
|
||||
protected $group = 'ABDM';
|
||||
protected $name = 'abdm:hiu-prune';
|
||||
protected $description = 'Delete abdm_hiu_workflows rows older than the retention window (default 2 days).';
|
||||
protected $usage = 'abdm:hiu-prune [--days 2] [--batch 2000] [--dry-run] [--optimize]';
|
||||
protected $arguments = [];
|
||||
protected $options = [
|
||||
'--days' => 'Retention window in days (default: 2).',
|
||||
'--batch' => 'Rows deleted per statement (default: 2000).',
|
||||
'--dry-run' => 'Report what would be deleted without deleting.',
|
||||
'--optimize' => 'Run OPTIMIZE TABLE afterwards to release disk space.',
|
||||
];
|
||||
|
||||
public function run(array $params)
|
||||
{
|
||||
$days = (int) (CLI::getOption('days') ?? 2);
|
||||
if ($days <= 0) {
|
||||
$days = 2;
|
||||
}
|
||||
|
||||
$batch = (int) (CLI::getOption('batch') ?? 2000);
|
||||
if ($batch <= 0) {
|
||||
$batch = 2000;
|
||||
}
|
||||
|
||||
$dryRun = CLI::getOption('dry-run') !== null;
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
if (! $db->tableExists('abdm_hiu_workflows')) {
|
||||
CLI::error('Table abdm_hiu_workflows not found.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$cutoff = date('Y-m-d H:i:s', strtotime('-' . $days . ' days'));
|
||||
|
||||
// Rows with a future retry slot are still in flight for abdm:hiu-retry.
|
||||
$where = '((created_at IS NOT NULL AND created_at < ?)'
|
||||
. ' OR (created_at IS NULL AND updated_at IS NOT NULL AND updated_at < ?))'
|
||||
. ' AND (is_retryable = 0 OR next_retry_at IS NULL OR next_retry_at <= NOW())';
|
||||
|
||||
$total = (int) ($db->query(
|
||||
'SELECT COUNT(*) AS c FROM abdm_hiu_workflows WHERE ' . $where,
|
||||
[$cutoff, $cutoff]
|
||||
)->getRowArray()['c'] ?? 0);
|
||||
|
||||
CLI::write('ABDM HIU Workflow Prune' . ($dryRun ? ' (dry-run)' : ''), 'yellow');
|
||||
CLI::write('Retention: ' . $days . ' day(s) — cutoff ' . $cutoff);
|
||||
CLI::write('Eligible rows: ' . $total);
|
||||
|
||||
if ($total === 0 || $dryRun) {
|
||||
return;
|
||||
}
|
||||
|
||||
$deleted = 0;
|
||||
do {
|
||||
$db->query(
|
||||
'DELETE FROM abdm_hiu_workflows WHERE ' . $where . ' ORDER BY id LIMIT ' . $batch,
|
||||
[$cutoff, $cutoff]
|
||||
);
|
||||
$affected = (int) $db->affectedRows();
|
||||
$deleted += $affected;
|
||||
} while ($affected > 0);
|
||||
|
||||
CLI::write('Deleted: ' . $deleted, 'green');
|
||||
|
||||
if (CLI::getOption('optimize') !== null) {
|
||||
$db->query('OPTIMIZE TABLE abdm_hiu_workflows');
|
||||
CLI::write('Table optimized (disk space released).', 'green');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class AddCreatedAtIndexToAbdmHiuWorkflows extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! $this->db->tableExists('abdm_hiu_workflows')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Existing composite keys all start with another column, so retention
|
||||
// pruning by age alone would table-scan.
|
||||
if (! $this->indexExists('abdm_hiu_workflows', 'idx_abdm_hiu_created_at')) {
|
||||
$this->db->query('CREATE INDEX idx_abdm_hiu_created_at ON abdm_hiu_workflows (created_at)');
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! $this->db->tableExists('abdm_hiu_workflows')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->indexExists('abdm_hiu_workflows', 'idx_abdm_hiu_created_at')) {
|
||||
$this->db->query('DROP INDEX idx_abdm_hiu_created_at ON abdm_hiu_workflows');
|
||||
}
|
||||
}
|
||||
|
||||
private function indexExists(string $table, string $indexName): bool
|
||||
{
|
||||
$rows = $this->db->query('SHOW INDEX FROM ' . $table)->getResultArray();
|
||||
foreach ($rows as $row) {
|
||||
if (($row['Key_name'] ?? '') === $indexName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@ $BLOCK_START
|
|||
* * * * * cd $PROJECT_DIR && $PHP_BIN spark snomed:process-coding >> $LOG_DIR/snomed_process.log 2>&1
|
||||
*/2 * * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:hiu-poll --limit=30 >> $LOG_DIR/abdm_hiu_poll.log 2>&1
|
||||
*/5 * * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:hiu-retry --limit=20 >> $LOG_DIR/abdm_hiu_retry.log 2>&1
|
||||
30 2 * * * cd $PROJECT_DIR && $PHP_BIN spark abdm:hiu-prune --days=2 --optimize >> $LOG_DIR/abdm_hiu_prune.log 2>&1
|
||||
$BLOCK_END
|
||||
EOF
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user