Skip to content

Commit

Permalink
feat: enhance cloud check in PostgresLoader to work in any GCP enviro…
Browse files Browse the repository at this point in the history
…nment

Prior to this change, it only worked in functions.
  • Loading branch information
stdavis authored and jacobdadams committed Oct 4, 2024
1 parent ae883cb commit 05748f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/palletjack/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import json
import logging
import mimetypes
import os
import random
import re
import time
Expand Down Expand Up @@ -500,7 +499,7 @@ def __init__(self, host, database, username, password, port=5432):
"""

self._class_logger = logging.getLogger(__name__).getChild(self.__class__.__name__)
if os.environ.get("FUNCTION_TARGET") is not None: #: this is an env var specific to cloud functions
if utils.is_running_in_gcp():
self._class_logger.info("running in GCF, using unix socket")
self.engine = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL.create(
Expand Down
16 changes: 16 additions & 0 deletions src/palletjack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,19 @@ def chunker(sequence, chunk_size):
"""

return (sequence[position : position + chunk_size] for position in range(0, len(sequence), chunk_size))


def is_running_in_gcp() -> bool:
"""Check if the code is running in a GCP environment
Returns:
bool: True if running in a GCP environment, False otherwise
"""

#: check if the metadata server is available
try:
requests.get("http://metadata.google.internal", headers={"Metadata-Flavor": "Google"}, timeout=3)
except requests.exceptions.ConnectionError:
return False

return True

0 comments on commit 05748f2

Please sign in to comment.