Skip to content

Commit

Permalink
Restore using import_string for jobs
Browse files Browse the repository at this point in the history
Using the 'import_string()' utility from Django allows the job script
class to be simplified, as module imports no longer need to avoid loops.
This should make it easier to queue and maintain jobs.
  • Loading branch information
alehaa committed Jun 20, 2024
1 parent 5fab8e4 commit 957bc3d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 4 additions & 5 deletions netbox/core/jobs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import logging

from netbox.search.backends import search_backend
from utilities.jobs import BackgroundJob
from .choices import DataSourceStatusChoices
from .exceptions import SyncError
from .models import DataSource

logger = logging.getLogger(__name__)

Expand All @@ -12,11 +16,6 @@ class SyncDataSourceJob(BackgroundJob):

@classmethod
def run(cls, job, *args, **kwargs):
from netbox.search.backends import search_backend
from .choices import DataSourceStatusChoices
from .exceptions import SyncError
from .models import DataSource

datasource = DataSource.objects.get(pk=job.object_id)

try:
Expand Down
3 changes: 2 additions & 1 deletion netbox/core/models/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.module_loading import import_string
from django.utils.translation import gettext as _

from netbox.constants import CENSOR_TOKEN, CENSOR_TOKEN_CHANGED
Expand Down Expand Up @@ -161,7 +162,7 @@ def enqueue_sync_job(self, request):
DataSource.objects.filter(pk=self.pk).update(status=self.status)

# Enqueue a sync job
from ..jobs import SyncDataSourceJob
SyncDataSourceJob = import_string('core.jobs.SyncDataSourceJob')
return SyncDataSourceJob.enqueue(
instance=self,
user=request.user
Expand Down

0 comments on commit 957bc3d

Please sign in to comment.