From d85b3f5998f2e527a5f67e74ed2afb19a02b7d76 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 14 Aug 2026 11:52:49 +0530 Subject: [PATCH] Add 2-day retention pruning for abdm_hiu_workflows --- app/Commands/AbdmHiuPrune.php | 80 +++++++++++++++++++ ...01_AddCreatedAtIndexToAbdmHiuWorkflows.php | 44 ++++++++++ scripts/install_hms_cron.sh | 1 + 3 files changed, 125 insertions(+) create mode 100644 app/Commands/AbdmHiuPrune.php create mode 100644 app/Database/Migrations/2026-08-14-000001_AddCreatedAtIndexToAbdmHiuWorkflows.php diff --git a/app/Commands/AbdmHiuPrune.php b/app/Commands/AbdmHiuPrune.php new file mode 100644 index 0000000..bc8213c --- /dev/null +++ b/app/Commands/AbdmHiuPrune.php @@ -0,0 +1,80 @@ + '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'); + } + } +} diff --git a/app/Database/Migrations/2026-08-14-000001_AddCreatedAtIndexToAbdmHiuWorkflows.php b/app/Database/Migrations/2026-08-14-000001_AddCreatedAtIndexToAbdmHiuWorkflows.php new file mode 100644 index 0000000..eb4e416 --- /dev/null +++ b/app/Database/Migrations/2026-08-14-000001_AddCreatedAtIndexToAbdmHiuWorkflows.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/scripts/install_hms_cron.sh b/scripts/install_hms_cron.sh index 2a46347..04cb078 100644 --- a/scripts/install_hms_cron.sh +++ b/scripts/install_hms_cron.sh @@ -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