Skip to content

Cleaner

Port: :4070
Source: apps/workers/cleaner/

The cleaner is the data retention worker. It runs continuous background loops to find and purge expired records across MongoDB, PostgreSQL, and Redis. Each sub-module handles one category of data with its own TTL and concurrency guard.

Responsibilities

ModuleData CleanedStoreTTL Env Var
AuditsModuleAudit log recordsPostgreSQLCLEANER_AUDIT_LOGS_TTL
StatsModuleAggregated stat entriesMongoDBCLEANER_STATS_TTL
SagasModuleCompleted saga stage recordsMongoDBCLEANER_SAGA_STAGES_TTL
MetricsModuleIoT metric time-seriesMongoDBCLEANER_METRICS_TTL
StashesModuleDispatcher stash recordsPostgreSQLCLEANER_STASH_LOGS_TTL

Default retention if a TTL is unset: stats 10 years, metrics 5 years, audit logs 4 years, saga stages 1 year, stash records 100 days.

Purge Loop Pattern

Every sub-module follows the same producer/consumer pattern:

A Redis key is set per record being purged to prevent duplicate deletions if the loop runs faster than the delete operation. The lock is always cleared in a finally block.

Infrastructure Dependencies

DependencyUsage
MongoDBPurges expired Stats, Metrics, and saga-stage (essential/saga-stages) records
PostgreSQLPurges expired Audits and Stashes
RedisTracks in-progress purge state to prevent duplicate deletions

Key Files

FilePurpose
modules/logger/audits/audits.service.tsPurge loop for audit logs
modules/special/stats/stats.service.tsPurge loop for stat entries
modules/essential/sagas/sagas.service.tsPurge loop for saga stage records
modules/thing/metrics/metrics.service.tsPurge loop for IoT metric readings
modules/dispatcher/stashes/stashes.service.tsPurge loop for dispatcher stash records
app.service.tsWorker health and initialization