Skip to content

Commit c2c1e88

Browse files
committed
Add ability to track import errors in osm.pgosm_flex table.
1 parent 39ad866 commit c2c1e88

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

docker/db.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,24 @@ def fix_pg_dump_create_public(export_path):
616616
export_path)
617617
LOGGER.debug('Completed replacement to not fail when public schema exists')
618618
LOGGER.debug(result)
619+
620+
621+
def log_import_message(import_uuid, msg):
622+
"""Logs msg to database in osm.pgosm_flex for import_uuid.
623+
624+
Parameters
625+
-------------------------------
626+
import_uuid : uuid
627+
msg : str
628+
"""
629+
sql_raw = """
630+
UPDATE osm.pgosm_flex
631+
SET import_status = %(msg)s
632+
WHERE import_uuid = %(import_uuid)s
633+
;
634+
"""
635+
with get_db_conn(conn_string=os.environ['PGOSM_CONN']) as conn:
636+
params = {'import_uuid': str(import_uuid), 'msg': msg}
637+
cur = conn.cursor()
638+
cur.execute(sql_raw, params=params)
639+

docker/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def verify_checksum(md5_file, path):
8282

8383

8484
def set_env_vars(region, subregion, srid, language, pgosm_date, layerset,
85-
layerset_path, sp_gist, replication):
85+
layerset_path, sp_gist, replication, import_uuid):
8686
"""Sets environment variables needed by PgOSM Flex.
8787
8888
See /docs/MANUAL-STEPS-RUN.md for usage examples of environment variables.
@@ -101,13 +101,16 @@ def set_env_vars(region, subregion, srid, language, pgosm_date, layerset,
101101
When `True` uses SP-GIST index instead of GIST for spatial indexes.
102102
replication : bool
103103
Indicates when osm2pgsql-replication is used
104+
import_uuid : uuid
105+
Required to track import failures in Postgres between Python and Lua
104106
"""
105107
logger = logging.getLogger('pgosm-flex')
106108
logger.debug('Ensuring env vars are not set from prior run')
107109
unset_env_vars()
108110
logger.debug('Setting environment variables')
109111

110112
os.environ['PGOSM_REGION'] = region
113+
os.environ['PGOSM_IMPORT_UUID'] = str(import_uuid)
111114

112115
if subregion is None:
113116
pgosm_region = f'{region}'
@@ -149,6 +152,7 @@ def set_env_vars(region, subregion, srid, language, pgosm_date, layerset,
149152
os.environ['PGOSM_REPLICATION'] = 'false'
150153

151154

155+
152156
def unset_env_vars():
153157
"""Unsets environment variables used by PgOSM Flex.
154158
@@ -166,3 +170,4 @@ def unset_env_vars():
166170
os.environ.pop('PGOSM_CONN_PG', None)
167171
os.environ.pop('PGOSM_GIST_TYPE', None)
168172
os.environ.pop('PGOSM_REPLICATION', None)
173+
os.environ.pop('PGOSM_IMPORT_UUID', None)

docker/pgosm_flex.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import sys
1515

1616
import click
17+
import uuid
1718

1819
import osm2pgsql_recommendation as rec
1920
import db
@@ -102,8 +103,10 @@ def run_pgosm_flex(ram, region, subregion, data_only, debug,
102103
if region is None and input_file:
103104
region = input_file
104105

106+
import_uuid = uuid.uuid4()
105107
helpers.set_env_vars(region, subregion, srid, language, pgosm_date,
106-
layerset, layerset_path, sp_gist, replication)
108+
layerset, layerset_path, sp_gist, replication,
109+
import_uuid)
107110
db.wait_for_postgres()
108111

109112
if replication:
@@ -140,6 +143,12 @@ def run_pgosm_flex(ram, region, subregion, data_only, debug,
140143
skip_nested=skip_nested,
141144
import_mode=import_mode)
142145

146+
if not success:
147+
msg = 'PgOSM Flex completed with errors. Details in output'
148+
db.log_import_message(import_uuid=import_uuid, msg='Import failed')
149+
logger.warning(msg)
150+
sys.exit(msg)
151+
143152
if schema_name != 'osm':
144153
db.rename_schema(schema_name)
145154

@@ -149,10 +158,7 @@ def run_pgosm_flex(ram, region, subregion, data_only, debug,
149158
data_only=data_only,
150159
schema_name=schema_name)
151160

152-
if success:
153-
logger.info('PgOSM Flex complete!')
154-
else:
155-
logger.warning('PgOSM Flex completed with errors. Details in output')
161+
logger.info('PgOSM Flex complete!')
156162

157163

158164
def run_osm2pgsql_standard(input_file, out_path, flex_path, ram, skip_nested,

flex-config/sql/pgosm-meta.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ COMMENT ON COLUMN osm.pgosm_flex.region IS 'Region specified at run time via env
1212
COMMENT ON COLUMN osm.pgosm_flex.language IS 'Preferred language specified at run time via env var PGOSM_LANGUAGE. Empty string when not defined.';
1313
COMMENT ON COLUMN osm.pgosm_flex.osm2pgsql_mode IS 'Indicates which osm2pgsql mode was used, create or append.';
1414
COMMENT ON COLUMN osm.pgosm_flex.osm2pgsql_replication IS 'True indicates when osm2pgsql-replication was used for the import.';
15-
15+
COMMENT ON COLUMN osm.pgosm_flex.import_uuid IS 'Used during import to set import_status on failure.';
16+
COMMENT ON COLUMN osm.pgosm_flex.import_status IS 'Set when import failure is detected.';
1617

1718
-- Helper Procedures for allowing updates via osm2pgsql-replication or similar
1819

flex-config/style/pgosm-meta.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ else
2020
error('ENV VAR PGOSM_REPLICATION must be set')
2121
end
2222

23+
local import_uuid = os.getenv("PGOSM_IMPORT_UUID")
2324

2425
local tables = {}
2526

@@ -37,6 +38,8 @@ CREATE TABLE IF NOT EXISTS osm.pgosm_flex (
3738
"language" text NOT NULL,
3839
osm2pgsql_mode TEXT NOT NULL DEFAULT 'create',
3940
osm2pgsql_replication BOOLEAN NOT NULL DEFAULT False,
41+
import_uuid TEXT NULL,
42+
import_status TEXT NULL,
4043
CONSTRAINT pk_osm_pgosm_flex PRIMARY KEY (id)
4144
);
4245
]=]
@@ -50,6 +53,12 @@ ALTER TABLE osm.pgosm_flex
5053
ALTER TABLE osm.pgosm_flex
5154
ADD COLUMN IF NOT EXISTS osm2pgsql_replication
5255
BOOLEAN NOT NULL DEFAULT False;
56+
57+
ALTER TABLE osm.pgosm_flex
58+
ADD COLUMN IF NOT EXISTS import_uuid TEXT NULL;
59+
60+
ALTER TABLE osm.pgosm_flex
61+
ADD COLUMN IF NOT EXISTS import_status TEXT;
5362
]=]
5463

5564

@@ -100,7 +109,7 @@ end
100109

101110
local sql_insert = [[ INSERT INTO osm.pgosm_flex (osm_date, default_date, region,
102111
pgosm_flex_version, srid, project_url, osm2pgsql_version, "language",
103-
osm2pgsql_mode, osm2pgsql_replication) ]] ..
112+
osm2pgsql_mode, osm2pgsql_replication, import_uuid) ]] ..
104113
[[ VALUES (']] ..
105114
con:escape(pgosm_date) .. [[', ]] ..
106115
default_date_str .. [[ , ']] .. -- special handling for boolean
@@ -111,7 +120,8 @@ local sql_insert = [[ INSERT INTO osm.pgosm_flex (osm_date, default_date, region
111120
con:escape(osm2pgsql_version) .. [[', ']] ..
112121
con:escape(pgosm_language) .. [[', ']] ..
113122
con:escape(osm2pgsql_mode) .. [[', ]] ..
114-
pgosm_replication .. [[ );]]
123+
pgosm_replication .. [[ , ']] ..
124+
con:escape(import_uuid) .. [[' );]]
115125

116126

117127
-- simple query to verify connection

0 commit comments

Comments
 (0)