nautobot.apps.jobs
¶
Nautobot Jobs API.
nautobot.apps.jobs.BaseJob
¶
Bases: Task
Base model for jobs.
Users can subclass this directly if they want to provide their own base class for implementing multiple jobs with shared functionality; if no such sharing is required, use Job class instead.
Jobs must define at minimum a run method.
Source code in nautobot/extras/jobs.py
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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 |
|
Meta
¶
Metaclass attributes - subclasses can define any or all of the following attributes:
- name (str)
- description (str)
- hidden (bool)
- field_order (list)
- approval_required (bool)
- soft_time_limit (int)
- time_limit (int)
- has_sensitive_variables (bool)
- task_queues (list)
Source code in nautobot/extras/jobs.py
after_return(status, retval, task_id, args, kwargs, einfo)
¶
Handler called after the task returns.
Parameters status - Current task state. retval - Task return value/exception. task_id - Unique id of the task. args - Original arguments for the task that returned. kwargs - Original keyword arguments for the task that returned.
Keyword Arguments einfo - ExceptionInfo instance, containing the traceback (if any).
Returns:
Type | Description |
---|---|
None
|
The return value of this handler is ignored. |
Source code in nautobot/extras/jobs.py
apply(args=None, kwargs=None, link=None, link_error=None, task_id=None, retries=None, throw=None, logfile=None, loglevel=None, headers=None, **options)
¶
Fix celery's apply method to propagate options to the task result
Source code in nautobot/extras/jobs.py
as_form(data=None, files=None, initial=None, approval_view=False)
classmethod
¶
Return a Django form suitable for populating the context data required to run this Job.
approval_view
will disable all fields from modification and is used to display the form
during a approval review workflow.
Source code in nautobot/extras/jobs.py
as_form_class()
classmethod
¶
Dynamically generate a Django form class corresponding to the variables in this Job.
In most cases you should use .as_form()
instead of calling this method directly.
Source code in nautobot/extras/jobs.py
before_start(task_id, args, kwargs)
¶
Handler called before the task starts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_id |
str
|
Unique id of the task to execute. |
required |
args |
Tuple
|
Original arguments for the task to execute. |
required |
kwargs |
Dict
|
Original keyword arguments for the task to execute. |
required |
Returns:
Type | Description |
---|---|
None
|
The return value of this handler is ignored. |
Source code in nautobot/extras/jobs.py
class_path()
¶
Unique identifier of a specific Job class, in the form
Examples:
my_script.MyScript - Local Job nautobot.core.jobs.MySystemJob - System Job my_plugin.jobs.MyPluginJob - App-provided Job git_repository.jobs.myjob.MyJob - GitRepository Job
Source code in nautobot/extras/jobs.py
class_path_dotted()
¶
Dotted class_path, suitable for use in things like Python logger names.
Deprecated as of Nautobot 2.0: just use .class_path instead.
Source code in nautobot/extras/jobs.py
class_path_js_escaped()
¶
Escape various characters so that the class_path can be used as a jQuery selector.
clear_cache()
¶
Clear all cached properties on this instance without accessing them. This is required because celery reuses task instances for multiple runs.
Source code in nautobot/extras/jobs.py
delete_files(*files_to_delete)
¶
Given an unpacked list of primary keys for FileProxy
objects, delete them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files_to_delete |
*args
|
List of primary keys to delete |
()
|
Returns:
Type | Description |
---|---|
int
|
number of objects deleted |
Source code in nautobot/extras/jobs.py
deserialize_data(data)
classmethod
¶
Given data input for a job execution, deserialize it by resolving object references using defined variables.
This converts a list of pk's back into a QuerySet for MultiObjectVar instances and single pk values into model instances for ObjectVar.
Note that when resolving querysets or model instances by their PK, we do not catch DoesNotExist
exceptions here, we leave it up the caller to handle those cases. The normal job execution code
path would consider this a failure of the job execution, as described in nautobot.extras.jobs.run_job
.
Source code in nautobot/extras/jobs.py
load_file(pk)
staticmethod
¶
Load a file proxy stored in the database by primary key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pk |
uuid
|
Primary key of the |
required |
Returns:
Type | Description |
---|---|
FileProxy
|
A File-like object |
Source code in nautobot/extras/jobs.py
load_json(filename)
¶
Return data from a JSON file
load_yaml(filename)
¶
Return data from a YAML file
on_bound(app)
classmethod
¶
Called when the task is bound to an app.
Note
This class method can be defined to do additional actions when the task class is bound to an app.
on_failure(exc, task_id, args, kwargs, einfo)
¶
Error handler.
This is run by the worker when the task fails.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc |
Exception
|
The exception raised by the task. |
required |
task_id |
str
|
Unique id of the failed task. |
required |
args |
Tuple
|
Original arguments for the task that failed. |
required |
kwargs |
Dict
|
Original keyword arguments for the task that failed. |
required |
einfo |
~billiard.einfo.ExceptionInfo
|
Exception information. |
required |
Returns:
Type | Description |
---|---|
None
|
The return value of this handler is ignored. |
Source code in nautobot/extras/jobs.py
on_retry(exc, task_id, args, kwargs, einfo)
¶
Retry handler.
This is run by the worker when the task is to be retried.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc |
Exception
|
The exception sent to :meth: |
required |
task_id |
str
|
Unique id of the retried task. |
required |
args |
Tuple
|
Original arguments for the retried task. |
required |
kwargs |
Dict
|
Original keyword arguments for the retried task. |
required |
einfo |
~billiard.einfo.ExceptionInfo
|
Exception information. |
required |
Returns:
Type | Description |
---|---|
None
|
The return value of this handler is ignored. |
Source code in nautobot/extras/jobs.py
on_success(retval, task_id, args, kwargs)
¶
Success handler.
Run by the worker if the task executes successfully.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retval |
Any
|
The return value of the task. |
required |
task_id |
str
|
Unique id of the executed task. |
required |
args |
Tuple
|
Original arguments for the executed task. |
required |
kwargs |
Dict
|
Original keyword arguments for the executed task. |
required |
Returns:
Type | Description |
---|---|
None
|
The return value of this handler is ignored. |
Source code in nautobot/extras/jobs.py
prepare_job_kwargs(job_kwargs)
classmethod
¶
Process dict and return kwargs that exist as ScriptVariables on this job.
properties_dict()
¶
Return all relevant classproperties as a dict.
Used for convenient rendering into job_edit.html via the json_script
template tag.
Source code in nautobot/extras/jobs.py
run(*args, **kwargs)
¶
save_file(uploaded_file)
staticmethod
¶
Save an uploaded file to the database as a file proxy and return the primary key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uploaded_file |
file
|
File handle of file to save to database |
required |
Returns:
Type | Description |
---|---|
uuid
|
The pk of the |
Source code in nautobot/extras/jobs.py
serialize_data(data)
staticmethod
¶
This method parses input data (from JobForm usually) and returns a dict which is safe to serialize
Here we convert the QuerySet of a MultiObjectVar to a list of the pk's and the model instance of an ObjectVar into the pk value.
These are converted back during job execution.
Source code in nautobot/extras/jobs.py
shadow_name(args, kwargs, options)
¶
Override for custom task name in worker logs/monitoring.
Example
from celery.utils.imports import qualname
def shadow_name(task, args, kwargs, options): return qualname(args[0])
@app.task(shadow_name=shadow_name, serializer='pickle') def apply_function_async(fun, args, kwargs): return fun(args, **kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Tuple
|
Task positional arguments. |
required |
kwargs |
Dict
|
Task keyword arguments. |
required |
options |
Dict
|
Task execution options. |
required |
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.BooleanVar
¶
Bases: ScriptVariable
Boolean representation (true/false). Renders as a checkbox.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.ChoiceVar
¶
Bases: ScriptVariable
Select one of several predefined static choices, passed as a list of two-tuples. Example:
color = ChoiceVar(
choices=(
('#ff0000', 'Red'),
('#00ff00', 'Green'),
('#0000ff', 'Blue')
)
)
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.DatabaseFileField
¶
Bases: forms.FileField
Specialized FileField
for use with DatabaseFileStorage
storage backend.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.DryRunVar
¶
Bases: BooleanVar
Special boolean variable that bypasses approval requirements if this is set to True on job execution.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.FileVar
¶
nautobot.apps.jobs.GitRepositoryDryRun
¶
Bases: Job
System Job to perform a dry run on a Git repository.
Source code in nautobot/core/jobs/__init__.py
nautobot.apps.jobs.GitRepositorySync
¶
Bases: Job
System job to clone and/or pull a Git repository, then invoke refresh_datasource_content()
.
Source code in nautobot/core/jobs/__init__.py
nautobot.apps.jobs.IPAddressVar
¶
nautobot.apps.jobs.IPAddressWithMaskVar
¶
nautobot.apps.jobs.IPNetworkVar
¶
Bases: ScriptVariable
An IPv4 or IPv6 prefix.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.IntegerVar
¶
Bases: ScriptVariable
Integer representation. Can enforce minimum/maximum values.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.Job
¶
nautobot.apps.jobs.JobButtonReceiver
¶
Bases: Job
Base class for job button receivers. Job button receivers are jobs that are initiated from UI buttons and are not intended to be run from the job form UI or API like standard jobs.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.JobHookReceiver
¶
Bases: Job
Base class for job hook receivers. Job hook receivers are jobs that are initiated from object changes and are not intended to be run from the UI or API like standard jobs.
Source code in nautobot/extras/jobs.py
receive_job_hook(change, action, changed_object)
¶
Method to be implemented by concrete JobHookReceiver subclasses.
:param change: an instance of nautobot.extras.models.ObjectChange
:param action: a string with the action performed on the changed object ("create", "update" or "delete")
:param changed_object: an instance of the object that was changed, or None
if the object has been deleted
Source code in nautobot/extras/jobs.py
run(object_change)
¶
JobHookReceiver subclasses generally shouldn't need to override this method.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.MultiChoiceVar
¶
nautobot.apps.jobs.MultiObjectVar
¶
nautobot.apps.jobs.NautobotKombuJSONEncoder
¶
Bases: JSONEncoder
Custom json encoder based on restframework's JSONEncoder that serializes objects that implement
the nautobot_serialize()
method via the __nautobot_type__
interface. This is useful
in passing special objects to and from Celery tasks.
This pattern should generally be avoided by passing pointers to persisted objects to the Celery tasks and retrieving them from within the task execution. While this is always possible for model instances (which covers 99% of use cases), for rare instances where it does not, and the actual object must be passed, this pattern allows for encoding and decoding of such objects.
It requires a conforming class to implement the instance method nautobot_serialize()
which
returns a json serializable dictionary of the object representation. The class must also implement
the nautobot_deserialize()
class method which takes the dictionary representation and returns
an actual instance of the class.
Source code in nautobot/core/celery/encoders.py
nautobot.apps.jobs.ObjectVar
¶
Bases: ScriptVariable
A single object within Nautobot.
:param model: The Nautobot model being referenced :param display_field: The attribute of the returned object to display in the selection list (default: 'name') :param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional) :param null_option: The label to use as a "null" selection option (optional)
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.RunJobTaskFailed
¶
nautobot.apps.jobs.ScriptVariable
¶
Base model for script variables
Source code in nautobot/extras/jobs.py
as_field()
¶
Render the variable as a Django form field.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.StringVar
¶
Bases: ScriptVariable
Character string representation. Can enforce minimum/maximum length and/or regex validation.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.TextVar
¶
nautobot.apps.jobs.enqueue_job_hooks(object_change)
¶
Find job hook(s) assigned to this changed object type + action and enqueue them to be processed
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.get_job(class_path)
¶
Retrieve a specific job class by its class_path (
May return None if the job isn't properly registered with Celery at this time.
Source code in nautobot/extras/jobs.py
nautobot.apps.jobs.is_job(obj)
¶
Returns True if the given object is a Job subclass.
nautobot.apps.jobs.is_variable(obj)
¶
nautobot.apps.jobs.register_jobs(*jobs)
¶
Helper method to register jobs with Celery