-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathconfiguration.py
More file actions
618 lines (526 loc) · 22.3 KB
/
Copy pathconfiguration.py
File metadata and controls
618 lines (526 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
from __future__ import annotations
import json
import logging
import os
from typing import Any, Dict, Optional, Union
from conductor.asyncio_client.http.configuration import (
Configuration as HttpConfiguration,
)
class Configuration:
"""
Configuration adapter for Orkes Conductor Asyncio Client with environment variable support.
This adapter wraps the generated HttpConfiguration class and provides:
- Environment variable support for standard Conductor settings
- Worker properties configuration (pollInterval, domain, etc.)
- Backward compatibility with existing code
Supported Environment Variables:
--------------------------------
CONDUCTOR_SERVER_URL: Server URL (e.g., http://localhost:8080/api)
CONDUCTOR_AUTH_KEY: Authentication key ID
CONDUCTOR_AUTH_SECRET: Authentication key secret
Worker Properties (via environment variables):
----------------------------------------------
CONDUCTOR_WORKER_DOMAIN: Default worker domain
CONDUCTOR_WORKER_POLL_INTERVAL: Polling interval in milliseconds (default: 100)
CONDUCTOR_WORKER_POLL_INTERVAL_SECONDS: Polling interval in seconds (default: 0)
CONDUCTOR_WORKER_<TASK_TYPE>_POLLING_INTERVAL: Task-specific polling interval
CONDUCTOR_WORKER_<TASK_TYPE>_DOMAIN: Task-specific domain
Authentication Retry Policy (401 Handling):
-------------------------------------------
CONDUCTOR_AUTH_401_MAX_ATTEMPTS: Maximum retry attempts per endpoint (default: 6)
CONDUCTOR_AUTH_401_BASE_DELAY_MS: Base delay in milliseconds (default: 1000.0)
CONDUCTOR_AUTH_401_MAX_DELAY_MS: Maximum delay cap in milliseconds (default: 60000.0)
CONDUCTOR_AUTH_401_JITTER_PERCENT: Random jitter percentage 0.0-1.0 (default: 0.2)
CONDUCTOR_AUTH_401_STOP_BEHAVIOR: Behavior after max attempts: 'stop_worker' or 'continue' (default: 'stop_worker')
Example:
--------
```python
# Using environment variables
os.environ['CONDUCTOR_SERVER_URL'] = 'http://localhost:8080/api'
os.environ['CONDUCTOR_AUTH_KEY'] = 'your_key'
os.environ['CONDUCTOR_AUTH_SECRET'] = 'your_secret'
config = Configuration()
# Or with explicit parameters
config = Configuration(
server_url='http://localhost:8080/api',
auth_key='your_key',
auth_secret='your_secret'
)
```
"""
def __init__(
self,
server_url: Optional[str] = None,
auth_key: Optional[str] = None,
auth_secret: Optional[str] = None,
debug: bool = False,
auth_token_ttl_min: int = 45,
# Worker properties
polling_interval: Optional[int] = None,
domain: Optional[str] = None,
polling_interval_seconds: Optional[int] = None,
# HTTP Configuration parameters
api_key: Optional[Dict[str, str]] = None,
api_key_prefix: Optional[Dict[str, str]] = None,
username: Optional[str] = None,
password: Optional[str] = None,
access_token: Optional[str] = None,
server_index: Optional[int] = None,
server_variables: Optional[Dict[str, str]] = None,
server_operation_index: Optional[Dict[int, int]] = None,
server_operation_variables: Optional[Dict[int, Dict[str, str]]] = None,
ignore_operation_servers: bool = False,
ssl_ca_cert: Optional[str] = None,
retries: Optional[int] = None,
ca_cert_data: Optional[Union[str, bytes]] = None,
cert_file: Optional[str] = None,
key_file: Optional[str] = None,
verify_ssl: Optional[bool] = None,
proxy: Optional[str] = None,
proxy_headers: Optional[Dict[str, str]] = None,
# 401-specific configuration
auth_401_max_attempts: Optional[int] = None,
auth_401_base_delay_ms: Optional[float] = None,
auth_401_max_delay_ms: Optional[float] = None,
auth_401_jitter_percent: Optional[float] = None,
auth_401_stop_behavior: Optional[str] = None,
**kwargs: Any,
):
"""
Initialize Configuration with environment variable support.
Parameters:
-----------
server_url : str, optional
Conductor server URL. If not provided, reads from CONDUCTOR_SERVER_URL env var.
auth_key : str, optional
Authentication key ID. If not provided, reads from CONDUCTOR_AUTH_KEY env var.
auth_secret : str, optional
Authentication key secret. If not provided, reads from CONDUCTOR_AUTH_SECRET env var.
debug : bool, optional
Enable debug logging. Default is False.
polling_interval : int, optional
Polling interval in milliseconds. If not provided, reads from CONDUCTOR_WORKER_POLL_INTERVAL env var.
domain : str, optional
Worker domain. If not provided, reads from CONDUCTOR_WORKER_DOMAIN env var.
polling_interval_seconds : int, optional
Polling interval in seconds. If not provided, reads from CONDUCTOR_WORKER_POLL_INTERVAL_SECONDS env var.
**kwargs : Any
Additional parameters passed to HttpConfiguration.
Environment Variables:
---------------------
CONDUCTOR_SERVER_URL: Server URL (e.g., http://localhost:8080/api)
CONDUCTOR_AUTH_KEY: Authentication key ID
CONDUCTOR_AUTH_SECRET: Authentication key secret
CONDUCTOR_PROXY: Proxy URL for HTTP requests
CONDUCTOR_PROXY_HEADERS: Proxy headers as JSON string or single header value
"""
# Resolve server URL from parameter or environment variable
if server_url is not None:
self.server_url = server_url
else:
self.server_url = os.getenv("CONDUCTOR_SERVER_URL")
if self.server_url is None or self.server_url == "":
self.server_url = "http://localhost:8080/api"
# Resolve authentication from parameters or environment variables
if auth_key is not None:
self.auth_key = auth_key
else:
self.auth_key = os.getenv("CONDUCTOR_AUTH_KEY")
if auth_secret is not None:
self.auth_secret = auth_secret
else:
self.auth_secret = os.getenv("CONDUCTOR_AUTH_SECRET")
# Additional worker properties with environment variable fallback
self.polling_interval = polling_interval or self._get_env_int(
"CONDUCTOR_WORKER_POLL_INTERVAL", 100
)
self.domain = domain or os.getenv("CONDUCTOR_WORKER_DOMAIN", None)
self.polling_interval_seconds = polling_interval_seconds or self._get_env_int(
"CONDUCTOR_WORKER_POLL_INTERVAL_SECONDS", 0
)
# Store additional worker properties
self._worker_properties: Dict[str, Dict[str, Any]] = {}
# Setup API key authentication if auth credentials are provided
if api_key is None:
api_key = {}
self.__ui_host = os.getenv("CONDUCTOR_UI_SERVER_URL")
if self.__ui_host is None:
self.__ui_host = self.server_url.replace("/api", "")
# Proxy configuration - can be set via parameter or environment variable
self.proxy = proxy or os.getenv("CONDUCTOR_PROXY")
# Proxy headers - can be set via parameter or environment variable
self.proxy_headers = proxy_headers
if not self.proxy_headers and os.getenv("CONDUCTOR_PROXY_HEADERS"):
try:
self.proxy_headers = json.loads(os.getenv("CONDUCTOR_PROXY_HEADERS"))
except (json.JSONDecodeError, TypeError):
# If JSON parsing fails, treat as a single header value
self.proxy_headers = {
"Authorization": os.getenv("CONDUCTOR_PROXY_HEADERS")
}
self.logger_format = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"
# Create the underlying HTTP configuration
http_config_kwargs = {
"host": self.server_url,
"api_key": api_key,
"api_key_prefix": api_key_prefix,
"username": username,
"password": password,
"access_token": access_token,
"server_index": server_index,
"server_variables": server_variables,
"server_operation_index": server_operation_index,
"server_operation_variables": server_operation_variables,
"ignore_operation_servers": ignore_operation_servers,
"ssl_ca_cert": ssl_ca_cert or os.getenv("CONDUCTOR_SSL_CA_CERT"),
"retries": retries,
"ca_cert_data": ca_cert_data or os.getenv("CONDUCTOR_SSL_CA_CERT_DATA"),
"debug": debug,
}
# Add SSL parameters if they exist in HttpConfiguration
if cert_file or os.getenv("CONDUCTOR_CERT_FILE"):
http_config_kwargs["cert_file"] = cert_file or os.getenv("CONDUCTOR_CERT_FILE")
if key_file or os.getenv("CONDUCTOR_KEY_FILE"):
http_config_kwargs["key_file"] = key_file or os.getenv("CONDUCTOR_KEY_FILE")
if verify_ssl is not None:
http_config_kwargs["verify_ssl"] = verify_ssl
elif os.getenv("CONDUCTOR_VERIFY_SSL"):
http_config_kwargs["verify_ssl"] = self._get_env_bool("CONDUCTOR_VERIFY_SSL", True)
http_config_kwargs.update(kwargs)
self._http_config = HttpConfiguration(**http_config_kwargs)
# Set proxy configuration on the HTTP config
if self.proxy:
self._http_config.proxy = self.proxy
if self.proxy_headers:
self._http_config.proxy_headers = self.proxy_headers
# Set proxy configuration on the HTTP config
if self.proxy:
self._http_config.proxy = self.proxy
if self.proxy_headers:
self._http_config.proxy_headers = self.proxy_headers
# Debug switch and logging setup
self.__debug = debug
if self.__debug:
self.__log_level = logging.DEBUG
else:
self.__log_level = logging.INFO
# Log format
self.__logger_format = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"
# Setup logging
self.logger = logging.getLogger(__name__)
if debug:
self.logger.setLevel(logging.DEBUG)
self.is_logger_config_applied = False
# Orkes Conductor auth token properties
self.token_update_time = 0
self.auth_token_ttl_sec = auth_token_ttl_min * 60
# 401-specific configuration
self.auth_401_max_attempts = auth_401_max_attempts or self._get_env_int(
"CONDUCTOR_AUTH_401_MAX_ATTEMPTS", 6
)
self.auth_401_base_delay_ms = auth_401_base_delay_ms or self._get_env_float(
"CONDUCTOR_AUTH_401_BASE_DELAY_MS", 1000.0
)
self.auth_401_max_delay_ms = auth_401_max_delay_ms or self._get_env_float(
"CONDUCTOR_AUTH_401_MAX_DELAY_MS", 60000.0
)
self.auth_401_jitter_percent = auth_401_jitter_percent or self._get_env_float(
"CONDUCTOR_AUTH_401_JITTER_PERCENT", 0.2
)
self.auth_401_stop_behavior = auth_401_stop_behavior or os.getenv(
"CONDUCTOR_AUTH_401_STOP_BEHAVIOR", "stop_worker"
)
def _get_env_float(self, env_var: str, default: float) -> float:
"""Get float value from environment variable with default fallback."""
try:
value = os.getenv(env_var)
if value is not None:
return float(value)
except (ValueError, TypeError):
self.logger.warning("Invalid float value for %s: %s", env_var, value)
return default
def _get_env_int(self, env_var: str, default: int) -> int:
"""Get integer value from environment variable with default fallback."""
try:
value = os.getenv(env_var)
if value is not None:
return int(value)
except (ValueError, TypeError):
self.logger.warning("Invalid float value for %s: %s", env_var, value)
return default
def _get_env_bool(self, env_var: str, default: bool) -> bool:
"""Get boolean value from environment variable with default fallback."""
value = os.getenv(env_var)
if value is not None:
return value.lower() in ("true", "1")
return default
def get_worker_property_value(
self, property_name: str, task_type: Optional[str] = None
) -> Optional[Any]:
"""
Get worker property value with task-specific and global fallback.
Follows the same pattern as the regular client:
1. Check for task-specific environment variable: CONDUCTOR_WORKER_<TASK_TYPE>_<PROPERTY>
2. Check for global environment variable: CONDUCTOR_WORKER_<PROPERTY>
3. Return configured default value
Parameters:
-----------
property_name : str
Property name (e.g., 'polling_interval', 'domain')
task_type : str, optional
Task type for task-specific configuration
Returns:
--------
Any
Property value or None if not found
"""
prefix = "conductor_worker"
# Look for task-specific property
if task_type:
key_specific = f"{prefix}_{task_type}_{property_name}".upper()
value = os.getenv(key_specific)
if value is not None:
return self._convert_property_value(property_name, value)
# Look for global property
key_global = f"{prefix}_{property_name}".upper()
value = os.getenv(key_global)
if value is not None:
return self._convert_property_value(property_name, value)
# Return default value
elif property_name == "domain":
return self.domain
elif property_name == "polling_interval":
return self.polling_interval
elif property_name == "poll_interval_seconds":
return self.polling_interval_seconds
return None
def _convert_property_value(self, property_name: str, value: str) -> Any:
"""Convert string property value to appropriate type."""
if property_name == "polling_interval":
try:
return float(value)
except (ValueError, TypeError):
self.logger.warning("Invalid polling_interval value: %s", value)
return self.polling_interval
elif property_name == "polling_interval_seconds":
try:
return float(value)
except (ValueError, TypeError):
self.logger.warning("Invalid polling_interval_seconds value: %s", value)
return self.polling_interval_seconds
# For other properties, return as string
return value
def set_worker_property(self, task_type: str, property_name: str, value: Any) -> None:
"""
Set worker property for a specific task type.
Parameters:
-----------
task_type : str
Task type name
property_name : str
Property name
value : Any
Property value
"""
if task_type not in self._worker_properties:
self._worker_properties[task_type] = {}
self._worker_properties[task_type][property_name] = value
def get_worker_property(self, task_type: str, property_name: str) -> Optional[Any]:
"""
Get worker property for a specific task type.
Parameters:
-----------
task_type : str
Task type name
property_name : str
Property name
Returns:
--------
Any
Property value or None if not found
"""
if task_type in self._worker_properties:
return self._worker_properties[task_type].get(property_name)
return None
def get_polling_interval(self, task_type: Optional[str] = None) -> float:
"""
Get polling interval for a task type with environment variable support.
Parameters:
-----------
task_type : str, optional
Task type for task-specific configuration
Returns:
--------
float
Polling interval in seconds
"""
value = self.get_worker_property_value("polling_interval", task_type)
return value if value is not None else self.default_polling_interval
def get_domain(self, task_type: Optional[str] = None) -> Optional[str]:
"""
Get domain for a task type with environment variable support.
Parameters:
-----------
task_type : str, optional
Task type for task-specific configuration
Returns:
--------
str, optional
Domain name or None
"""
return self.get_worker_property_value("domain", task_type)
def get_poll_interval(self, task_type: Optional[str] = None) -> int:
"""
Get polling interval in milliseconds for a task type with environment variable support.
Parameters:
-----------
task_type : str, optional
Task type for task-specific configuration
Returns:
--------
int
Polling interval in milliseconds
"""
if task_type:
value = self.get_worker_property_value("polling_interval", task_type)
if value is not None:
return int(value)
return self.polling_interval
def get_poll_interval_seconds(self) -> int:
"""
Get polling interval in seconds.
Returns:
--------
int
Polling interval in seconds
"""
return self.polling_interval_seconds
# Properties for commonly used HTTP configuration attributes
@property
def host(self) -> str:
"""Get server host URL."""
if getattr(self, "_http_config", None) is not None:
return self._http_config.host
return getattr(self, "_host", None)
@host.setter
def host(self, value: str) -> None:
"""Set server host URL."""
if getattr(self, "_http_config", None) is not None:
self._http_config.host = value
self._host = value
@property
def debug(self) -> bool:
"""Get debug status."""
return self._http_config.debug
@debug.setter
def debug(self, value: bool) -> None:
"""Set debug status."""
self._http_config.debug = value
if value:
self.logger.setLevel(logging.DEBUG)
self.__log_level = logging.DEBUG
else:
self.logger.setLevel(logging.WARNING)
self.__log_level = logging.INFO
@property
def api_key(self) -> Dict[str, str]:
"""Get API key dictionary."""
return self._http_config.api_key
@api_key.setter
def api_key(self, value: Dict[str, str]) -> None:
"""Set API key dictionary."""
self._http_config.api_key = value
@property
def api_key_prefix(self) -> Dict[str, str]:
"""Get API key prefix dictionary."""
return self._http_config.api_key_prefix
@api_key_prefix.setter
def api_key_prefix(self, value: Dict[str, str]) -> None:
"""Set API key prefix dictionary."""
self._http_config.api_key_prefix = value
# Additional commonly used properties
@property
def username(self) -> Optional[str]:
"""Get username for HTTP basic authentication."""
return self._http_config.username
@username.setter
def username(self, value: Optional[str]) -> None:
"""Set username for HTTP basic authentication."""
self._http_config.username = value
@property
def password(self) -> Optional[str]:
"""Get password for HTTP basic authentication."""
return self._http_config.password
@password.setter
def password(self, value: Optional[str]) -> None:
"""Set password for HTTP basic authentication."""
self._http_config.password = value
@property
def access_token(self) -> Optional[str]:
"""Get access token."""
return self._http_config.access_token
@access_token.setter
def access_token(self, value: Optional[str]) -> None:
"""Set access token."""
self._http_config.access_token = value
@property
def verify_ssl(self) -> bool:
"""Get SSL verification status."""
return self._http_config.verify_ssl
@verify_ssl.setter
def verify_ssl(self, value: bool) -> None:
"""Set SSL verification status."""
self._http_config.verify_ssl = value
@property
def ssl_ca_cert(self) -> Optional[str]:
"""Get SSL CA certificate path."""
return self._http_config.ssl_ca_cert
@ssl_ca_cert.setter
def ssl_ca_cert(self, value: Optional[str]) -> None:
"""Set SSL CA certificate path."""
self._http_config.ssl_ca_cert = value
@property
def retries(self) -> Optional[int]:
"""Get number of retries."""
return self._http_config.retries
@retries.setter
def retries(self, value: Optional[int]) -> None:
"""Set number of retries."""
self._http_config.retries = value
@property
def logger_format(self) -> str:
"""Get logger format."""
return self.__logger_format
@logger_format.setter
def logger_format(self, value: str) -> None:
"""Set logger format."""
self.__logger_format = value
@property
def log_level(self) -> int:
"""Get log level."""
return self.__log_level
def apply_logging_config(self, log_format: Optional[str] = None, level=None):
"""Apply logging configuration for the application."""
if self.is_logger_config_applied:
return
if log_format is None:
log_format = self.logger_format
if level is None:
level = self.__log_level
logging.basicConfig(format=log_format, level=level)
self.is_logger_config_applied = True
@staticmethod
def get_logging_formatted_name(name):
"""Format a logger name with the current process ID."""
return f"[pid:{os.getpid()}] {name}"
@property
def ui_host(self):
return self.__ui_host
# For any other attributes, delegate to the HTTP configuration
def __getattr__(self, name: str) -> Any:
"""Delegate attribute access to underlying HTTP configuration."""
if "_http_config" not in self.__dict__ or self._http_config is None:
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{name}'"
)
return getattr(self._http_config, name)