FastAPI Worker
FastAPIWorker(inventory: str, broker: str, worker_name: str, exit_event=None, init_done_event=None, log_level: str = None, log_queue: object = None)
¤
Bases: NFPWorker
FastAPIWorker is a worker class that integrates with FastAPI and Uvicorn to serve a FastAPI application. It handles initialization, starting the server, and managing bearer tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inventory
|
str
|
Inventory configuration for the worker. |
required |
broker
|
str
|
Broker URL to connect to. |
required |
worker_name
|
str
|
Name of this worker. |
required |
exit_event
|
Event
|
Event to signal worker to stop/exit. |
None
|
init_done_event
|
Event
|
Event to signal when worker is done initializing. |
None
|
log_level
|
str
|
Logging level for this worker. |
None
|
log_queue
|
object
|
Queue for logging. |
None
|
Source code in norfab\workers\fastapi_worker.py
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 |
|
_get_diskcache() -> FanoutCache
¤
Initializes and returns a FanoutCache object.
The FanoutCache is configured with the following parameters:
- directory: The directory where the cache will be stored.
- shards: Number of shards to use for the cache.
- timeout: Timeout for cache operations in seconds.
- size_limit: Maximum size of the cache in bytes.
Returns:
Name | Type | Description |
---|---|---|
FanoutCache |
FanoutCache
|
An instance of FanoutCache configured with the specified parameters. |
Source code in norfab\workers\fastapi_worker.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
fastapi_start()
¤
Starts the FastAPI server.
This method initializes the FastAPI application using the provided configuration, starts the Uvicorn server in a separate thread, and waits for the server to be fully started before logging the server's URL.
Steps:
- Create the FastAPI application using
make_fast_api_app
. - Configure the Uvicorn server with the application and settings.
- Start the Uvicorn server in a new thread.
- Wait for the server to start.
- Log the server's URL.'
Attributes:
Name | Type | Description |
---|---|---|
self.app |
FastAPI
|
The FastAPI application instance. |
self.uvicorn_server |
Server
|
The Uvicorn server instance. |
self.uvicorn_server_thread |
Thread
|
The thread running the Uvicorn server. |
Raises:
Type | Description |
---|---|
Exception
|
If the server fails to start. |
Source code in norfab\workers\fastapi_worker.py
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 |
|
worker_exit()
¤
Terminates the current process by sending a SIGTERM signal to itself.
This method retrieves the current process ID using os.getpid()
and then
sends a SIGTERM signal to terminate the process using os.kill()
.
Source code in norfab\workers\fastapi_worker.py
141 142 143 144 145 146 147 148 |
|
get_version()
¤
Produce a report of the versions of various Python packages.
This method collects the versions of several specified Python packages and returns them in a dictionary.
Returns:
Name | Type | Description |
---|---|---|
Result |
An object containing the task name and a dictionary with the package names as keys and their respective versions as values. |
Source code in norfab\workers\fastapi_worker.py
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 |
|
get_inventory() -> Dict
¤
Retrieve the inventory of the FastAPI worker.
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary containing the combined inventory of FastAPI and Uvicorn. |
Source code in norfab\workers\fastapi_worker.py
179 180 181 182 183 184 185 186 187 188 189 |
|
bearer_token_store(username: str, token: str, expire: int = None) -> bool
¤
Method to store a bearer token in the database.
This method stores a bearer token associated with a username in the cache.
If an expiration time is not provided, it retrieves the default token TTL from the FastAPI inventory configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
str - The name of the user to store the token for. |
required |
token
|
str
|
str - The token string to store. |
required |
expire
|
int
|
int, optional - The number of seconds before the token expires. |
None
|
Returns:
Type | Description |
---|---|
bool
|
bool - Returns True if the token is successfully stored. |
Source code in norfab\workers\fastapi_worker.py
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 |
|
bearer_token_delete(username: str = None, token: str = None) -> bool
¤
Deletes a bearer token from the cache. This method removes a bearer token from the cache based on either the token itself or the associated username.
If a token is provided, it will be removed directly. If a username is provided, all tokens associated with that username will be evicted from the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username associated with the token(s) to be removed. Defaults to None. |
None
|
token
|
str
|
The bearer token to be removed. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the operation was successful, otherwise raises an exception. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the token removal from the cache fails. |
Exception
|
If neither username nor token is provided. |
Source code in norfab\workers\fastapi_worker.py
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 |
|
bearer_token_list(username: str = None) -> list
¤
Retrieves a list of bearer tokens from the cache, optionally filtered by username.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username to filter tokens by. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of dictionaries containing token information. Each dictionary contains:
|
If no tokens are found, a list with a single dictionary containing empty strings for all fields is returned.
Source code in norfab\workers\fastapi_worker.py
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 |
|
bearer_token_check(token: str) -> bool
¤
Checks if the provided bearer token is present in the cache and still active.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
The bearer token to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the token is found in the cache, False otherwise. |
Source code in norfab\workers\fastapi_worker.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
|
make_fast_api_app(worker: object, config: dict) -> FastAPI
¤
Create a FastAPI application with endpoints for posting, getting, and running jobs.
This function sets up a FastAPI application with three endpoints:
- POST /job: To post a job to the NorFab service.
- GET /job: To get job results from the NorFab service.
- POST /job/run: To run a job and return job results synchronously.
Each endpoint requires a bearer token for authentication, which is validated against the worker's token database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
worker
|
object
|
An object representing the worker that will handle the job requests. |
required |
config
|
dict
|
A dictionary of configuration options for the FastAPI application. |
required |
Returns:
Name | Type | Description |
---|---|---|
FastAPI |
FastAPI
|
A FastAPI application instance. |
Source code in norfab\workers\fastapi_worker.py
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 |
|