Skip to content

Commit f654c89

Browse files
authored
Merge pull request #2213 from lonvia/fix-unavailable-state-info
Replication: guess date from file when state info is not available
2 parents c1862b9 + e934f20 commit f654c89

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

scripts/osm2pgsql-replication

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ except ImportError:
4545
try:
4646
from osmium.replication.server import ReplicationServer
4747
from osmium.replication.utils import get_replication_header
48+
from osmium.replication import newest_change_from_file
4849
from osmium import WriteHandler
4950
except ImportError:
5051
missing_modules.append('osmium')
@@ -208,7 +209,8 @@ class Osm2pgsqlProperties:
208209
def write_replication_state(self, base_url, seq, date):
209210
self._set_prop('replication_base_url', base_url)
210211
self._set_prop('replication_sequence_number', seq)
211-
self._set_prop('replication_timestamp', osm_date(date))
212+
if date is not None:
213+
self._set_prop('replication_timestamp', osm_date(date))
212214
self.db.conn.commit()
213215

214216

@@ -553,19 +555,25 @@ def update(props, args):
553555
seq = endseq
554556

555557
nextstate = repl.get_state_info(seq)
556-
timestamp = nextstate.timestamp if nextstate else None
558+
if nextstate:
559+
timestamp = nextstate.timestamp
560+
else:
561+
# Can't get state information for some reason, get the timestamp from file.
562+
timestamp = newest_change_from_file(str(outfile))
563+
if timestamp <= dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc):
564+
timestamp = None
557565

558566
if args.post_processing:
559567
cmd = [args.post_processing, str(endseq), str(timestamp or '')]
560568
LOG.debug('Calling post-processing script: %s', ' '.join(cmd))
561569
subprocess.run(cmd, check=True)
562570

563-
props.write_replication_state(base_url, seq, nextstate.timestamp if nextstate else None)
571+
props.write_replication_state(base_url, seq, timestamp)
564572

565-
if nextstate is not None:
573+
if timestamp is not None:
566574
LOG.info("Data imported until %s. Backlog remaining: %s",
567-
osm_date(nextstate.timestamp.astimezone(dt.timezone.utc)),
568-
pretty_format_timedelta((dt.datetime.now(dt.timezone.utc) - nextstate.timestamp).total_seconds()),
575+
osm_date(timestamp.astimezone(dt.timezone.utc)),
576+
pretty_format_timedelta((dt.datetime.now(dt.timezone.utc) - timestamp).total_seconds()),
569577
)
570578

571579
if args.once:

0 commit comments

Comments
 (0)