Skip to content

Commit e3ebe72

Browse files
committed
Optimization logic
1 parent 0d685a4 commit e3ebe72

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

packages/tencent-cloud-sdk-serverless-functions/tencent/cloud/serverless/functions/__init__.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,28 +966,41 @@ async def invoke_async(
966966
namespace_name, function_name, function_event,
967967
function_version, function_async)
968968

969-
def use_routine_framework() -> IntegrateDispatch:
969+
def use_routine_dispatcher(
970+
override_handler: bool = True
971+
) -> IntegrateDispatch:
970972
'''
971973
Use the routine dispatch framework for integrated
972974
invoke for current serverless cloud functions.
973975
974976
Note that this function will immediately add or override
975-
the main function at the source of the call.
977+
the main function at the source of the call, unless
978+
the parameter override_handler is set to False.
979+
980+
Args:
981+
override_handler: Whether to create or override the
982+
serverless cloud function main processing function
983+
at the source of this function call.
976984
977985
Returns:
978986
Returns an instance of the integrated invoke dispatcher.
979987
980988
Raises:
989+
ValueError: Parameter values are not as expected.
981990
ModuleNotFoundError: Can't fault the module object from which
982991
the current function call originated.
983992
'''
984993

994+
if override_handler == None or not isinstance(override_handler, bool):
995+
raise ValueError('<override_handler> value invalid')
996+
985997
integrate_dispatch: IntegrateDispatch = IntegrateDispatch()
986998

987-
try:
988-
module_name: str = inspect.currentframe().f_back.f_globals['__name__']
989-
setattr(sys.modules[module_name], 'main', integrate_dispatch.handler)
990-
except KeyError:
991-
raise ModuleNotFoundError('no call source module found')
999+
if override_handler:
1000+
try:
1001+
module_name: str = inspect.currentframe().f_back.f_globals['__name__']
1002+
setattr(sys.modules[module_name], 'main', integrate_dispatch.handler)
1003+
except KeyError:
1004+
raise ModuleNotFoundError('no call source module found')
9921005

9931006
return integrate_dispatch

packages/tencent-cloud-sdk-serverless-functions/tencent/cloud/serverless/functions/integrate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ def handler(self,
7575
Args:
7676
event: Serverless cloud function events.
7777
context: Serverless cloud function execution metadata.
78-
78+
7979
Raises:
8080
ValueError: Parameter value or integrated invoke protocol
8181
is not as expected.
8282
NotFoundError: The calling function indicated by the
8383
integrated invoke protocol does not exist.
84-
84+
RuntimeError: Unsupported invoke request.
85+
8586
Returns:
8687
Returns an integrated invoke protocol payload string.
8788
'''
@@ -92,6 +93,9 @@ def handler(self,
9293
if not context or not isinstance(context, dict):
9394
raise ValueError('<context> value invalid')
9495

96+
if 'protocol' not in event:
97+
raise RuntimeError('unsupported invoke request')
98+
9599
try:
96100
protocol_version: int = event['protocol']['version']
97101
protocol_payload: str = event['protocol']['payload']

0 commit comments

Comments
 (0)