I am working on a project in charge of uploading the files everyday from a database to a drive in OneDrive. It happens that for a strange reason when you have uploaded half the files it suddenly fails. I wanted to know if there's a way to implement a 'retry' function written in python that when the file upload process fails, it executes and tries again three times and if it fails after the three times ends with some message like "The uploading process of today failed". Any kind of help is welcome. Python version **3.8.3
the code that performs the entire upload process is as follows
if __name__ == '__main__':
reg_manager = RegistryManager()
keys = reg_manager.get_keys()
try:
tables = BCPManager.load_tables()
dir_name = datetime.today().strftime('%Y-%m-%d')
dir_path = f'./files/{dir_name}'
final_file = 'tables.zip'
FileManager.create_folder(dir_path)
for table in tables:
table_columns = BCPManager.get_table_columns(table, dir_path)
table_data = BCPManager.get_table_data(table, dir_path, 3)
table_path = f'{dir_path}/{table}.csv'
FileManager.merge_table_files(table_path, table_columns, table_data)
dir_files = FileManager.get_directory_files(dir_path)
with ZipFile(
f'{dir_path}/{final_file}',
'w',
compression=ZIP_DEFLATED,
compresslevel=9
) as zip_file:
for dir_file in dir_files:
if dir_file.name == final_file:
continue
print(f'zipping file {dir_file.path}')
zip_file.write(dir_file.path, arcname=dir_file.name)
FileManager.remove_file(dir_file.path)
file_size = os.path.getsize(f'{dir_path}/{final_file}')
uploader = OneDriveUploader(
'drives/{DriveID}/items/{ChildreID}',
{
'client_id': keys['client_id'],
'client_secret': keys['client_secret']
}
)
uploader.create_folder(dir_name)
print('uploading file')
with open(f'{dir_path}/{final_file}', 'rb') as open_file:
print('getting file chunks')
chunks = FileManager.get_file_chunks(open_file)
index = 0
offset = 0
headers = dict()
upload_url = uploader.get_upload_link(dir_name, final_file)
for chunk in chunks:
offset = index + len(chunk)
headers['Content-Type'] = 'application/octet-stream'
headers['Content-Length'] = str(file_size)
headers['Content-Range'] = f'bytes {index}-{offset - 1}' \
f'/{file_size} '
uploader.upload_file_chunk(upload_url, chunk, headers)
index = offset
print('removing file')
FileManager.remove_file(f'{dir_path}/{final_file}')
except Exception as uploader_exception:
print('There was an error while uploading files')
print(uploader_exception)
message = Mail(
from_email='test@outlook.com',
to_emails=['test@gmail.com'],
subject='OneDrive upload failed',
html_content='Something went wrong while uploading files to '
'onedrive')
try:
sg = SendGridAPIClient(keys['sendgrid_api_key'])
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e)
When half the files have been uploaded it throws the following error:
<Response [202]> {'expirationDateTime': '2021-08-14T12:01:08.341Z', 'nextExpectedRanges': ['11141120-109593649']}
<Response [202]> {'expirationDateTime': '2021-08-14T12:01:08.341Z', 'nextExpectedRanges': ['11468800-109593649']}
<Response [202]> {'expirationDateTime': '2021-08-14T12:01:08.341Z', 'nextExpectedRanges': ['11796480-109593649']}
There was an error while uploading files
Expecting value: line 1 column 1 (char 0)
202
b''
Server: nginx
Date: Mon, 09 Aug 2021 12:01:33 GMT
Content-Length: 0
Connection: close
X-Message-Id: cMEuH6TtSHCmiKMKUzV1Dg
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
Strict-Transport-Security: max-age=600; includeSubDomains
Something curious about this is that it does not fail every day of the week, it fails occasionally once a week, that is why the idea of implementing a function that is responsible for retrying the upload of files, if it fails after three times of Tried, nothing happens because there's 6 days left.