|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import threading |
| 4 | +from time import sleep |
| 5 | +from typing import Dict, Optional |
| 6 | + |
| 7 | +from aleph_message.models import ItemHash |
| 8 | +from schedule import Job, Scheduler |
| 9 | + |
| 10 | +from .conf import settings |
| 11 | +from .models import VmExecution |
| 12 | +from .snapshots import CompressedDiskVolumeSnapshot |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def wrap_async_snapshot(execution): |
| 18 | + asyncio.run(do_execution_snapshot(execution)) |
| 19 | + |
| 20 | + |
| 21 | +def run_threaded_snapshot(execution): |
| 22 | + job_thread = threading.Thread(target=wrap_async_snapshot, args=(execution,)) |
| 23 | + job_thread.start() |
| 24 | + |
| 25 | + |
| 26 | +async def do_execution_snapshot(execution: VmExecution) -> CompressedDiskVolumeSnapshot: |
| 27 | + try: |
| 28 | + logger.debug(f"Starting new snapshot for VM {execution.vm_hash}") |
| 29 | + assert execution.vm, "VM execution not set" |
| 30 | + |
| 31 | + snapshot = await execution.vm.create_snapshot() |
| 32 | + await snapshot.upload() |
| 33 | + |
| 34 | + logger.debug( |
| 35 | + f"New snapshots for VM {execution.vm_hash} created in {snapshot.path}" |
| 36 | + ) |
| 37 | + return snapshot |
| 38 | + except ValueError: |
| 39 | + raise ValueError("Something failed taking an snapshot") |
| 40 | + |
| 41 | + |
| 42 | +def infinite_run_scheduler_jobs(scheduler: Scheduler) -> None: |
| 43 | + while True: |
| 44 | + scheduler.run_pending() |
| 45 | + sleep(1) |
| 46 | + |
| 47 | + |
| 48 | +class SnapshotExecution: |
| 49 | + vm_hash: ItemHash |
| 50 | + execution: VmExecution |
| 51 | + frequency: int |
| 52 | + _scheduler: Scheduler |
| 53 | + _job: Job |
| 54 | + |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + scheduler: Scheduler, |
| 58 | + vm_hash: ItemHash, |
| 59 | + execution: VmExecution, |
| 60 | + frequency: int, |
| 61 | + ): |
| 62 | + self.vm_hash = vm_hash |
| 63 | + self.execution = execution |
| 64 | + self.frequency = frequency |
| 65 | + self._scheduler = scheduler |
| 66 | + |
| 67 | + async def start(self) -> None: |
| 68 | + logger.debug( |
| 69 | + f"Starting snapshots for VM {self.vm_hash} every {self.frequency} minutes" |
| 70 | + ) |
| 71 | + job = self._scheduler.every(self.frequency).minutes.do( |
| 72 | + run_threaded_snapshot, self.execution |
| 73 | + ) |
| 74 | + self._job = job |
| 75 | + |
| 76 | + async def stop(self) -> None: |
| 77 | + logger.debug(f"Stopping snapshots for VM {self.vm_hash}") |
| 78 | + self._scheduler.cancel_job(self._job) |
| 79 | + |
| 80 | + |
| 81 | +class SnapshotManager: |
| 82 | + """ |
| 83 | + Manage VM snapshots. |
| 84 | + """ |
| 85 | + |
| 86 | + executions: Dict[ItemHash, SnapshotExecution] |
| 87 | + _scheduler: Scheduler |
| 88 | + |
| 89 | + def __init__(self): |
| 90 | + self.executions = {} |
| 91 | + self._scheduler = Scheduler() |
| 92 | + |
| 93 | + def run_snapshots(self) -> None: |
| 94 | + job_thread = threading.Thread( |
| 95 | + target=infinite_run_scheduler_jobs, |
| 96 | + args=[self._scheduler], |
| 97 | + daemon=True, |
| 98 | + name="SnapshotManager", |
| 99 | + ) |
| 100 | + job_thread.start() |
| 101 | + |
| 102 | + async def start_for( |
| 103 | + self, execution: VmExecution, frequency: Optional[int] = None |
| 104 | + ) -> None: |
| 105 | + if not execution.is_instance: |
| 106 | + raise TypeError("VM execution should be an Instance only") |
| 107 | + |
| 108 | + if not frequency: |
| 109 | + frequency = settings.SNAPSHOT_FREQUENCY |
| 110 | + |
| 111 | + vm_hash = execution.vm_hash |
| 112 | + snapshot_execution = SnapshotExecution( |
| 113 | + scheduler=self._scheduler, |
| 114 | + vm_hash=vm_hash, |
| 115 | + execution=execution, |
| 116 | + frequency=frequency, |
| 117 | + ) |
| 118 | + self.executions[vm_hash] = snapshot_execution |
| 119 | + await snapshot_execution.start() |
| 120 | + |
| 121 | + async def stop_for(self, vm_hash: ItemHash) -> None: |
| 122 | + if not self.executions[vm_hash]: |
| 123 | + raise ValueError(f"Snapshot execution not running for VM {vm_hash}") |
| 124 | + |
| 125 | + await self.executions[vm_hash].stop() |
| 126 | + |
| 127 | + async def stop_all(self) -> None: |
| 128 | + await asyncio.gather( |
| 129 | + *(self.stop_for(vm_hash) for vm_hash, execution in self.executions) |
| 130 | + ) |
0 commit comments