2 Functions for updating a database from a replication source.
9 from ..db import status
10 from .exec_utils import run_osm2pgsql
11 from ..errors import UsageError
14 from osmium.replication.server import ReplicationServer
15 from osmium import WriteHandler
16 except ModuleNotFoundError as exc:
17 logging.getLogger().fatal("pyosmium not installed. Replication functions not available.\n"
18 "To install pyosmium via pip: pip3 install osmium")
19 raise UsageError("replication tools not available") from exc
21 LOG = logging.getLogger()
23 def init_replication(conn, base_url):
24 """ Set up replication for the server at the given base URL.
26 LOG.info("Using replication source: %s", base_url)
27 date = status.compute_database_date(conn)
29 # margin of error to make sure we get all data
30 date -= dt.timedelta(hours=3)
32 repl = ReplicationServer(base_url)
34 seq = repl.timestamp_to_sequence(date)
37 LOG.fatal("Cannot reach the configured replication service '%s'.\n"
38 "Does the URL point to a directory containing OSM update data?",
40 raise UsageError("Failed to reach replication service")
42 status.set_status(conn, date=date, seq=seq)
44 LOG.warning("Updates intialised at sequence %s (%s)", seq, date)
47 def check_for_updates(conn, base_url):
48 """ Check if new data is available from the replication service at the
51 _, seq, _ = status.get_status(conn)
54 LOG.error("Replication not set up. "
55 "Please run 'nominatim replication --init' first.")
58 state = ReplicationServer(base_url).get_state_info()
61 LOG.error("Cannot get state for URL %s.", base_url)
64 if state.sequence <= seq:
65 LOG.warning("Database is up to date.")
68 LOG.warning("New data available (%i => %i).", seq, state.sequence)
71 class UpdateState(Enum):
72 """ Possible states after an update has run.
80 def update(conn, options):
81 """ Update database from the next batch of data. Returns the state of
82 updates according to `UpdateState`.
84 startdate, startseq, indexed = status.get_status(conn)
87 LOG.error("Replication not set up. "
88 "Please run 'nominatim replication --init' first.")
89 raise UsageError("Replication not set up.")
91 if not indexed and options['indexed_only']:
92 LOG.info("Skipping update. There is data that needs indexing.")
93 return UpdateState.MORE_PENDING
95 last_since_update = dt.datetime.now(dt.timezone.utc) - startdate
96 update_interval = dt.timedelta(seconds=options['update_interval'])
97 if last_since_update < update_interval:
98 duration = (update_interval - last_since_update).seconds
99 LOG.warning("Sleeping for %s sec before next update.", duration)
102 if options['import_file'].exists():
103 options['import_file'].unlink()
105 # Read updates into file.
106 repl = ReplicationServer(options['base_url'])
108 outhandler = WriteHandler(str(options['import_file']))
109 endseq = repl.apply_diffs(outhandler, startseq + 1,
110 max_size=options['max_diff_size'] * 1024)
114 return UpdateState.NO_CHANGES
116 # Consume updates with osm2pgsql.
117 options['append'] = True
118 run_osm2pgsql(options)
120 # Write the current status to the file
121 endstate = repl.get_state_info(endseq)
122 status.set_status(conn, endstate.timestamp, seq=endseq, indexed=False)
124 return UpdateState.UP_TO_DATE