Skip to content

Commit 277cd9f

Browse files
committed
fixed lint errors
1 parent 4abfc3d commit 277cd9f

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

api/src/database/database.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from database_gen.sqlacodegen_models import Base, Feed, Gtfsfeed, Gtfsrealtimefeed, Gbfsfeed
1111
from sqlalchemy.orm import sessionmaker
1212
import logging
13-
from typing import Final
1413

1514
lock = threading.Lock()
1615

@@ -76,7 +75,7 @@ class Database:
7675

7776
def __new__(cls, *args, **kwargs):
7877
if not isinstance(cls.instance, cls):
79-
with lock:
78+
with cls.lock:
8079
if not isinstance(cls.instance, cls):
8180
cls.instance = object.__new__(cls)
8281
return cls.instance

api/tests/integration/test_database.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pytest
55
from sqlalchemy.orm import Query
6-
import os
76

87
from database.database import Database, generate_unique_id
98
from database_gen.sqlacodegen_models import Feature, Gtfsdataset

functions-python/batch_process_dataset/src/main.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def create_dataset(self, dataset_file: DatasetFile):
239239
session.add(latest_dataset)
240240
session.add(new_dataset)
241241

242-
db.refresh_materialized_view(t_feedsearch.name) # todo: ????????
242+
db.refresh_materialized_view(session, t_feedsearch.name)
243243
session.commit()
244244
logging.info(f"[{self.feed_stable_id}] Dataset created successfully.")
245245
except Exception as e:
@@ -310,49 +310,52 @@ def process_dataset(cloud_event: CloudEvent):
310310
stable_id = "UNKNOWN"
311311
execution_id = "UNKNOWN"
312312
bucket_name = os.getenv("DATASETS_BUCKET_NANE")
313-
start_db_session(os.getenv("FEEDS_DATABASE_URL"))
314-
maximum_executions = os.getenv("MAXIMUM_EXECUTIONS", 1)
315-
public_hosted_datasets_url = os.getenv("PUBLIC_HOSTED_DATASETS_URL")
316-
trace_service = None
317-
dataset_file: DatasetFile = None
318-
error_message = None
313+
db = Database(database_url=os.getenv("FEEDS_DATABASE_URL"))
319314
try:
320-
# Extract data from message
321-
data = base64.b64decode(cloud_event.data["message"]["data"]).decode()
322-
json_payload = json.loads(data)
323-
logging.info(
324-
f"[{json_payload['feed_stable_id']}] JSON Payload: {json.dumps(json_payload)}"
325-
)
326-
stable_id = json_payload["feed_stable_id"]
327-
execution_id = json_payload["execution_id"]
328-
trace_service = DatasetTraceService()
329-
330-
trace = trace_service.get_by_execution_and_stable_ids(execution_id, stable_id)
331-
logging.info(f"[{stable_id}] Dataset trace: {trace}")
332-
executions = len(trace) if trace else 0
333-
logging.info(
334-
f"[{stable_id}] Dataset executed times={executions}/{maximum_executions} "
335-
f"in execution=[{execution_id}] "
336-
)
315+
with db.start_db_session():
316+
maximum_executions = os.getenv("MAXIMUM_EXECUTIONS", 1)
317+
public_hosted_datasets_url = os.getenv("PUBLIC_HOSTED_DATASETS_URL")
318+
trace_service = None
319+
dataset_file: DatasetFile = None
320+
error_message = None
321+
# Extract data from message
322+
data = base64.b64decode(cloud_event.data["message"]["data"]).decode()
323+
json_payload = json.loads(data)
324+
logging.info(
325+
f"[{json_payload['feed_stable_id']}] JSON Payload: {json.dumps(json_payload)}"
326+
)
327+
stable_id = json_payload["feed_stable_id"]
328+
execution_id = json_payload["execution_id"]
329+
trace_service = DatasetTraceService()
337330

338-
if executions > 0:
339-
if executions >= maximum_executions:
340-
error_message = f"[{stable_id}] Function already executed maximum times in execution: [{execution_id}]"
341-
logging.error(error_message)
342-
return error_message
343-
344-
processor = DatasetProcessor(
345-
json_payload["producer_url"],
346-
json_payload["feed_id"],
347-
stable_id,
348-
execution_id,
349-
json_payload["dataset_hash"],
350-
bucket_name,
351-
int(json_payload["authentication_type"]),
352-
json_payload["api_key_parameter_name"],
353-
public_hosted_datasets_url,
354-
)
355-
dataset_file = processor.process()
331+
trace = trace_service.get_by_execution_and_stable_ids(
332+
execution_id, stable_id
333+
)
334+
logging.info(f"[{stable_id}] Dataset trace: {trace}")
335+
executions = len(trace) if trace else 0
336+
logging.info(
337+
f"[{stable_id}] Dataset executed times={executions}/{maximum_executions} "
338+
f"in execution=[{execution_id}] "
339+
)
340+
341+
if executions > 0:
342+
if executions >= maximum_executions:
343+
error_message = f"[{stable_id}] Function already executed maximum times in execution: [{execution_id}]"
344+
logging.error(error_message)
345+
return error_message
346+
347+
processor = DatasetProcessor(
348+
json_payload["producer_url"],
349+
json_payload["feed_id"],
350+
stable_id,
351+
execution_id,
352+
json_payload["dataset_hash"],
353+
bucket_name,
354+
int(json_payload["authentication_type"]),
355+
json_payload["api_key_parameter_name"],
356+
public_hosted_datasets_url,
357+
)
358+
dataset_file = processor.process()
356359
except Exception as e:
357360
logging.error(e)
358361
error_message = f"[{stable_id}] Error execution: [{execution_id}] error: [{e}]"

functions-python/helpers/database.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
from contextlib import contextmanager
1818
import os
1919
import threading
20-
from typing import Final, Optional, TYPE_CHECKING
20+
from typing import Final, Optional
2121

2222
from sqlalchemy import create_engine, text
2323
from sqlalchemy.orm import sessionmaker
2424
import logging
2525

26-
if TYPE_CHECKING:
27-
from sqlalchemy.engine import Engine
28-
2926
DB_REUSE_SESSION: Final[str] = "DB_REUSE_SESSION"
3027

3128

@@ -55,7 +52,7 @@ class Database:
5552

5653
def __new__(cls, *args, **kwargs):
5754
if not isinstance(cls.instance, cls):
58-
with lock:
55+
with cls.lock:
5956
if not isinstance(cls.instance, cls):
6057
cls.instance = object.__new__(cls)
6158
return cls.instance

0 commit comments

Comments
 (0)