NFAPI (Python API)
Utility class to implement Python API for interfacing with NorFab.
NorFab Python API Client initialization class
from norfab.core.nfapi import NorFab
nf = NorFab(inventory="./inventory.yaml")
nf.start(start_broker=True, workers=["my-worker-1"])
NFCLIENT = nf.make_client()
or using dictionary inventory data
from norfab.core.nfapi import NorFab
data = {
'broker': {'endpoint': 'tcp://127.0.0.1:5555'},
'workers': {'my-worker-1': ['workers/common.yaml'],
}
nf = NorFab(inventory_data=data, base_dir="./")
nf.start(start_broker=True, workers=["my-worker-1"])
NFCLIENT = nf.make_client()
Args:
inventory: OS path to NorFab inventory YAML file
inventory_data: dictionary with NorFab inventory
base_dir: OS path to base directory to anchor NorFab at
log_level: one or supported logging levels - CRITICAL
, ERROR
, WARNING
, INFO
, DEBUG
Source code in norfab\core\nfapi.py
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 |
|
register_plugins()
¤
Registers worker plugins by iterating through the entry points in the 'norfab.workers' group and registering each worker plugin.
This method loads each entry point and registers it using the
register_worker_plugin
method.
Raises: Any exceptions raised by the entry point loading or registration process.
Source code in norfab\core\nfapi.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
register_worker_plugin(service_name, worker_plugin)
¤
Registers a worker plugin for a given service.
This method registers a worker plugin under the specified service name. If a plugin is already registered under the same service name and it is different from the provided plugin, an exception is raised.
Args: service_name (str): The name of the service to register the plugin for. worker_plugin (object): The worker plugin to be registered.
Raises: norfab_exceptions.ServicePluginAlreadyRegistered: If a different plugin is already registered under the same service name.
Source code in norfab\core\nfapi.py
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 |
|
handle_ctrl_c(signum, frame)
¤
Handle the CTRL-C signal (SIGINT) to gracefully exit the application.
This method is called when the user interrupts the program with a CTRL-C
signal. It logs the interruption, performs necessary cleanup by calling
self.destroy()
, and then signals termination to the main process.
Args: signum (int): The signal number (should be SIGINT). frame (FrameType): The current stack frame.
Note: This method reassigns the SIGINT signal to the default handler and sends the SIGINT signal to the current process to ensure proper termination.
Source code in norfab\core\nfapi.py
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 |
|
setup_logging()
¤
Sets up logging configuration and starts a log queue listener.
This method updates the logging levels for all handlers based on the inventory, configures the logging system using the provided inventory, and starts a log queue listener to process logs from child processes.
Source code in norfab\core\nfapi.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
start_broker()
¤
Starts the broker process if a broker endpoint is defined. This method initializes and starts a separate process for the broker using the provided broker endpoint. It waits for the broker to signal that it has fully initiated, with a timeout of 30 seconds. If the broker fails to start within this time, the method logs an error message and raises a SystemExit exception.
Raises: SystemExit: If the broker fails to start within 30 seconds.
Logs: Info: When the broker starts successfully. Error: If no broker endpoint is defined or if the broker fails to start.
Source code in norfab\core\nfapi.py
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 |
|
start_worker(worker_name, worker_data)
¤
Starts a worker process if it is not already running.
Args: worker_name (str): The name of the worker to start. worker_data (dict): A dictionary containing data about the worker, including any dependencies.
Raises: RuntimeError: If a dependent process is not alive. norfab_exceptions.ServicePluginNotRegistered: If no worker plugin is registered for the worker's service.
Returns: None
Source code in norfab\core\nfapi.py
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 |
|
start(start_broker=True, workers=True)
¤
Starts the broker and specified workers.
Args: start_broker (bool): If True, starts the broker if it is defined in the inventory topology. workers (Union[bool, list]): Determines which workers to start. If True, starts all workers defined in the inventory topology. If False or None, no workers are started. If a list, starts the specified workers.
Returns: None
Raises: KeyError: If a worker fails to start due to missing inventory data. FileNotFoundError: If a worker fails to start because the inventory file is not found. Exception: If a worker fails to start due to any other error.
Notes: - The method waits for all workers to initialize within a specified timeout period. - If the initialization timeout expires, an error is logged and the system is destroyed. - After starting the workers, any startup hooks defined in the inventory are executed.
Source code in norfab\core\nfapi.py
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 |
|
run()
¤
Runs the main loop until a termination signal (CTRL+C) is received.
This method checks if there are any broker or worker processes running.
If none are detected, it logs a critical message and exits.
Otherwise, it enters a loop that continues to run until the exiting
flag is set to True.
Source code in norfab\core\nfapi.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
destroy()
¤
Gracefully stop all NORFAB processes and clean up resources.
This method performs the following steps:
- Executes any registered exit hooks.
- Sets the
exiting
flag to indicate that NORFAB is shutting down. - Stops all client processes.
- Stops all worker processes and waits for them to terminate.
- Stops the broker process and waits for it to terminate.
- Stops the logging queue listener.
Returns: None
Source code in norfab\core\nfapi.py
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 |
|
make_client(broker_endpoint=None)
¤
Creates and returns an NFPClient instance.
Args: broker_endpoint (str, optional): The broker endpoint to connect to. If not provided, the instance's broker_endpoint attribute will be used.
Returns: NFPClient: The created client instance if a broker endpoint is defined. None: If no broker endpoint is defined.
Raises: None
Notes: If this is the first client being created, it will be assigned to the instance's client attribute.
Source code in norfab\core\nfapi.py
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 |
|