This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclean_expired_records.py
More file actions
42 lines (34 loc) · 1.57 KB
/
clean_expired_records.py
File metadata and controls
42 lines (34 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import time
from prometheus_client import start_http_server as start_metrics_server
from historical_system_profiles import config, db_interface, listener_logging
from historical_system_profiles import listener_metrics as metrics
from historical_system_profiles import probes
from historical_system_profiles.app import create_app
def main():
logger = listener_logging.initialize_logging()
start_metrics_server(config.listener_metrics_port)
app = create_app()
logger.info(
"starting expired record cleaning loop, will remove records older than "
"%s days every %s minutes"
% (config.valid_profile_age_days, config.expired_cleaner_sleep_minutes)
)
expired_record_cleaning_loop(app.app, logger)
def expired_record_cleaning_loop(flask_app, logger):
with flask_app.app_context():
probes._update_readiness_state()
while True:
try:
with metrics.records_cleaning_time.time():
deleted_count = db_interface.clean_expired_records(
config.valid_profile_age_days
)
logger.info("deleted %s expired records" % deleted_count)
metrics.records_cleaned.inc(deleted_count)
except Exception:
logger.exception("An error occurred during expired record cleaning loop")
probes._update_liveness_state()
logger.info("sleeping for %s minutes" % config.expired_cleaner_sleep_minutes)
time.sleep(config.expired_cleaner_sleep_minutes * 60)
if __name__ == "__main__":
main()