mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
30 lines
850 B
Python
30 lines
850 B
Python
|
import logging
|
||
|
|
||
|
from extras.choices import JobResultStatusChoices
|
||
|
from netbox.search.backends import search_backend
|
||
|
from .choices import *
|
||
|
from .exceptions import SyncError
|
||
|
from .models import DataSource
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
def sync_datasource(job_result, *args, **kwargs):
|
||
|
"""
|
||
|
Call sync() on a DataSource.
|
||
|
"""
|
||
|
datasource = DataSource.objects.get(name=job_result.name)
|
||
|
|
||
|
try:
|
||
|
job_result.start()
|
||
|
datasource.sync()
|
||
|
|
||
|
# Update the search cache for DataFiles belonging to this source
|
||
|
search_backend.cache(datasource.datafiles.iterator())
|
||
|
|
||
|
except SyncError as e:
|
||
|
job_result.set_status(JobResultStatusChoices.STATUS_ERRORED)
|
||
|
job_result.save()
|
||
|
DataSource.objects.filter(pk=datasource.pk).update(status=DataSourceStatusChoices.FAILED)
|
||
|
logging.error(e)
|