|
| 1 | +import os |
| 2 | +import logging |
| 3 | +import subprocess |
| 4 | +import random |
| 5 | + |
| 6 | + |
| 7 | +from datalad_service.config import DATALAD_DATASET_PATH |
| 8 | +from datalad_service.broker import broker |
| 9 | + |
| 10 | + |
| 11 | +def dataset_factory(): |
| 12 | + """ |
| 13 | + Factory that yields dataset paths from the DATALAD_DATASET_PATH directory. |
| 14 | + It shuffles the list of datasets and yields them one by one. |
| 15 | + When all datasets have been yielded, it re-reads the directory and shuffles again. |
| 16 | + """ |
| 17 | + datasets = [] |
| 18 | + while True: |
| 19 | + if not datasets: |
| 20 | + # Read the directory and shuffle if the list is empty |
| 21 | + try: |
| 22 | + datasets = [ |
| 23 | + os.path.join(DATALAD_DATASET_PATH, d) |
| 24 | + for d in os.listdir(DATALAD_DATASET_PATH) |
| 25 | + if os.path.isdir(os.path.join(DATALAD_DATASET_PATH, d)) |
| 26 | + and d.startswith('ds') |
| 27 | + ] |
| 28 | + if not datasets: |
| 29 | + logging.warning( |
| 30 | + f'No datasets found in {DATALAD_DATASET_PATH} for maintenance tasks.' |
| 31 | + ) |
| 32 | + return |
| 33 | + random.shuffle(datasets) |
| 34 | + except FileNotFoundError: |
| 35 | + logging.error(f'DATALAD_DATASET_PATH not found: {DATALAD_DATASET_PATH}') |
| 36 | + return |
| 37 | + yield datasets.pop() |
| 38 | + |
| 39 | + |
| 40 | +gc_dataset_generator = dataset_factory() |
| 41 | +fsck_dataset_generator = dataset_factory() |
| 42 | + |
| 43 | + |
| 44 | +@broker.task(schedule=[{'cron': '*/15 * * * *'}]) |
| 45 | +def gc_dataset(dataset_path=None): |
| 46 | + """Run git gc on a random dataset periodically.""" |
| 47 | + try: |
| 48 | + if not dataset_path: |
| 49 | + dataset_path = next(gc_dataset_generator) |
| 50 | + except StopIteration: |
| 51 | + logging.info('No datasets available for git gc.') |
| 52 | + return |
| 53 | + |
| 54 | + logging.info(f'Running git gc on dataset: {dataset_path}') |
| 55 | + |
| 56 | + gc = subprocess.run( |
| 57 | + ['git', 'gc', '--cruft', '--prune=1.day.ago'], |
| 58 | + cwd=dataset_path, |
| 59 | + capture_output=True, |
| 60 | + text=True, |
| 61 | + ) |
| 62 | + if gc.returncode != 0: |
| 63 | + logging.error(f'`git gc` failed for `{dataset_path}`: {gc.stderr}') |
| 64 | + |
| 65 | + |
| 66 | +@broker.task(schedule=[{'cron': '7 * * * *'}]) |
| 67 | +def git_fsck_dataset(dataset_path=None): |
| 68 | + """Routinely verify git repository integrity and produce an error if any issues are reported.""" |
| 69 | + try: |
| 70 | + if not dataset_path: |
| 71 | + dataset_path = next(fsck_dataset_generator) |
| 72 | + except StopIteration: |
| 73 | + logging.info('No datasets available for git fsck.') |
| 74 | + return |
| 75 | + git_fsck = subprocess.run( |
| 76 | + ['git', 'fsck', '--full'], cwd=dataset_path, capture_output=True, text=True |
| 77 | + ) |
| 78 | + if git_fsck.returncode != 0: |
| 79 | + logging.error( |
| 80 | + f'`git fsck --full` failed for `{dataset_path}`: {git_fsck.stderr}' |
| 81 | + ) |
0 commit comments