ob-metaflow-extensions 1.1.162rc0__py2.py3-none-any.whl → 1.1.162rc1__py2.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.

Potentially problematic release.


This version of ob-metaflow-extensions might be problematic. Click here for more details.

@@ -2,8 +2,10 @@ from metaflow.decorators import StepDecorator
2
2
  from metaflow import current
3
3
  import functools
4
4
  import os
5
+ import threading
5
6
 
6
- from .ollama import OllamaManager
7
+ from .ollama import OllamaManager, OllamaRequestInterceptor
8
+ from .status_card import OllamaStatusCard
7
9
  from ..card_utilities.injector import CardDecoratorInjector
8
10
 
9
11
  __mf_promote_submodules__ = ["plugins.ollama"]
@@ -38,10 +40,16 @@ class OllamaDecorator(StepDecorator, CardDecoratorInjector):
38
40
  Determines where and how to run the Ollama process.
39
41
  force_pull: bool
40
42
  Whether to run `ollama pull` no matter what, or first check the remote cache in Metaflow datastore for this model key.
41
- skip_push_check: bool
42
- Whether to skip the check that populates/overwrites remote cache on terminating an ollama model.
43
+ cache_update_policy: str
44
+ Cache update policy: "auto", "force", or "never".
45
+ force_cache_update: bool
46
+ Simple override for "force" cache update policy.
43
47
  debug: bool
44
48
  Whether to turn on verbose debugging logs.
49
+ circuit_breaker_config: dict
50
+ Configuration for circuit breaker protection. Keys: failure_threshold, recovery_timeout, reset_timeout.
51
+ timeout_config: dict
52
+ Configuration for various operation timeouts. Keys: pull, stop, health_check, install, server_startup.
45
53
  """
46
54
 
47
55
  name = "ollama"
@@ -49,8 +57,22 @@ class OllamaDecorator(StepDecorator, CardDecoratorInjector):
49
57
  "models": [],
50
58
  "backend": "local",
51
59
  "force_pull": False,
52
- "skip_push_check": False,
60
+ "cache_update_policy": "auto", # "auto", "force", "never"
61
+ "force_cache_update": False, # Simple override for "force"
53
62
  "debug": False,
63
+ "circuit_breaker_config": {
64
+ "failure_threshold": 3,
65
+ "recovery_timeout": 60,
66
+ "reset_timeout": 30,
67
+ },
68
+ "timeout_config": {
69
+ "pull": 600, # 10 minutes for model pulls
70
+ "stop": 30, # 30 seconds for model stops
71
+ "health_check": 5, # 5 seconds for health checks
72
+ "install": 60, # 1 minute for Ollama installation
73
+ "server_startup": 300, # 5 minutes for server startup
74
+ },
75
+ "card_refresh_interval": 10, # seconds - how often to update the status card
54
76
  }
55
77
 
56
78
  def step_init(
@@ -61,29 +83,143 @@ class OllamaDecorator(StepDecorator, CardDecoratorInjector):
61
83
  )
62
84
  self.flow_datastore_backend = flow_datastore._storage_impl
63
85
 
86
+ # Attach the ollama status card
87
+ self.attach_card_decorator(
88
+ flow,
89
+ step_name,
90
+ "ollama_status",
91
+ "blank",
92
+ refresh_interval=self.attributes["card_refresh_interval"],
93
+ )
94
+
64
95
  def task_decorate(
65
96
  self, step_func, flow, graph, retry_count, max_user_code_retries, ubf_context
66
97
  ):
67
98
  @functools.wraps(step_func)
68
99
  def ollama_wrapper():
100
+ self.ollama_manager = None
101
+ self.request_interceptor = None
102
+ self.status_card = None
103
+ self.card_monitor_thread = None
104
+
69
105
  try:
106
+ # Initialize status card and monitoring
107
+ self.status_card = OllamaStatusCard(
108
+ refresh_interval=self.attributes["card_refresh_interval"]
109
+ )
110
+
111
+ # Start card monitoring in background
112
+ def monitor_card():
113
+ try:
114
+ self.status_card.on_startup(current.card["ollama_status"])
115
+
116
+ while not getattr(
117
+ self.card_monitor_thread, "_stop_event", False
118
+ ):
119
+ try:
120
+ # Trigger card update with current data
121
+ self.status_card.on_update(
122
+ current.card["ollama_status"], None
123
+ )
124
+ import time
125
+
126
+ time.sleep(self.attributes["card_refresh_interval"])
127
+ except Exception as e:
128
+ if self.attributes["debug"]:
129
+ print(f"[@ollama] Card monitoring error: {e}")
130
+ break
131
+ except Exception as e:
132
+ if self.attributes["debug"]:
133
+ print(f"[@ollama] Card monitor thread error: {e}")
134
+ self.status_card.on_error(current.card["ollama_status"], str(e))
135
+
136
+ self.card_monitor_thread = threading.Thread(
137
+ target=monitor_card, daemon=True
138
+ )
139
+ self.card_monitor_thread._stop_event = False
140
+ self.card_monitor_thread.start()
141
+
142
+ # Initialize OllamaManager with status card
70
143
  self.ollama_manager = OllamaManager(
71
144
  models=self.attributes["models"],
72
145
  backend=self.attributes["backend"],
73
146
  flow_datastore_backend=self.flow_datastore_backend,
74
147
  force_pull=self.attributes["force_pull"],
75
- skip_push_check=self.attributes["skip_push_check"],
148
+ cache_update_policy=self.attributes["cache_update_policy"],
149
+ force_cache_update=self.attributes["force_cache_update"],
76
150
  debug=self.attributes["debug"],
151
+ circuit_breaker_config=self.attributes["circuit_breaker_config"],
152
+ timeout_config=self.attributes["timeout_config"],
153
+ status_card=self.status_card,
77
154
  )
155
+
156
+ # Install request protection by monkey-patching ollama package
157
+ self.request_interceptor = OllamaRequestInterceptor(
158
+ self.ollama_manager.circuit_breaker, self.attributes["debug"]
159
+ )
160
+ self.request_interceptor.install_protection()
161
+
162
+ if self.attributes["debug"]:
163
+ print(
164
+ "[@ollama] OllamaManager initialized and request protection installed"
165
+ )
166
+
78
167
  except Exception as e:
168
+ if self.status_card:
169
+ self.status_card.add_event(
170
+ "error", f"Initialization failed: {str(e)}"
171
+ )
172
+ try:
173
+ self.status_card.on_error(current.card["ollama_status"], str(e))
174
+ except:
175
+ pass
79
176
  print(f"[@ollama] Error initializing OllamaManager: {e}")
80
177
  raise
178
+
81
179
  try:
180
+ if self.status_card:
181
+ self.status_card.add_event("info", "Starting user step function")
82
182
  step_func()
183
+ if self.status_card:
184
+ self.status_card.add_event(
185
+ "success", "User step function completed successfully"
186
+ )
83
187
  finally:
84
- self.ollama_manager.terminate_models()
85
- if self.attributes["debug"]:
86
- print(f"[@ollama] process statuses: {self.ollama_manager.processes}")
87
- print(f"[@ollama] process runtime stats: {self.ollama_manager.stats}")
188
+ # Remove request protection first (before terminating models)
189
+ if self.request_interceptor:
190
+ self.request_interceptor.remove_protection()
191
+ if self.attributes["debug"]:
192
+ print("[@ollama] Request protection removed")
193
+
194
+ # Then cleanup ollama manager (while card monitoring is still active)
195
+ if self.ollama_manager:
196
+ self.ollama_manager.terminate_models()
197
+
198
+ # Give the card a moment to render the final shutdown events
199
+ if self.card_monitor_thread and self.status_card:
200
+ import time
201
+
202
+ # Trigger one final card update to capture all shutdown events
203
+ try:
204
+ self.status_card.on_update(current.card["ollama_status"], None)
205
+ except Exception as e:
206
+ if self.attributes["debug"]:
207
+ print(f"[@ollama] Final card update error: {e}")
208
+ time.sleep(2) # Allow final events to be rendered
209
+
210
+ # Now stop card monitoring
211
+ if self.card_monitor_thread:
212
+ self.card_monitor_thread._stop_event = True
213
+
214
+ if self.ollama_manager and self.attributes["debug"]:
215
+ print(
216
+ f"[@ollama] process statuses: {self.ollama_manager.processes}"
217
+ )
218
+ print(
219
+ f"[@ollama] process runtime stats: {self.ollama_manager.stats}"
220
+ )
221
+ print(
222
+ f"[@ollama] Circuit Breaker status: {self.ollama_manager.circuit_breaker.get_status()}"
223
+ )
88
224
 
89
225
  return ollama_wrapper