Skip to content

Commit

Permalink
added async backup with create snapshot task
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerlf1 committed Jul 2, 2023
1 parent a08b87f commit d1e35fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
20 changes: 11 additions & 9 deletions gce_rescue/tasks/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ def _list_tasks(vm: Instance, action: str) -> List:
'vm': vm
}]
},
{
'name': take_snapshot,
'args': [{
'vm': vm
}]
},
{
'name': create_rescue_disk,
'args': [{
Expand Down Expand Up @@ -127,9 +121,13 @@ def _list_tasks(vm: Instance, action: str) -> List:
def call_tasks(vm: Instance, action: str) -> None:
""" Loop tasks dict and execute """
tasks = _list_tasks(vm = vm, action = action)
if get_config('skip-snapshot'):
_logger.info(f'Skipping snapshot backup.')
tasks = [task for task in tasks if task['name'].__name__ != 'take_snapshot']
async_backup_thread = None
if action == 'set_rescue_mode':
if get_config('skip-snapshot'):
_logger.info(f'Skipping snapshot backup.')
else:
async_backup_thread = take_snapshot(vm)
async_backup_thread.start()
total_tasks = len(tasks)

tracker = Tracker(total_tasks)
Expand All @@ -139,6 +137,10 @@ def call_tasks(vm: Instance, action: str) -> None:
execute = task['name']
args = task['args'][0]

if task['name'].__name__ == 'start_instance' and async_backup_thread:
_logger.info(f'Waiting for async backup to finish before starting instance in rescue mode..')
async_backup_thread.join()

execute(**args)
tracker.advance(step = 1)

Expand Down
5 changes: 3 additions & 2 deletions gce_rescue/tasks/disks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from typing import Dict
import logging
from threading import Thread

import googleapiclient.errors

Expand Down Expand Up @@ -178,8 +179,8 @@ def _detach_disk(vm, disk: str) -> Dict:
return result


def take_snapshot(vm) -> None:
create_snapshot(vm)
def take_snapshot(vm) -> Thread:
return Thread(target=create_snapshot, args=(vm,), daemon=True)


def create_rescue_disk(vm) -> None:
Expand Down

0 comments on commit d1e35fc

Please sign in to comment.