by-framework 0.2.2.dev7__py3-none-any.whl → 0.2.2.dev9__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.
by_framework/admin/cli.py CHANGED
@@ -13,7 +13,8 @@ from rich.console import Console
13
13
  from rich.table import Table
14
14
 
15
15
  from by_framework.admin.worker_manager import WorkerManager
16
- from by_framework.common.redis_client import init_redis_from_url
16
+ from by_framework.common.config import RedisConfig
17
+ from by_framework.common.redis_client import init_redis, init_redis_from_url
17
18
  from by_framework.core.registry import WorkerRegistry
18
19
  from by_framework.metrics.snapshot import (
19
20
  build_observability_snapshot,
@@ -27,7 +28,13 @@ from by_framework.metrics.snapshot import (
27
28
  app = typer.Typer(
28
29
  name="by-admin",
29
30
  no_args_is_help=True,
30
- help="by-framework cluster admin CLI",
31
+ help=(
32
+ "by-framework cluster admin CLI. For Redis Cluster, omit --redis-url "
33
+ "and set REDIS_CLUSTER_HOST (comma-separated host:port list; implies "
34
+ "both cluster mode and REDIS_KEY_SCHEMA_VERSION=v2 unless set "
35
+ "explicitly). REDIS_MODE=cluster + REDIS_CLUSTER_NODES + "
36
+ "REDIS_KEY_SCHEMA_VERSION=v2 also still works."
37
+ ),
31
38
  )
32
39
  worker_app = typer.Typer(no_args_is_help=True)
33
40
  type_app = typer.Typer(no_args_is_help=True)
@@ -43,7 +50,8 @@ app.add_typer(metrics_app, name="metrics", help="Metrics and observability")
43
50
 
44
51
  console = Console()
45
52
  err_console = Console(stderr=True)
46
- _redis_url = "redis://localhost:6379/0"
53
+ _DEFAULT_REDIS_URL = "redis://localhost:6379/0"
54
+ _redis_url: Optional[str] = None
47
55
 
48
56
 
49
57
  @app.callback()
@@ -52,13 +60,16 @@ def _global(
52
60
  None,
53
61
  "--redis-url",
54
62
  envvar=["BYAI_REDIS_URL", "REDIS_URL"],
55
- help="Redis connection URL [env: BYAI_REDIS_URL]",
63
+ help=(
64
+ "Standalone Redis connection URL [env: BYAI_REDIS_URL]. "
65
+ "For Redis Cluster, omit this and set REDIS_CLUSTER_HOST "
66
+ "(comma-separated host:port list)."
67
+ ),
56
68
  ),
57
69
  ):
58
70
  """by-framework cluster admin CLI."""
59
71
  global _redis_url
60
- if redis_url:
61
- _redis_url = redis_url
72
+ _redis_url = redis_url
62
73
 
63
74
 
64
75
  def _run(coro): # type: ignore[no-untyped-def]
@@ -70,8 +81,14 @@ def _die(msg: str, code: int = 1) -> None:
70
81
  raise typer.Exit(code)
71
82
 
72
83
 
73
- def _get_redis():
74
- return init_redis_from_url(_redis_url)
84
+ def _get_redis(redis_url: Optional[str] = None):
85
+ configured_url = redis_url if redis_url is not None else _redis_url
86
+ if configured_url:
87
+ return init_redis_from_url(configured_url)
88
+ config = RedisConfig.from_env()
89
+ if config.mode == "cluster":
90
+ return init_redis(config=config)
91
+ return init_redis_from_url(_DEFAULT_REDIS_URL)
75
92
 
76
93
 
77
94
  # --------------------------------------------------------------------------- #
@@ -9,6 +9,7 @@ from dataclasses import dataclass, field
9
9
  from typing import Literal, Optional
10
10
 
11
11
  from by_framework.common.constants import RedisKeys
12
+ from by_framework.common.logger import logger
12
13
 
13
14
 
14
15
  @dataclass(frozen=True)
@@ -29,22 +30,40 @@ class RedisConfig:
29
30
 
30
31
  @classmethod
31
32
  def from_env(cls) -> "RedisConfig":
32
- """Load Redis configuration from environment variables."""
33
+ """Load Redis configuration from environment variables.
34
+
35
+ REDIS_CLUSTER_HOST (comma-separated "host:port" list) is the
36
+ preferred way to configure Cluster mode: just setting it is enough
37
+ to switch to cluster mode, no separate REDIS_MODE=cluster needed.
38
+ An explicit REDIS_MODE still takes precedence when set, so existing
39
+ explicit REDIS_MODE=cluster + REDIS_CLUSTER_NODES setups keep working.
40
+
41
+ REDIS_DATABASE replaces REDIS_DB (which still works as a deprecated
42
+ fallback, logging a warning, during the transition period).
43
+ """
33
44
  password = os.environ.get("REDIS_PASSWORD", "")
34
45
  username = os.environ.get("REDIS_USERNAME") or None
35
46
  max_connections = os.environ.get("REDIS_MAX_CONNECTIONS")
36
- mode = os.environ.get("REDIS_MODE", "standalone")
37
- cluster_nodes_str = os.environ.get("REDIS_CLUSTER_NODES")
47
+ cluster_host_str = os.environ.get("REDIS_CLUSTER_HOST")
48
+ cluster_nodes_str = cluster_host_str or os.environ.get("REDIS_CLUSTER_NODES")
49
+ mode = os.environ.get("REDIS_MODE") or (
50
+ "cluster" if cluster_host_str else "standalone"
51
+ )
38
52
  cluster_nodes = None
39
53
  if cluster_nodes_str:
40
54
  cluster_nodes = []
41
55
  for node in cluster_nodes_str.split(","):
42
56
  node_host, node_port = node.rsplit(":", 1)
43
57
  cluster_nodes.append((node_host, int(node_port)))
58
+ db_str = os.environ.get("REDIS_DATABASE")
59
+ if db_str is None:
60
+ db_str = os.environ.get("REDIS_DB")
61
+ if db_str is not None:
62
+ logger.warning("REDIS_DB is deprecated, use REDIS_DATABASE instead")
44
63
  return cls(
45
64
  host=os.environ.get("REDIS_HOST", "localhost"),
46
65
  port=int(os.environ.get("REDIS_PORT", "6379")),
47
- db=int(os.environ.get("REDIS_DB", "0")),
66
+ db=int(db_str) if db_str is not None else 0,
48
67
  password=password,
49
68
  username=username,
50
69
  max_connections=int(max_connections) if max_connections else None,
@@ -12,10 +12,20 @@ from typing import Optional
12
12
  def get_key_schema_version() -> str:
13
13
  """Return the configured Redis key schema version ("v1" or "v2").
14
14
 
15
- Controlled by REDIS_KEY_SCHEMA_VERSION, defaulting to "v1" (the current
16
- unprefixed key format). Cluster mode requires "v2" (see redis_client.init_redis).
15
+ Controlled by REDIS_KEY_SCHEMA_VERSION, which always wins when set
16
+ explicitly. When it isn't set, REDIS_CLUSTER_HOST being configured
17
+ implies "v2" (cluster mode requires v2 - v1 keys have no hash tags and
18
+ hit CROSSSLOT errors under Cluster; see redis_client.init_redis's
19
+ fail-fast check); otherwise it defaults to "v1" (the legacy unprefixed
20
+ key format). This mirrors RedisConfig.from_env()'s REDIS_MODE/
21
+ REDIS_CLUSTER_HOST precedence, but deliberately does NOT infer "v2"
22
+ from an explicit REDIS_MODE=cluster alone (without REDIS_CLUSTER_HOST)
23
+ - that legacy explicit-mode path still requires
24
+ REDIS_KEY_SCHEMA_VERSION=v2 to be set by hand.
17
25
  """
18
- version = os.environ.get("REDIS_KEY_SCHEMA_VERSION", "v1")
26
+ version = os.environ.get("REDIS_KEY_SCHEMA_VERSION")
27
+ if version is None:
28
+ version = "v2" if os.environ.get("REDIS_CLUSTER_HOST") else "v1"
19
29
  if version not in ("v1", "v2"):
20
30
  raise ValueError(
21
31
  f"Invalid REDIS_KEY_SCHEMA_VERSION: {version!r} (must be 'v1' or 'v2')"
@@ -112,11 +112,11 @@ def init_redis_from_url(url: str) -> Redis:
112
112
  def get_redis() -> Redis:
113
113
  """Get the initialized global Redis client.
114
114
 
115
- If not initialized, will automatically initialize with default config.
115
+ If not initialized, automatically initialize from environment config.
116
116
  """
117
117
  global _redis_client # pylint: disable=global-variable-not-assigned
118
118
  if _redis_client is None:
119
- return init_redis()
119
+ return init_redis(config=RedisConfig.from_env())
120
120
  return _redis_client
121
121
 
122
122
 
@@ -131,6 +131,17 @@ async def _run_worker_async(
131
131
  # parties can configure either mode purely programmatically or purely
132
132
  # via env vars, without the two modes behaving inconsistently.
133
133
  env_config = RedisConfig.from_env()
134
+ if redis_mode is not None:
135
+ effective_mode = redis_mode
136
+ elif redis_cluster_nodes:
137
+ # Mirrors REDIS_CLUSTER_HOST's env-var precedence: passing
138
+ # redis_cluster_nodes alone is enough to imply cluster mode, no
139
+ # separate redis_mode="cluster" required. Without this, passing
140
+ # redis_cluster_nodes alone would silently stay in standalone mode
141
+ # and the nodes list would be dropped on the floor.
142
+ effective_mode = "cluster"
143
+ else:
144
+ effective_mode = env_config.mode
134
145
  effective_config = replace(
135
146
  env_config,
136
147
  host=redis_host if redis_host is not None else env_config.host,
@@ -142,7 +153,7 @@ async def _run_worker_async(
142
153
  username=(
143
154
  redis_username if redis_username is not None else env_config.username
144
155
  ),
145
- mode=redis_mode if redis_mode is not None else env_config.mode,
156
+ mode=effective_mode,
146
157
  cluster_nodes=(
147
158
  redis_cluster_nodes
148
159
  if redis_cluster_nodes is not None
@@ -276,13 +287,23 @@ def run_worker(
276
287
  Redis configuration: every redis_* argument defaults to None, meaning
277
288
  "not specified" - when not passed, the value is read from the
278
289
  corresponding REDIS_* environment variable (REDIS_HOST, REDIS_PORT,
279
- REDIS_DB, REDIS_PASSWORD, REDIS_USERNAME, REDIS_MODE,
280
- REDIS_CLUSTER_NODES), falling back to "localhost"/6379/standalone if
281
- neither is set. An explicitly-passed argument always takes precedence
282
- over its env var, for every field, in both standalone and cluster mode
283
- - so either mode can be configured purely programmatically (e.g.
284
- ``redis_mode="cluster", redis_cluster_nodes=[("h1", 6379)]``) or purely
285
- via env vars, without the two modes behaving inconsistently.
290
+ REDIS_DATABASE, REDIS_PASSWORD, REDIS_USERNAME, REDIS_MODE,
291
+ REDIS_CLUSTER_HOST, REDIS_CLUSTER_NODES), falling back to
292
+ "localhost"/6379/standalone if none is set. REDIS_DATABASE replaces
293
+ REDIS_DB (still works as a deprecated fallback, logging a warning).
294
+ Setting REDIS_CLUSTER_HOST (comma-separated "host:port" list) alone is
295
+ enough to switch to cluster mode - no separate REDIS_MODE=cluster
296
+ required, though an explicit REDIS_MODE still wins if set; it also
297
+ implies REDIS_KEY_SCHEMA_VERSION=v2 unless that's set explicitly. An
298
+ explicitly-passed argument always takes precedence over its env var,
299
+ for every field, in both standalone and cluster mode. The same
300
+ single-argument convenience carries over to the programmatic path:
301
+ passing just ``redis_cluster_nodes=[("h1", 6379)]`` (no ``redis_mode``)
302
+ is enough to switch to cluster mode, mirroring REDIS_CLUSTER_HOST -
303
+ ``redis_mode`` only needs to be passed to explicitly force a mode
304
+ (e.g. pin a script to standalone regardless of REDIS_CLUSTER_HOST).
305
+ So either mode can be configured purely programmatically or purely via
306
+ env vars, without the two paths behaving inconsistently.
286
307
 
287
308
  Plugin support:
288
309
  - **Auto mode**: System automatically discovers and loads all subclasses
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: by-framework
3
- Version: 0.2.2.dev7
3
+ Version: 0.2.2.dev9
4
4
  Summary: 分布式 Agent 调度框架
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,18 +1,18 @@
1
1
  by_framework/__init__.py,sha256=5Ff4xfX-TNXcp4RMoBLQCWuehPMLpNRHwZrXofgFx5A,5300
2
2
  by_framework/__main__.py,sha256=_vIEieJzSHfZnPp-zna_hbVtWQUgE0I3mrWD41WzXiA,2561
3
3
  by_framework/admin/__init__.py,sha256=MKGQBtKk8YdnMrZ5gkzEemMl4iL8OfeFhEV6HdWR2XI,138
4
- by_framework/admin/cli.py,sha256=jsOYflCGKvSCNmZqA18fOR1mzsz_9XADl5BaEhdQVuM,10850
4
+ by_framework/admin/cli.py,sha256=KBmTHONKdb6BeskW6d3DL6GRnL91xen0PJ80JB0h0lc,11710
5
5
  by_framework/admin/worker_manager.py,sha256=ihXRMlFJAR74T2JSh3xTNCDjzAElWd0-_yVDAZwSTJU,5924
6
6
  by_framework/client/__init__.py,sha256=kGP7uoXIlTJuiJ2Cgl1Q5wqYjnMA4SZqoa7Q7BhQ5JQ,272
7
7
  by_framework/client/byai_client.py,sha256=bB1NyKLvVW82bXefD_3zymqZ1vk2O925CIbxOho9Qww,2426
8
8
  by_framework/client/client.py,sha256=T_5ahX9enmsaXm-xBGvwpMx-yALzO7wOTgY-qAVj0Q4,40342
9
9
  by_framework/common/__init__.py,sha256=EkfIiVdmb8SCpi7OOUm6Hu84B7Wzekb-TG2hbvD_R1w,1685
10
- by_framework/common/config.py,sha256=KgUJ8s2BI3Rm3JIDa1Yd0i72LthQQua-n7h-Y69QzmU,4075
11
- by_framework/common/constants.py,sha256=qzWIZcaTg4bGf-dz7bb6xJzlfvGIuw-fXhnB4mmhddc,21481
10
+ by_framework/common/config.py,sha256=RGe5Ce-uUgpZv1bgkdT4kCRcaIpaYhzskyvQy17Qn_o,5043
11
+ by_framework/common/constants.py,sha256=xUp6Nvx31EH7EzkxDNBzXD0GIE9FpPnPmuVbL-BAAoI,22089
12
12
  by_framework/common/emitter.py,sha256=Mcp6vglFlS7_JVsLd_KVKvV49TKjoyoN4eCM_tm4I8s,11310
13
13
  by_framework/common/exceptions.py,sha256=NKQC7ueLZ4-gVTRz3SFRZNoPHsayu6jxi3-YlW0n_jg,962
14
14
  by_framework/common/logger.py,sha256=DX6S7U5CLcCQIUQ3IWqeeDnSC7QGuHKYaEhiKvKrBpk,5823
15
- by_framework/common/redis_client.py,sha256=DVMTGoj1RdsFXr9vdE92Y3ZMPdtVZfYQhXosyymbIcA,4640
15
+ by_framework/common/redis_client.py,sha256=JfYE-QutI9G27dbehzvLw0LziTM1gHmG4tdVjxd0IoA,4668
16
16
  by_framework/core/__init__.py,sha256=bEaOfjGoCF0WacR_PoYSb5kUqJv-g59pngdnEE7fJzo,2070
17
17
  by_framework/core/availability.py,sha256=ZME5S002eNzNpzaW_QuWw8AmUN1JelUxwCt2U3PPAeQ,17624
18
18
  by_framework/core/delivery_gate.py,sha256=yijlCOw8zEou1HCyQzwRHDJZtLBsUnXtQ3x1FZsc4yU,2012
@@ -82,7 +82,7 @@ by_framework/worker/__init__.py,sha256=GBkUWYnHc2j3_Wx2AC2P49DzFl0CSrl0SKhdRP5CT
82
82
  by_framework/worker/_control_handling.py,sha256=0bL1Alx8_qaeXQV32Xv4ARSd9q77ySh9LMd7KtZZXu8,8111
83
83
  by_framework/worker/_execution_tracking.py,sha256=AWahfDKTXGrx_JWSCNisjGKkGAQruu9vSlXisof0BYQ,2493
84
84
  by_framework/worker/_message_processing.py,sha256=rgKzJTgPVDFp_HUfMcqJBwNdTCEfspuRAo5IM9VKsxs,2622
85
- by_framework/worker/app.py,sha256=7b3BkKewPjZb5HrzC2xftGTXP59YwMtMUPZsBqbbeUM,13195
85
+ by_framework/worker/app.py,sha256=pf_q4F1ypf6u97HP4IpHVPVW5pssMg9_XXAwzv0wJfM,14387
86
86
  by_framework/worker/byai_context.py,sha256=DvKw-X8ePZr5c5osdapvzTvH4G23u0J80U-fgMLtge4,2388
87
87
  by_framework/worker/byai_worker.py,sha256=0NoRqzuTG_MPMxVbZ1ueeOrqI0H8-Adxh7dM3vckjJo,1757
88
88
  by_framework/worker/context.py,sha256=XTNeTAhXvzS2OMDRCDKVD0s3uQscE1rFo9eImUw773w,44747
@@ -92,8 +92,8 @@ by_framework/worker/runner.py,sha256=ZuuhzdXB4oIjSaNyJCjOIYhzvw6L5N7X6kPzvrbWET8
92
92
  by_framework/worker/worker.py,sha256=_RmNkvHEN5kDPlqbHtVQhkhgiKx37xjnCxNkf_tvASM,35441
93
93
  by_framework/worker/sandbox/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
94
94
  by_framework/worker/sandbox/hook_sandbox.py,sha256=XXSdH5YGX814LvV03vkq-zn5DVIBPYYt2NhRv1lT1Bk,2329
95
- by_framework-0.2.2.dev7.dist-info/METADATA,sha256=eLdbc4la4ZUqdpLajSt3MQNsNHUV0g3vZABp3zPwYgI,26533
96
- by_framework-0.2.2.dev7.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
97
- by_framework-0.2.2.dev7.dist-info/entry_points.txt,sha256=5t6kgMjkR0UgJTpOsbuQDo0fOf7zeXjQW9DhkxfAGFc,56
98
- by_framework-0.2.2.dev7.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
99
- by_framework-0.2.2.dev7.dist-info/RECORD,,
95
+ by_framework-0.2.2.dev9.dist-info/METADATA,sha256=qmdF2bXuu0_IfQI5_QazjrVOxd8EjIHiqnm0PiIVZVo,26533
96
+ by_framework-0.2.2.dev9.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
97
+ by_framework-0.2.2.dev9.dist-info/entry_points.txt,sha256=5t6kgMjkR0UgJTpOsbuQDo0fOf7zeXjQW9DhkxfAGFc,56
98
+ by_framework-0.2.2.dev9.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
99
+ by_framework-0.2.2.dev9.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.30.1
2
+ Generator: hatchling 1.31.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any