catch all exceptions and save de snapshot

This commit is contained in:
Cayo Puigdefabregas 2020-10-07 19:58:09 +02:00
parent e36256ee72
commit 0e89af1215
1 changed files with 60 additions and 53 deletions

View File

@ -1,5 +1,6 @@
import os import os
import json import json
import copy
from time import time from time import time
from distutils.version import StrictVersion from distutils.version import StrictVersion
from typing import List from typing import List
@ -28,11 +29,12 @@ def save_snapshot_in_file(snapshot_json):
This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory
The file need to be saved with one name format with the stamptime and uuid joins The file need to be saved with one name format with the stamptime and uuid joins
""" """
name_file = "{uuid}_{time}.json".format(uuid=snapshot_json['uuid'], time=int(time()))
path_name = "{tmp}/{file}".format(tmp=TMP_SNAPSHOTS, file=name_file)
if not os.path.isdir(TMP_SNAPSHOTS): if not os.path.isdir(TMP_SNAPSHOTS):
os.system('mkdir -p {}'.format(TMP_SNAPSHOTS)) os.system('mkdir -p {}'.format(TMP_SNAPSHOTS))
name_file = "{uuid}_{time}.json".format(uuid=snapshot_json['uuid'], time=int(time()))
path_name = "{tmp}/{file}".format(tmp=TMP_SNAPSHOTS, file=name_file)
snapshot_file = open(path_name, 'w') snapshot_file = open(path_name, 'w')
snapshot_file.write(json.dumps(snapshot_json)) snapshot_file.write(json.dumps(snapshot_json))
snapshot_file.close() snapshot_file.close()
@ -78,6 +80,8 @@ class ActionView(View):
# model object, when we flush them to the db we will flush # model object, when we flush them to the db we will flush
# snapshot, and we want to wait to flush snapshot at the end # snapshot, and we want to wait to flush snapshot at the end
snapshot_json_bakup = copy.copy(snapshot_json)
try:
device = snapshot_json.pop('device') # type: Computer device = snapshot_json.pop('device') # type: Computer
components = None components = None
if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid): if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid):
@ -134,6 +138,9 @@ class ActionView(View):
ret.status_code = 201 ret.status_code = 201
db.session.commit() db.session.commit()
return ret return ret
except Exception as err:
save_snapshot_in_file(snapshot_json_bakup)
raise err
def transfer_ownership(self): def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device""" """Perform a InitTransfer action to change author_id of device"""