0
# Services
1
2
Service abstractions for long-running processes like model servers and deployments. Services represent persistent processes that run outside pipeline execution, such as model serving endpoints, data processing services, or monitoring daemons.
3
4
## Capabilities
5
6
### Base Service
7
8
```python { .api }
9
class BaseService:
10
"""
11
Abstract base class for services.
12
13
Services are long-running processes managed by ZenML,
14
such as model deployment servers, feature store servers, etc.
15
16
Attributes:
17
- uuid: Service UUID
18
- config: Service configuration
19
- status: Service status object
20
"""
21
22
def start(self):
23
"""Start the service."""
24
25
def stop(self):
26
"""Stop the service."""
27
28
def is_running(self) -> bool:
29
"""Check if service is running."""
30
31
def get_logs(self) -> str:
32
"""Get service logs."""
33
```
34
35
Import from:
36
37
```python
38
from zenml.services import BaseService
39
```
40
41
### Service Config
42
43
```python { .api }
44
class ServiceConfig:
45
"""
46
Configuration for services.
47
48
Base configuration class for all service types.
49
50
Attributes:
51
- name: Service name
52
- description: Service description
53
- pipeline_name: Associated pipeline name
54
- pipeline_run_id: Associated pipeline run ID
55
"""
56
```
57
58
Import from:
59
60
```python
61
from zenml.services import ServiceConfig
62
```
63
64
### Service Status
65
66
```python { .api }
67
class ServiceStatus:
68
"""
69
Status tracking for services.
70
71
Tracks the current state and health of a service.
72
73
Attributes:
74
- state: Service state (from ServiceState enum)
75
- last_state: Previous state
76
- error: Error message if in error state
77
"""
78
```
79
80
Import from:
81
82
```python
83
from zenml.services import ServiceStatus
84
```
85
86
### Service Endpoint
87
88
```python { .api }
89
class BaseServiceEndpoint:
90
"""
91
Abstract base for service endpoints.
92
93
Represents a network endpoint where a service is accessible.
94
"""
95
96
class ServiceEndpointConfig:
97
"""Configuration for service endpoints."""
98
99
class ServiceEndpointStatus:
100
"""Status tracking for service endpoints."""
101
102
class ServiceEndpointProtocol(str, Enum):
103
"""
104
Protocol types.
105
106
Values:
107
- HTTP: HTTP protocol
108
- HTTPS: HTTPS protocol
109
- TCP: TCP protocol
110
- UDP: UDP protocol
111
- GRPC: gRPC protocol
112
"""
113
HTTP = "http"
114
HTTPS = "https"
115
TCP = "tcp"
116
UDP = "udp"
117
GRPC = "grpc"
118
```
119
120
Import from:
121
122
```python
123
from zenml.services import (
124
BaseServiceEndpoint,
125
ServiceEndpointConfig,
126
ServiceEndpointProtocol
127
)
128
```
129
130
### Health Monitoring
131
132
```python { .api }
133
class BaseServiceEndpointHealthMonitor:
134
"""Base for health monitoring."""
135
136
class HTTPEndpointHealthMonitor(BaseServiceEndpointHealthMonitor):
137
"""HTTP health monitor."""
138
139
class TCPEndpointHealthMonitor(BaseServiceEndpointHealthMonitor):
140
"""TCP health monitor."""
141
```
142
143
Import from:
144
145
```python
146
from zenml.services import (
147
BaseServiceEndpointHealthMonitor,
148
HTTPEndpointHealthMonitor,
149
TCPEndpointHealthMonitor
150
)
151
```
152
153
### Container Service
154
155
```python { .api }
156
class ContainerService(BaseService):
157
"""
158
Service running in a container.
159
160
Manages services running as Docker containers.
161
"""
162
163
class ContainerServiceConfig(ServiceConfig):
164
"""Configuration for container services."""
165
166
class ContainerServiceStatus(ServiceStatus):
167
"""Status for container services."""
168
```
169
170
Import from:
171
172
```python
173
from zenml.services import (
174
ContainerService,
175
ContainerServiceConfig
176
)
177
```
178
179
### Local Daemon Service
180
181
```python { .api }
182
class LocalDaemonService(BaseService):
183
"""
184
Service running as a local daemon.
185
186
Manages services running as background processes on the local machine.
187
"""
188
189
class LocalDaemonServiceConfig(ServiceConfig):
190
"""Configuration for local daemon services."""
191
192
class LocalDaemonServiceStatus(ServiceStatus):
193
"""Status for local daemon services."""
194
```
195
196
Import from:
197
198
```python
199
from zenml.services import (
200
LocalDaemonService,
201
LocalDaemonServiceConfig
202
)
203
```
204
205
### Service State Enum
206
207
```python { .api }
208
class ServiceState(str, Enum):
209
"""
210
Service states.
211
212
Values:
213
- INACTIVE: Service not running
214
- ACTIVE: Service running
215
- PENDING_STARTUP: Starting up
216
- PENDING_SHUTDOWN: Shutting down
217
- ERROR: Error state
218
- SCALED_TO_ZERO: Scaled to zero (serverless)
219
"""
220
INACTIVE = "inactive"
221
ACTIVE = "active"
222
PENDING_STARTUP = "pending_startup"
223
PENDING_SHUTDOWN = "pending_shutdown"
224
ERROR = "error"
225
SCALED_TO_ZERO = "scaled_to_zero"
226
```
227
228
Import from:
229
230
```python
231
from zenml.enums import ServiceState
232
```
233
234
## Usage Examples
235
236
### Managing Services via Client
237
238
```python
239
from zenml.client import Client
240
241
client = Client()
242
243
# List all services
244
services = client.list_services()
245
for service in services:
246
print(f"Service: {service.name}")
247
print(f"Type: {service.service_type}")
248
print(f"State: {service.state}")
249
250
# Get specific service
251
service = client.get_service("model_server_123")
252
print(f"Service: {service.name}")
253
print(f"Config: {service.config}")
254
255
# Delete service
256
client.delete_service("old_service_id")
257
```
258
259
### Service Lifecycle
260
261
```python
262
from zenml.services import BaseService, ServiceState
263
264
# Get service instance
265
service = get_my_service() # Implementation specific
266
267
# Check status
268
if service.status.state == ServiceState.INACTIVE:
269
print("Service is not running")
270
271
# Start service
272
service.start()
273
274
# Wait for service to be active
275
while service.status.state == ServiceState.PENDING_STARTUP:
276
time.sleep(1)
277
278
if service.status.state == ServiceState.ACTIVE:
279
print("Service is running")
280
281
# Get logs
282
logs = service.get_logs()
283
print(logs)
284
285
# Stop service
286
service.stop()
287
```
288
289
### Service with Endpoint
290
291
```python
292
from zenml.services import (
293
BaseService,
294
ServiceEndpointProtocol,
295
HTTPEndpointHealthMonitor
296
)
297
298
# Service with HTTP endpoint
299
service = get_model_server() # Implementation specific
300
301
# Access endpoint
302
endpoint = service.endpoint
303
print(f"Service URL: {endpoint.protocol}://{endpoint.hostname}:{endpoint.port}")
304
305
# Check health
306
health_monitor = HTTPEndpointHealthMonitor(
307
endpoint=endpoint,
308
healthcheck_uri="/health"
309
)
310
311
is_healthy = health_monitor.check_health()
312
print(f"Service healthy: {is_healthy}")
313
```
314
315
### Model Deployment Service
316
317
```python
318
from zenml import step, pipeline
319
from zenml.client import Client
320
321
@step
322
def deploy_model(model: dict) -> str:
323
"""Deploy model and return service ID."""
324
# Deploy model (integration-specific)
325
# Returns service ID
326
return "service_123"
327
328
@pipeline
329
def deployment_pipeline():
330
model = {"weights": [0.1, 0.2]}
331
service_id = deploy_model(model)
332
return service_id
333
334
# Run pipeline
335
deployment_pipeline()
336
337
# Get deployed service
338
client = Client()
339
service = client.get_service("service_123")
340
print(f"Model service: {service.name}")
341
```
342
343
### Querying Service Status
344
345
```python
346
from zenml.client import Client
347
from zenml.enums import ServiceState
348
349
client = Client()
350
351
# Get all active services
352
services = client.list_services()
353
active_services = [
354
s for s in services
355
if s.state == ServiceState.ACTIVE
356
]
357
358
print(f"Active services: {len(active_services)}")
359
360
# Find services in error state
361
error_services = [
362
s for s in services
363
if s.state == ServiceState.ERROR
364
]
365
366
for service in error_services:
367
print(f"Service {service.name} in error: {service.error}")
368
```
369
370
### Service Cleanup
371
372
```python
373
from zenml.client import Client
374
from zenml.enums import ServiceState
375
376
client = Client()
377
378
# Clean up inactive services
379
services = client.list_services()
380
for service in services:
381
if service.state == ServiceState.INACTIVE:
382
print(f"Deleting inactive service: {service.name}")
383
client.delete_service(service.id)
384
```
385