|
21 | 21 | logging.getLogger("PIL").setLevel(logging.WARNING) |
22 | 22 |
|
23 | 23 |
|
24 | | -def load_last_run(log_dir: str) -> Optional[datetime]: |
| 24 | +def load_last_run(log_dir: str, logger: Logger = None) -> Optional[datetime]: |
25 | 25 | """ |
26 | 26 | Load the last run timestamp from the .last_run file in the log directory. |
27 | 27 | """ |
28 | | - last_run_file = os.path.join(log_dir, ".last_run") |
29 | | - if os.path.exists(last_run_file): |
30 | | - with open(last_run_file, "r") as f: |
31 | | - ts = f.read().strip() |
32 | | - try: |
33 | | - return datetime.fromisoformat(ts) |
34 | | - except Exception: |
35 | | - pass |
36 | | - return None |
37 | | - |
38 | | - |
39 | | -def save_last_run(log_dir: str, dt: Optional[datetime] = None) -> None: |
| 28 | + try: |
| 29 | + last_run_file = os.path.join(log_dir, ".last_run") |
| 30 | + |
| 31 | + if os.path.exists(last_run_file): |
| 32 | + with open(last_run_file, "r") as f: |
| 33 | + ts = f.read().strip() |
| 34 | + try: |
| 35 | + return datetime.fromisoformat(ts) |
| 36 | + except Exception: |
| 37 | + pass |
| 38 | + except Exception as e: |
| 39 | + logger.error(f"Failed to read file: {e}") |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def save_last_run(log_dir: str, dt: Optional[datetime] = None, logger: Logger = None) -> None: |
40 | 44 | """ |
41 | 45 | Save the current timestamp (or provided datetime) to the .last_run file in the log directory. |
42 | 46 | """ |
43 | | - last_run_file = os.path.join(log_dir, ".last_run") |
44 | | - now = dt or datetime.now() |
45 | | - with open(last_run_file, "w") as f: |
46 | | - f.write(now.isoformat()) |
| 47 | + try: |
| 48 | + last_run_file = os.path.join(log_dir, ".last_run") |
| 49 | + now = dt or datetime.now() |
| 50 | + with open(last_run_file, "w") as f: |
| 51 | + f.write(now.isoformat()) |
| 52 | + except Exception as e: |
| 53 | + logger.error(f"Failed to write file: {e}") |
47 | 54 |
|
48 | 55 |
|
49 | 56 | def check_holiday( |
@@ -516,7 +523,7 @@ def no_assets_exit(): |
516 | 523 | return |
517 | 524 |
|
518 | 525 | log_dir = get_log_dir(config.module_name) |
519 | | - last_run = load_last_run(log_dir) |
| 526 | + last_run = load_last_run(log_dir, logger=logger) |
520 | 527 | if config.log_level.lower() == "debug": |
521 | 528 | print_settings(logger, config) |
522 | 529 |
|
@@ -611,6 +618,7 @@ def group_assets(assets): |
611 | 618 | if config.log_level == "debug": |
612 | 619 | print_json(assets_dict, logger, config.module_name, "assets_dict") |
613 | 620 | print_json(messages, logger, config.module_name, "messages") |
| 621 | + save_last_run(log_dir, logger=logger) |
614 | 622 |
|
615 | 623 |
|
616 | 624 | # --- Formatting function for border actions output --- |
@@ -669,7 +677,5 @@ def main(config: SimpleNamespace) -> None: |
669 | 677 | logger.error("\n\nAn error occurred:\n", exc_info=True) |
670 | 678 | logger.error("\n\n") |
671 | 679 | finally: |
672 | | - log_dir = get_log_dir(config.module_name) |
673 | | - save_last_run(log_dir) |
674 | 680 | # Log outro message with run time |
675 | 681 | logger.log_outro() |
0 commit comments