nsj-rest-lib2 0.0.2__py3-none-any.whl → 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nsj_rest_lib2/service/entity_loader.py +50 -34
- nsj_rest_lib2/settings.py +7 -0
- {nsj_rest_lib2-0.0.2.dist-info → nsj_rest_lib2-0.0.3.dist-info}/METADATA +1 -1
- {nsj_rest_lib2-0.0.2.dist-info → nsj_rest_lib2-0.0.3.dist-info}/RECORD +6 -6
- {nsj_rest_lib2-0.0.2.dist-info → nsj_rest_lib2-0.0.3.dist-info}/WHEEL +0 -0
- {nsj_rest_lib2-0.0.2.dist-info → nsj_rest_lib2-0.0.3.dist-info}/top_level.txt +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import datetime
|
|
1
2
|
import json
|
|
2
3
|
import sys
|
|
3
4
|
import threading
|
|
@@ -7,13 +8,15 @@ from nsj_rest_lib.settings import get_logger
|
|
|
7
8
|
|
|
8
9
|
from nsj_rest_lib2.exception import MissingEntityConfigException
|
|
9
10
|
from nsj_rest_lib2.redis_config import get_redis
|
|
10
|
-
from nsj_rest_lib2.settings import ESCOPO_RESTLIB2
|
|
11
|
+
from nsj_rest_lib2.settings import ESCOPO_RESTLIB2, MIN_TIME_SOURCE_REFRESH
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class LoadedEntity:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self.dto_class_name: str = ""
|
|
17
|
+
self.entity_class_name: str = ""
|
|
18
|
+
self.entity_hash: str = ""
|
|
19
|
+
self.loaded_at: datetime.datetime = datetime.datetime.now()
|
|
17
20
|
|
|
18
21
|
|
|
19
22
|
class Namespace:
|
|
@@ -24,7 +27,7 @@ class Namespace:
|
|
|
24
27
|
self.module: types.ModuleType = None
|
|
25
28
|
|
|
26
29
|
|
|
27
|
-
namespaces_dict = {}
|
|
30
|
+
namespaces_dict: dict[str, Namespace] = {}
|
|
28
31
|
|
|
29
32
|
|
|
30
33
|
class EntityLoader:
|
|
@@ -48,34 +51,51 @@ class EntityLoader:
|
|
|
48
51
|
|
|
49
52
|
# Se conseguiu carregar da memória, verifica se houve alteração no hash, em relação ao redis
|
|
50
53
|
if result is not None:
|
|
51
|
-
# Desempacotando o result
|
|
52
|
-
|
|
54
|
+
# Desempacotando o result e recuperando informações do namespace
|
|
55
|
+
(
|
|
56
|
+
entity_config_key,
|
|
57
|
+
namespace,
|
|
58
|
+
) = result
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
loaded_entity = namespace.loaded_entities[entity_id]
|
|
61
|
+
dto_class_name = loaded_entity.dto_class_name
|
|
62
|
+
entity_class_name = loaded_entity.entity_class_name
|
|
63
|
+
entities_dict = namespace.entities_dict
|
|
58
64
|
|
|
59
|
-
# Se
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
# Se o tempo entre o carregamento e agora for maior do que MIN_TIME_SOURCE_REFRESH minutos,
|
|
66
|
+
# verifica se precisa de refresh
|
|
67
|
+
time_diff = datetime.datetime.now() - loaded_entity.loaded_at
|
|
62
68
|
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
if time_diff.total_seconds() >= MIN_TIME_SOURCE_REFRESH * 60:
|
|
70
|
+
# Recuperando do Redis direto pela key (faz uma só chamada ao redis)
|
|
71
|
+
loaded_config = self._load_entity_config_from_redis(
|
|
72
|
+
entity_id, grupo_key, tenant_key, default_key, entity_config_key
|
|
73
|
+
)
|
|
65
74
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
entity_config_key,
|
|
70
|
-
entity_id,
|
|
71
|
-
check_refresh=True,
|
|
72
|
-
)
|
|
75
|
+
# Se não achar no redis, usa o que estava em memória
|
|
76
|
+
if not loaded_config:
|
|
77
|
+
return (dto_class_name, entity_class_name, entities_dict)
|
|
73
78
|
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
# Desempacotando resultado
|
|
80
|
+
entity_config_key, entity_config_str = loaded_config
|
|
81
|
+
|
|
82
|
+
# Executando o código da entidade, só se houver mudança no hash
|
|
83
|
+
result_execute = self._execute_entity_source(
|
|
84
|
+
entity_config_str,
|
|
85
|
+
entity_config_key,
|
|
86
|
+
entity_id,
|
|
87
|
+
check_refresh=True,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# Se não carregou novo código, usa o que estava em memória
|
|
91
|
+
if result_execute is None:
|
|
92
|
+
return (dto_class_name, entity_class_name, entities_dict)
|
|
93
|
+
else:
|
|
94
|
+
dto_class_name, entity_class_name, namespace = result_execute
|
|
95
|
+
return (dto_class_name, entity_class_name, namespace.entities_dict)
|
|
76
96
|
else:
|
|
77
|
-
|
|
78
|
-
return (dto_class_name, entity_class_name,
|
|
97
|
+
# Se não deu o intervalo de verificação do refresh, retorna o que está em memória
|
|
98
|
+
return (dto_class_name, entity_class_name, entities_dict)
|
|
79
99
|
|
|
80
100
|
# Se não conseguir recuperar a entidade, procura no redis:
|
|
81
101
|
loaded_config = self._load_entity_config_from_redis(
|
|
@@ -236,7 +256,7 @@ class EntityLoader:
|
|
|
236
256
|
grupo_key: str,
|
|
237
257
|
tenant_key: str,
|
|
238
258
|
default_key: str,
|
|
239
|
-
) -> tuple[str,
|
|
259
|
+
) -> tuple[str, Namespace] | None:
|
|
240
260
|
namespace = None
|
|
241
261
|
entity_config_key = None
|
|
242
262
|
|
|
@@ -258,11 +278,7 @@ class EntityLoader:
|
|
|
258
278
|
entity_config_key = default_key
|
|
259
279
|
namespace = default_namespace
|
|
260
280
|
|
|
261
|
-
if namespace:
|
|
262
|
-
|
|
263
|
-
dto_class_name = loaded_entity.dto_class_name
|
|
264
|
-
entity_class_name = loaded_entity.entity_class_name
|
|
265
|
-
entities_dict = namespace.entities_dict
|
|
266
|
-
return (dto_class_name, entity_class_name, entities_dict, entity_config_key)
|
|
281
|
+
if namespace and entity_config_key:
|
|
282
|
+
return (entity_config_key, namespace)
|
|
267
283
|
else:
|
|
268
284
|
return None
|
nsj_rest_lib2/settings.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nsj_rest_lib2
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.3
|
|
4
4
|
Summary: Biblioteca para permitir a distribuição de rotas dinâmicas numa API, configuradas por meio de EDLs declarativos (em formato JSON).
|
|
5
5
|
Home-page: https://github.com/Nasajon/nsj_rest_lib2
|
|
6
6
|
Author: Nasajon Sistemas
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
nsj_rest_lib2/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
nsj_rest_lib2/exception.py,sha256=E9uMUdoCCQOVQfc8f6gD9b5Pxurf3Q4SytDCcqSlkZ8,56
|
|
3
3
|
nsj_rest_lib2/redis_config.py,sha256=4KLcvYS3nJO7PMQgF6F9_j6r-TyqcS7TBbd3LEQuKDU,629
|
|
4
|
-
nsj_rest_lib2/settings.py,sha256=
|
|
4
|
+
nsj_rest_lib2/settings.py,sha256=f_jqFEv1rkhfEfyvCtTduUEwc4dwiiLnP00VM7gvWPw,243
|
|
5
5
|
nsj_rest_lib2/controller/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
nsj_rest_lib2/controller/dynamic_controller.py,sha256=4HS1__ton8cQ1LSvMoZUv93BQEnGV5f2gE-jW5lD510,13448
|
|
7
7
|
nsj_rest_lib2/service/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
nsj_rest_lib2/service/entity_loader.py,sha256=
|
|
9
|
-
nsj_rest_lib2-0.0.
|
|
10
|
-
nsj_rest_lib2-0.0.
|
|
11
|
-
nsj_rest_lib2-0.0.
|
|
12
|
-
nsj_rest_lib2-0.0.
|
|
8
|
+
nsj_rest_lib2/service/entity_loader.py,sha256=1_NhcO_7g7CWC3WTKHCYkblH2luO3AbPRyE2uCQ2zXw,10638
|
|
9
|
+
nsj_rest_lib2-0.0.3.dist-info/METADATA,sha256=wkUs-y8kYkmSQYVt2pi4QHR28ECogJkjez3VcsGpcMY,1196
|
|
10
|
+
nsj_rest_lib2-0.0.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
11
|
+
nsj_rest_lib2-0.0.3.dist-info/top_level.txt,sha256=L6zh0EfH8_rur7OJ8_V-El-XEMf4qg3bkF8ADgqLVIA,14
|
|
12
|
+
nsj_rest_lib2-0.0.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|