Skip to content

Commit 244ba61

Browse files
author
Victoria Hall
committed
refactoring for env var reading
1 parent bcf434b commit 244ba61

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

azure_functions_worker/dispatcher.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def __init__(
115115
self._context_api = None
116116
self._trace_context_propagator = None
117117

118+
config_manager.read_environment_variables()
118119
# We allow the customer to change synchronous thread pool max worker
119120
# count by setting the PYTHON_THREADPOOL_THREAD_COUNT app setting.
120121
# For 3.[6|7|8] The default value is 1.
@@ -375,7 +376,7 @@ async def _handle__worker_init_request(self, request):
375376
self._sync_call_tp = self._create_sync_call_tp(
376377
self._get_sync_tp_max_workers()
377378
)
378-
config_manager.read_config(
379+
config_manager.set_config(
379380
os.path.join(worker_init_request.function_app_directory, "az-config.json")
380381
)
381382
logger.info(
@@ -822,7 +823,7 @@ async def _handle__function_environment_reload_request(self, request):
822823
env_vars = func_env_reload_request.environment_variables
823824
for var in env_vars:
824825
os.environ[var] = env_vars[var]
825-
config_manager.read_config(
826+
config_manager.set_config(
826827
os.path.join(
827828
func_env_reload_request.function_app_directory, "az-config.json"
828829
)

azure_functions_worker/utils/config_manager.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def __init__(self):
1515
"""
1616
self.config_data: Optional[Dict[str, str]] = None
1717

18+
def read_environment_variables(self):
19+
if self.config_data is None:
20+
self.config_data = {}
21+
env_copy = os.environ
22+
for k, v in env_copy.items():
23+
self.config_data.update({k.upper(): v})
24+
1825
def read_config(self, function_path: str):
1926
if self.config_data is None:
2027
self.config_data = {}
@@ -29,13 +36,17 @@ def read_config(self, function_path: str):
2936

3037
# updates the config dictionary with the environment variables
3138
# this prioritizes set env variables over the config file
32-
env_copy = os.environ
33-
for k, v in env_copy.items():
34-
self.config_data.update({k.upper(): v})
39+
# env_copy = os.environ
40+
# for k, v in env_copy.items():
41+
# self.config_data.update({k.upper(): v})
42+
43+
def set_config(self, function_path: str):
44+
self.read_config(function_path)
45+
self.read_environment_variables()
3546

3647
def config_exists(self) -> bool:
3748
if self.config_data is None:
38-
self.read_config("")
49+
self.set_config("")
3950
return self.config_data is not None
4051

4152
def get_config(self) -> dict:

tests/utils/testutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class AsyncTestCase(unittest.TestCase, metaclass=AsyncTestCaseMeta):
141141
class WebHostTestCaseMeta(type(unittest.TestCase)):
142142

143143
def __new__(mcls, name, bases, dct):
144+
config_manager.read_environment_variables()
144145
if config_manager.is_envvar_true(DEDICATED_DOCKER_TEST) \
145146
or config_manager.is_envvar_true(CONSUMPTION_DOCKER_TEST):
146147
return super().__new__(mcls, name, bases, dct)

0 commit comments

Comments
 (0)