|
30 | 30 | from .__main__ import Patroni |
31 | 31 | from .dcs import Cluster |
32 | 32 | from .exceptions import PostgresConnectionException, PostgresException |
33 | | -from .postgresql.misc import postgres_version_to_int, PostgresqlRole, PostgresqlState |
| 33 | +from .postgresql.misc import postgres_version_to_int, PostgresqlState |
34 | 34 | from .utils import cluster_as_json, deep_compare, enable_keepalive, parse_bool, \ |
35 | 35 | parse_int, patch_config, Retry, RetryFailedError, split_host_port, tzutc, uri |
36 | 36 |
|
@@ -323,7 +323,7 @@ def do_GET(self, write_status_code_only: bool = False) -> None: |
323 | 323 | is_lagging = leader_optime and leader_optime > replayed_location + max_replica_lag |
324 | 324 |
|
325 | 325 | replica_status_code = 200 if not patroni.noloadbalance and not is_lagging and \ |
326 | | - response.get('role') == PostgresqlRole.REPLICA and response.get('state') == PostgresqlState.RUNNING else 503 |
| 326 | + response.get('role') == 'replica' and response.get('state') == PostgresqlState.RUNNING else 503 |
327 | 327 |
|
328 | 328 | if not cluster and response.get('pause'): |
329 | 329 | leader_status_code = 200 if response.get('role') in (PostgresqlRole.PRIMARY, |
@@ -359,7 +359,7 @@ def do_GET(self, write_status_code_only: bool = False) -> None: |
359 | 359 | elif 'read-only' in path and 'sync' not in path and 'quorum' not in path: |
360 | 360 | status_code = 200 if 200 in (primary_status_code, standby_leader_status_code) else replica_status_code |
361 | 361 | elif 'health' in path: |
362 | | - status_code = 200 if response.get('state') == 'running' else 503 |
| 362 | + status_code = 200 if response.get('state') == PostgresqlState.RUNNING else 503 |
363 | 363 | elif cluster: # dcs is available |
364 | 364 | is_quorum = response.get('quorum_standby') |
365 | 365 | is_synchronous = response.get('sync_standby') |
@@ -463,7 +463,7 @@ def do_GET_readiness(self) -> None: |
463 | 463 | patroni = self.server.patroni |
464 | 464 | if patroni.ha.is_leader(): |
465 | 465 | status_code = 200 |
466 | | - elif patroni.postgresql.state == 'running': |
| 466 | + elif patroni.postgresql.state == PostgresqlState.RUNNING: |
467 | 467 | status_code = 200 if patroni.dcs.cluster else 503 |
468 | 468 | else: |
469 | 469 | status_code = 503 |
@@ -573,7 +573,8 @@ def do_GET_metrics(self) -> None: |
573 | 573 |
|
574 | 574 | metrics.append("# HELP patroni_postgres_running Value is 1 if Postgres is running, 0 otherwise.") |
575 | 575 | metrics.append("# TYPE patroni_postgres_running gauge") |
576 | | - metrics.append("patroni_postgres_running{0} {1}".format(labels, int(postgres['state'] == 'running'))) |
| 576 | + metrics.append("patroni_postgres_running{0} {1}".format( |
| 577 | + labels, int(postgres['state'] == PostgresqlState.RUNNING))) |
577 | 578 |
|
578 | 579 | metrics.append("# HELP patroni_postmaster_start_time Epoch seconds since Postgres started.") |
579 | 580 | metrics.append("# TYPE patroni_postmaster_start_time gauge") |
@@ -899,7 +900,7 @@ def do_POST_restart(self) -> None: |
899 | 900 | If it's not able to parse the request body, then the request is silently discarded. |
900 | 901 | """ |
901 | 902 | status_code = 500 |
902 | | - data = 'restart failed' |
| 903 | + data = PostgresqlState.RESTART_FAILED |
903 | 904 | request = self._read_json_content(body_is_optional=True) |
904 | 905 | cluster = self.server.patroni.dcs.get_cluster() |
905 | 906 | if request is None: |
@@ -1297,10 +1298,7 @@ def get_postgresql_status(self, retry: bool = False) -> Dict[str, Any]: |
1297 | 1298 |
|
1298 | 1299 | :returns: a dict with the status of Postgres/Patroni. The keys are: |
1299 | 1300 |
|
1300 | | - * ``state``: Postgres state among ``stopping``, ``stopped``, ``stop failed``, ``crashed``, ``running``, |
1301 | | - ``starting``, ``start failed``, ``restarting``, ``restart failed``, ``initializing new cluster``, |
1302 | | - ``initdb failed``, ``running custom bootstrap script``, ``custom bootstrap failed``, |
1303 | | - ``creating replica``, or ``unknown``; |
| 1301 | + * ``state``: one of :class:`~patroni.postgresql.misc.PostgresqlState` or ``unknown``; |
1304 | 1302 | * ``postmaster_start_time``: ``pg_postmaster_start_time()``; |
1305 | 1303 | * ``role``: :class:`~patroni.postgresql.misc.PostgresqlRole.REPLICA` or |
1306 | 1304 | :class:`~patroni.postgresql.misc.PostgresqlRole.PRIMARY` based on ``pg_is_in_recovery()`` output; |
@@ -1342,7 +1340,8 @@ def get_postgresql_status(self, retry: bool = False) -> Dict[str, Any]: |
1342 | 1340 | config = global_config.from_cluster(cluster) |
1343 | 1341 | try: |
1344 | 1342 |
|
1345 | | - if postgresql.state not in ('running', 'restarting', 'starting'): |
| 1343 | + if postgresql.state not in (PostgresqlState.RUNNING, PostgresqlState.RESTARTING, |
| 1344 | + PostgresqlState.STARTING): |
1346 | 1345 | raise RetryFailedError('') |
1347 | 1346 | replication_state = ('(pg_catalog.pg_stat_get_wal_receiver()).status' |
1348 | 1347 | if postgresql.major_version >= 90600 else 'NULL') + ", " +\ |
@@ -1393,7 +1392,7 @@ def get_postgresql_status(self, retry: bool = False) -> Dict[str, Any]: |
1393 | 1392 |
|
1394 | 1393 | except (psycopg.Error, RetryFailedError, PostgresConnectionException): |
1395 | 1394 | state = postgresql.state |
1396 | | - if state == 'running': |
| 1395 | + if state == PostgresqlState.RUNNING: |
1397 | 1396 | logger.exception('get_postgresql_status') |
1398 | 1397 | state = 'unknown' |
1399 | 1398 | result: Dict[str, Any] = {'state': state, 'role': postgresql.role} |
|
0 commit comments