From 934d6ee170256575655d2884d2f1ffb6a04ba198 Mon Sep 17 00:00:00 2001 From: index-git Date: Tue, 15 Nov 2022 13:37:18 +0100 Subject: [PATCH] Encapsulate second try in layer.db.refresh_table --- src/layman/layer/db/tasks.py | 73 +++++++++++++++--------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/src/layman/layer/db/tasks.py b/src/layman/layer/db/tasks.py index c7d1650c4..26c18358e 100644 --- a/src/layman/layer/db/tasks.py +++ b/src/layman/layer/db/tasks.py @@ -44,49 +44,36 @@ def refresh_table( main_filepath = main_filepaths[0] table_name = db.get_table_name(workspace, layername) - process = db.import_layer_vector_file_async(workspace, table_name, main_filepath, crs_id) - while process.poll() is None and not self.is_aborted(): - pass - if self.is_aborted(): - logger.info(f'terminating {workspace} {layername}') - process.terminate() - logger.info(f'terminating {workspace} {layername}') - table.delete_layer(workspace, layername) - raise AbortedException - return_code = process.poll() - output = process.stdout.read() - if return_code != 0 or output: - info = table.get_layer_info(workspace, layername) - if not info: - pg_error = str(output) - logger.error(f"STDOUT: {pg_error}") - if "ERROR: zero-length delimited identifier at or near" in pg_error: - err_code = 28 - raise LaymanError(err_code, private_data=pg_error) - if 'ERROR: invalid byte sequence for encoding "UTF8":' in pg_error: - processes = db.import_layer_vector_file_async_with_iconv(workspace, table_name, main_filepath, crs_id) - _, sterr = processes[-1].communicate() - if sterr: - logger.error(f"STDOUT: {str(sterr)}") - if self.is_aborted(): - logger.info(f'terminating {workspace} {layername}') - for proc in processes: - proc.terminate() - logger.info(f'terminating {workspace} {layername}') - table.delete_layer(workspace, layername) - raise AbortedException - return_code = process.poll() - output = process.stdout.read() - if return_code != 0 or output: - info = table.get_layer_info(workspace, layername) - if not info: - pg_error = str(output) - logger.error(f"STDOUT: {pg_error}") - err_code = 11 - raise LaymanError(err_code, private_data=pg_error) - else: - err_code = 11 - raise LaymanError(err_code, private_data=pg_error) + for try_num in [1, 2]: + if try_num == 1: + processes = [db.import_layer_vector_file_async(workspace, table_name, main_filepath, crs_id)] + elif try_num == 2: + processes = db.import_layer_vector_file_async_with_iconv(workspace, table_name, main_filepath, crs_id) + process = processes[-1] + stdout, stderr = process.communicate() + return_code = process.poll() + if self.is_aborted(): + logger.info(f'terminating {workspace} {layername}') + for proc in processes: + proc.terminate() + logger.info(f'deleting {workspace} {layername}') + table.delete_layer(workspace, layername) + raise AbortedException + if return_code != 0 or stdout or stderr: + info = table.get_layer_info(workspace, layername) + if not info: + str_error = str(stderr) + str_out = str(stdout) + logger.error(f"STDOUT: {str(stdout)}") + logger.error(f"STDERR: {str_error}") + if "ERROR: zero-length delimited identifier at or near" in str_out: + err_code = 28 + elif 'ERROR: invalid byte sequence for encoding "UTF8":' in str_out: + continue + else: + err_code = 11 + raise LaymanError(err_code, private_data=str_error) + break crs = db.get_crs(workspace, table_name) if crs_def.CRSDefinitions[crs].srid: