Skip to content

Improve hot-reloading of custom scripts #7803

@kkthxbye-code

Description

@kkthxbye-code

NetBox version

V3.0.9

Feature type

Change to existing functionality

Proposed functionality

Allow renaming of classes in custom scripts. Currently if you rename a class or remove a class from a file, netbox will throw OSError : could not find class definition and the script functionality will be broken until the application server is restarted.

The reason for this is that python uses sys.modules to cache all imported modules. When you change a class in the script and reload it in get_scripts, which is called on both the list view and when executing scripts, the module includes both the old name for the class and the new name (not sure why), causing the import to fail on the old name.

The solution is pretty simple, deleting the module from sys.modules. A simple implementation can be seen here:

kkthxbye-code@ae6ed97

I wasn't able to notice any performance regressions, the extra load should be minimal even with a lot of scripts. The advantage is that the loaded script is actually correct.

Use case

Scripts already allow changing code without restart, with the requested change you will also be able to rename or delete script classes.

Database changes

None

External dependencies

None

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions