Skip to content

Commit 32aab17

Browse files
committed
Try to fix lambda handler not finding sqs handler function
1 parent adf54a6 commit 32aab17

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed
Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import pathlib
34
import typing
45

@@ -14,36 +15,38 @@
1415
type RecordType = dict[str, typing.Any]
1516

1617
SQSEventType = typing.TypedDict("SQSEventType", {"Records": list[RecordType]})
17-
WorkerType = typing.Callable[[chalice.app.Chalice, RecordType], dict[str, typing.Any]]
18+
WorkerType = typing.Callable[[RecordType], dict[str, typing.Any]]
19+
20+
logger = logging.getLogger(__name__)
21+
workers: dict[str, WorkerType] = {}
22+
23+
for _workers in typing.cast(
24+
list[list[WorkerType]],
25+
import_util.auto_import_patterns(pattern="workers", file_prefix="", dir=pathlib.Path(__file__).parent),
26+
):
27+
_func_names = {worker.__name__ for worker in _workers}
28+
if _duplicated := _func_names & workers.keys():
29+
raise ValueError(f"Worker {_duplicated} is already registered")
30+
31+
workers.update({worker.__name__: worker for worker in _workers})
32+
33+
34+
def _sqs_handler(event: chalice.app.SQSEvent) -> list[dict[str, typing.Any]]:
35+
parsed_event: SQSEventType = event.to_dict()
36+
logger.info(f"{parsed_event=}")
37+
38+
results: list[dict[str, typing.Any]] = []
39+
for record in parsed_event["Records"]:
40+
try:
41+
worker_name = json.loads(record["body"])["worker"]
42+
results.append(workers[worker_name](record))
43+
except Exception as e:
44+
logger.error(f"Failed to handle event: {record}", exc_info=e)
45+
results.append({"error": "Failed to handle event"})
46+
47+
logger.info(f"{results=}")
48+
return results
1849

1950

2051
def register_worker(app: chalice.app.Chalice) -> None:
21-
workers: dict[str, WorkerType] = {}
22-
23-
for _workers in typing.cast(
24-
list[list[WorkerType]],
25-
import_util.auto_import_patterns(pattern="workers", file_prefix="", dir=pathlib.Path(__file__).parent),
26-
):
27-
_func_names = {worker.__name__ for worker in _workers}
28-
if _duplicated := _func_names & workers.keys():
29-
raise ValueError(f"Worker {_duplicated} is already registered")
30-
31-
workers.update({worker.__name__: worker for worker in _workers})
32-
33-
@app.on_sqs_message(queue=config_module.config.infra.queue_name)
34-
def sqs_handler(event: chalice.app.SQSEvent) -> list[dict[str, typing.Any]]:
35-
parsed_event: SQSEventType = event.to_dict()
36-
app.log.info(f"{parsed_event=}")
37-
38-
results: list[dict[str, typing.Any]] = []
39-
for record in parsed_event["Records"]:
40-
try:
41-
worker_name = json.loads(record["body"])["worker"]
42-
result = workers[worker_name](app, record)
43-
results.append(result)
44-
except Exception as e:
45-
app.log.error(f"Failed to handle event: {record}", exc_info=e)
46-
results.append({"error": "Failed to handle event"})
47-
48-
app.log.info(f"{results=}")
49-
return results
52+
app.on_sqs_message(queue=config_module.config.infra.queue_name)(_sqs_handler)

runtime/chalicelib/worker/notification_sender.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import functools
22
import typing
33

4-
import chalice.app
54
import chalicelib.send_manager as send_manager
65
import chalicelib.send_manager.__interface__ as send_mgr_interface
76
import pydantic
@@ -42,7 +41,7 @@ def send(self) -> dict[str, str]:
4241
return self.send_manager.send(self.send_request_payload)
4342

4443

45-
def notification_sender(app: chalice.app.Chalice, record: RecordType) -> dict[str, str]:
44+
def notification_sender(record: RecordType) -> dict[str, str]:
4645
return WorkerPayload.model_validate_json(record["body"]).send()
4746

4847

runtime/chalicelib/worker/test_worker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import typing
22

3-
import chalice.app
4-
53
if typing.TYPE_CHECKING:
64
import mypy_boto3_sqs.type_defs
75

@@ -10,7 +8,7 @@
108
type RecordType = dict[str, typing.Any]
119

1210

13-
def test_handler(app: chalice.app.Chalice, record: RecordType) -> RecordType:
11+
def test_handler(record: RecordType) -> RecordType:
1412
print(record)
1513
return record
1614

0 commit comments

Comments
 (0)