Skip to content

Commit aafd082

Browse files
committed
Addressing comments
1 parent 1217794 commit aafd082

File tree

5 files changed

+80
-69
lines changed

5 files changed

+80
-69
lines changed

azure_functions_worker/dispatcher.py

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@
1212
import queue
1313
import sys
1414
import threading
15-
import uuid
1615
from asyncio import BaseEventLoop
1716
from logging import LogRecord
1817
from typing import List, Optional
1918

2019
import grpc
21-
from azure.functions import Function
2220

2321
from . import bindings, constants, functions, loader, protos
2422
from .bindings.shared_memory_data_transfer import SharedMemoryManager
2523
from .constants import (PYTHON_THREADPOOL_THREAD_COUNT,
2624
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT,
2725
PYTHON_THREADPOOL_THREAD_COUNT_MAX_37,
2826
PYTHON_THREADPOOL_THREAD_COUNT_MIN,
29-
PYTHON_ENABLE_DEBUG_LOGGING, SCRIPT_FILE_NAME,
30-
PYTHON_LANGUAGE_RUNTIME)
27+
PYTHON_ENABLE_DEBUG_LOGGING, SCRIPT_FILE_NAME)
3128
from .extension import ExtensionManager
3229
from .logging import disable_console_logging, enable_console_logging
3330
from .logging import enable_debug_logging_recommendation
@@ -322,7 +319,20 @@ async def _handle__functions_metadata_request(self, request):
322319
fx_metadata_results = []
323320
indexed_functions = loader.index_function_app(function_path)
324321
if indexed_functions:
325-
fx_metadata_results = self._process_indexed_function(
322+
indexed_function_logs: List[str] = []
323+
for func in indexed_functions:
324+
function_log = \
325+
f"Function Name: {func.get_function_name()} " \
326+
"Function Binding: " \
327+
f"{[binding.name for binding in func.get_bindings()]}"
328+
indexed_function_logs.append(function_log)
329+
330+
logger.info(
331+
f'Successfully processed FunctionMetadataRequest for '
332+
f'functions: {" ".join(indexed_function_logs)}')
333+
334+
fx_metadata_results = loader.process_indexed_function(
335+
self._functions,
326336
indexed_functions)
327337

328338
return protos.StreamingMessage(
@@ -745,49 +755,6 @@ def gen(resp_queue):
745755
error_logger.exception('unhandled error in gRPC thread')
746756
raise
747757

748-
def _process_indexed_function(self, indexed_functions: List[Function]):
749-
fx_metadata_results = []
750-
for indexed_function in indexed_functions:
751-
function_id = str(uuid.uuid4())
752-
function_info = self._functions.add_indexed_function(
753-
function_id,
754-
function=indexed_function)
755-
756-
binding_protos = {}
757-
for binding in indexed_function.get_bindings():
758-
binding_protos[binding.name] = protos.BindingInfo(
759-
type=binding.type,
760-
data_type=binding.data_type,
761-
direction=binding.direction)
762-
763-
function_metadata = protos.RpcFunctionMetadata(
764-
name=function_info.name,
765-
function_id=function_id,
766-
managed_dependency_enabled=False, # only enabled for PowerShell
767-
directory=function_info.directory,
768-
script_file=indexed_function.function_script_file,
769-
entry_point=function_info.name,
770-
is_proxy=False, # not supported in V4
771-
language=PYTHON_LANGUAGE_RUNTIME,
772-
bindings=binding_protos,
773-
raw_bindings=indexed_function.get_raw_bindings())
774-
775-
fx_metadata_results.append(function_metadata)
776-
777-
indexed_function_logs: List[str] = []
778-
for function in indexed_functions:
779-
function_log = \
780-
f"Function Name: {function.get_function_name()} " \
781-
f"Function Binding: " \
782-
f"{[binding.name for binding in function.get_bindings()]}"
783-
indexed_function_logs.append(function_log)
784-
785-
logger.info(
786-
f'Successfully processed FunctionMetadataRequest for functions: '
787-
f'{" ".join(indexed_function_logs)}')
788-
789-
return fx_metadata_results
790-
791758

792759
class AsyncLoggingHandler(logging.Handler):
793760

azure_functions_worker/functions.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ def validate_binding_direction(binding_name: str,
8989
func_name,
9090
'"inout" bindings are not supported')
9191

92-
if binding_name == '$return':
93-
if binding_direction != protos.BindingInfo.out:
94-
raise FunctionLoadError(
95-
func_name,
96-
'"$return" binding must have direction set to "out"')
92+
if binding_name == '$return' and \
93+
binding_direction != protos.BindingInfo.out:
94+
raise FunctionLoadError(
95+
func_name,
96+
'"$return" binding must have direction set to "out"')
9797

9898
@staticmethod
9999
def is_context_required(params, bound_params: dict,
@@ -240,15 +240,15 @@ def validate_function_params(params: dict, bound_params: dict,
240240
return input_types, output_types
241241

242242
@staticmethod
243-
def get_function_return_type(annotations, has_explicit_return,
244-
has_implicit_return, binding_name,
243+
def get_function_return_type(annotations: dict, has_explicit_return: bool,
244+
has_implicit_return: bool, binding_name: str,
245245
func_name: str):
246246
return_pytype = None
247247
if has_explicit_return and 'return' in annotations:
248248
return_anno = annotations.get('return')
249-
if (typing_inspect.is_generic_type(return_anno)
250-
and typing_inspect.get_origin(
251-
return_anno).__name__ == 'Out'):
249+
if typing_inspect.is_generic_type(
250+
return_anno) and typing_inspect.get_origin(
251+
return_anno).__name__ == 'Out':
252252
raise FunctionLoadError(
253253
func_name,
254254
'return annotation should not be azure.functions.Out')
@@ -286,9 +286,11 @@ def add_func_to_registry_and_return_funcinfo(self, function,
286286
requires_context: bool,
287287
has_explicit_return: bool,
288288
has_implicit_return: bool,
289-
input_types,
290-
output_types,
291-
return_type):
289+
input_types: typing.Dict[
290+
str, ParamTypeInfo],
291+
output_types: typing.Dict[
292+
str, ParamTypeInfo],
293+
return_type: str):
292294

293295
function_info = FunctionInfo(
294296
func=function,

azure_functions_worker/loader.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import os.path
99
import pathlib
1010
import sys
11-
import typing
11+
import uuid
1212
from os import PathLike, fspath
13+
from typing import List, Optional, Dict
1314

1415
from azure.functions import Function, FunctionApp
1516

16-
from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME
17+
from . import protos, functions
18+
from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME, \
19+
PYTHON_LANGUAGE_RUNTIME
1720
from .utils.wrappers import attach_message_to_exception
1821

1922
_AZURE_NAMESPACE = '__app__'
@@ -44,14 +47,53 @@ def uninstall() -> None:
4447
pass
4548

4649

50+
def build_binding_protos(indexed_function: List[Function]) -> Dict:
51+
binding_protos = {}
52+
for binding in indexed_function.get_bindings():
53+
binding_protos[binding.name] = protos.BindingInfo(
54+
type=binding.type,
55+
data_type=binding.data_type,
56+
direction=binding.direction)
57+
58+
return binding_protos
59+
60+
61+
def process_indexed_function(functions_registry: functions.Registry,
62+
indexed_functions: List[Function]):
63+
fx_metadata_results = []
64+
for indexed_function in indexed_functions:
65+
function_id = str(uuid.uuid4())
66+
function_info = functions_registry.add_indexed_function(
67+
function_id,
68+
function=indexed_function)
69+
70+
binding_protos = build_binding_protos(indexed_function)
71+
72+
function_metadata = protos.RpcFunctionMetadata(
73+
name=function_info.name,
74+
function_id=function_id,
75+
managed_dependency_enabled=False, # only enabled for PowerShell
76+
directory=function_info.directory,
77+
script_file=indexed_function.function_script_file,
78+
entry_point=function_info.name,
79+
is_proxy=False, # not supported in V4
80+
language=PYTHON_LANGUAGE_RUNTIME,
81+
bindings=binding_protos,
82+
raw_bindings=indexed_function.get_raw_bindings())
83+
84+
fx_metadata_results.append(function_metadata)
85+
86+
return fx_metadata_results
87+
88+
4789
@attach_message_to_exception(
4890
expt_type=ImportError,
4991
message=f'Please check the requirements.txt file for the missing module. '
5092
f'For more info, please refer the troubleshooting'
5193
f' guide: {MODULE_NOT_FOUND_TS_URL} '
5294
)
5395
def load_function(name: str, directory: str, script_file: str,
54-
entry_point: typing.Optional[str]):
96+
entry_point: Optional[str]):
5597
dir_path = pathlib.Path(directory)
5698
script_path = pathlib.Path(script_file) if script_file else pathlib.Path(
5799
_DEFAULT_SCRIPT_FILENAME)
@@ -99,11 +141,11 @@ def load_function(name: str, directory: str, script_file: str,
99141
expt_type=ImportError,
100142
message=f'Troubleshooting Guide: {MODULE_NOT_FOUND_TS_URL}'
101143
)
102-
def index_function_app(function_path: str) -> typing.List[Function]:
144+
def index_function_app(function_path: str) -> List[Function]:
103145
module_name = pathlib.Path(function_path).stem
104146
imported_module = importlib.import_module(module_name)
105147

106-
app: typing.Optional[FunctionApp] = None
148+
app: Optional[FunctionApp] = None
107149
for i in imported_module.__dir__():
108150
if isinstance(getattr(imported_module, i, None), FunctionApp):
109151
if not app:

tests/endtoend/queue_functions/queue_functions_stein/function_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import azure.functions as func
66

7-
app = func.FunctionApp(auth_level=func.AuthLevel.ANONYMOUS)
7+
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
88

99

1010
@app.function_name(name="get_queue_blob")

tests/unittests/http_functions/http_functions_stein/function_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import azure.functions as func
1212

13-
app = func.FunctionApp(auth_level=func.AuthLevel.ANONYMOUS)
13+
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
1414

1515
logger = logging.getLogger("my-function")
1616

@@ -229,7 +229,7 @@ def return_http_404(req: func.HttpRequest):
229229
return func.HttpResponse('bye', status_code=404)
230230

231231

232-
@app.route(route="return_http_auth_admin", auth_level=func.AuthLevel.ADMIN)
232+
@app.route(route="return_http_auth_admin", http_auth_level=func.AuthLevel.ADMIN)
233233
def return_http_auth_admin(req: func.HttpRequest):
234234
return func.HttpResponse('<h1>Hello World™</h1>',
235235
mimetype='text/html')

0 commit comments

Comments
 (0)