|
24 | 24 |
|
25 | 25 | import json |
26 | 26 | import logging |
| 27 | +import random |
| 28 | +import time |
27 | 29 | from datetime import datetime |
28 | 30 |
|
29 | 31 | import boto3 |
@@ -227,27 +229,46 @@ def refresh_has_metakg(): |
227 | 229 | def routine(no_backup=False): |
228 | 230 | logger = logging.getLogger("routine") |
229 | 231 |
|
| 232 | + # Add jitter: random delay between 100 and 500 milliseconds (adjust range as needed) |
| 233 | + jitter_ms = random.uniform(100, 500) # Jitter in milliseconds |
| 234 | + jitter_seconds = jitter_ms / 1000 # Convert milliseconds to seconds |
| 235 | + logger.info(f"Applying jitter delay of {jitter_ms:.2f} milliseconds before acquiring lock.") |
| 236 | + time.sleep(jitter_seconds) |
| 237 | + |
| 238 | + lock_acquired = False |
| 239 | + |
230 | 240 | try: |
231 | 241 | # if previously acquired, |
232 | 242 | # it won't block here |
233 | | - _lock.acquire() |
234 | | - logger.info("Schedule lock acquired successfully.") |
| 243 | + lock_acquired = _lock.acquire() |
| 244 | + if lock_acquired: |
| 245 | + logger.info("Schedule lock acquired successfully.") |
| 246 | + if not no_backup: |
| 247 | + logger.info("backup_to_s3()") |
| 248 | + backup_to_s3() |
| 249 | + logger.info("refresh_document()") |
| 250 | + refresh_document() |
| 251 | + logger.info("check_uptime()") |
| 252 | + check_uptime() |
| 253 | + logger.info("refresh_metakg()") |
| 254 | + refresh_metakg() |
| 255 | + logger.info("consolidate_metakg()") |
| 256 | + consolidate_metakg() |
| 257 | + logger.info("refresh_has_metakg()") |
| 258 | + refresh_has_metakg() |
| 259 | + else: |
| 260 | + logger.warning("Schedule lock acquired by another process. No need to run it in this process.") |
235 | 261 | except Timeout: |
236 | 262 | logger.warning("Schedule lock acquired by another process. No need to run it in this process.") |
| 263 | + except Exception as e: |
| 264 | + logger.error(f"An error occurred during the routine: {e}") |
| 265 | + if lock_acquired: |
| 266 | + _lock.release() |
237 | 267 | return |
238 | | - if not no_backup: |
239 | | - logger.info("backup_to_s3()") |
240 | | - backup_to_s3() |
241 | | - logger.info("refresh_document()") |
242 | | - refresh_document() |
243 | | - logger.info("check_uptime()") |
244 | | - check_uptime() |
245 | | - logger.info("refresh_metakg()") |
246 | | - refresh_metakg() |
247 | | - logger.info("consolidate_metakg()") |
248 | | - consolidate_metakg() |
249 | | - logger.info("refresh_has_metakg()") |
250 | | - refresh_has_metakg() |
| 268 | + finally: |
| 269 | + if lock_acquired: |
| 270 | + _lock.release() |
| 271 | + logger.info("Schedule lock released successfully.") |
251 | 272 |
|
252 | 273 |
|
253 | 274 | if __name__ == "__main__": |
|
0 commit comments