|
@@ -14,16 +14,13 @@ from django.utils.translation import gettext_lazy as _
|
|
|
from django.views.generic import View
|
|
from django.views.generic import View
|
|
|
from django_rq.queues import get_connection, get_queue_by_index, get_redis_connection
|
|
from django_rq.queues import get_connection, get_queue_by_index, get_redis_connection
|
|
|
from django_rq.settings import QUEUES_MAP, QUEUES_LIST
|
|
from django_rq.settings import QUEUES_MAP, QUEUES_LIST
|
|
|
-from django_rq.utils import get_jobs, get_statistics, stop_jobs
|
|
|
|
|
-from rq import requeue_job
|
|
|
|
|
|
|
+from django_rq.utils import get_statistics
|
|
|
from rq.exceptions import NoSuchJobError
|
|
from rq.exceptions import NoSuchJobError
|
|
|
from rq.job import Job as RQ_Job, JobStatus as RQJobStatus
|
|
from rq.job import Job as RQ_Job, JobStatus as RQJobStatus
|
|
|
-from rq.registry import (
|
|
|
|
|
- DeferredJobRegistry, FailedJobRegistry, FinishedJobRegistry, ScheduledJobRegistry, StartedJobRegistry,
|
|
|
|
|
-)
|
|
|
|
|
from rq.worker import Worker
|
|
from rq.worker import Worker
|
|
|
from rq.worker_registration import clean_worker_registry
|
|
from rq.worker_registration import clean_worker_registry
|
|
|
|
|
|
|
|
|
|
+from core.utils import delete_rq_job, enqueue_rq_job, get_rq_jobs_from_status, requeue_rq_job, stop_rq_job
|
|
|
from netbox.config import get_config, PARAMS
|
|
from netbox.config import get_config, PARAMS
|
|
|
from netbox.views import generic
|
|
from netbox.views import generic
|
|
|
from netbox.views.generic.base import BaseObjectView
|
|
from netbox.views.generic.base import BaseObjectView
|
|
@@ -363,41 +360,12 @@ class BackgroundTaskListView(TableMixin, BaseRQView):
|
|
|
table = tables.BackgroundTaskTable
|
|
table = tables.BackgroundTaskTable
|
|
|
|
|
|
|
|
def get_table_data(self, request, queue, status):
|
|
def get_table_data(self, request, queue, status):
|
|
|
- jobs = []
|
|
|
|
|
|
|
|
|
|
# Call get_jobs() to returned queued tasks
|
|
# Call get_jobs() to returned queued tasks
|
|
|
if status == RQJobStatus.QUEUED:
|
|
if status == RQJobStatus.QUEUED:
|
|
|
return queue.get_jobs()
|
|
return queue.get_jobs()
|
|
|
|
|
|
|
|
- # For other statuses, determine the registry to list (or raise a 404 for invalid statuses)
|
|
|
|
|
- try:
|
|
|
|
|
- registry_cls = {
|
|
|
|
|
- RQJobStatus.STARTED: StartedJobRegistry,
|
|
|
|
|
- RQJobStatus.DEFERRED: DeferredJobRegistry,
|
|
|
|
|
- RQJobStatus.FINISHED: FinishedJobRegistry,
|
|
|
|
|
- RQJobStatus.FAILED: FailedJobRegistry,
|
|
|
|
|
- RQJobStatus.SCHEDULED: ScheduledJobRegistry,
|
|
|
|
|
- }[status]
|
|
|
|
|
- except KeyError:
|
|
|
|
|
- raise Http404
|
|
|
|
|
- registry = registry_cls(queue.name, queue.connection)
|
|
|
|
|
-
|
|
|
|
|
- job_ids = registry.get_job_ids()
|
|
|
|
|
- if status != RQJobStatus.DEFERRED:
|
|
|
|
|
- jobs = get_jobs(queue, job_ids, registry)
|
|
|
|
|
- else:
|
|
|
|
|
- # Deferred jobs require special handling
|
|
|
|
|
- for job_id in job_ids:
|
|
|
|
|
- try:
|
|
|
|
|
- jobs.append(RQ_Job.fetch(job_id, connection=queue.connection, serializer=queue.serializer))
|
|
|
|
|
- except NoSuchJobError:
|
|
|
|
|
- pass
|
|
|
|
|
-
|
|
|
|
|
- if jobs and status == RQJobStatus.SCHEDULED:
|
|
|
|
|
- for job in jobs:
|
|
|
|
|
- job.scheduled_at = registry.get_scheduled_time(job)
|
|
|
|
|
-
|
|
|
|
|
- return jobs
|
|
|
|
|
|
|
+ return get_rq_jobs_from_status(queue, status)
|
|
|
|
|
|
|
|
def get(self, request, queue_index, status):
|
|
def get(self, request, queue_index, status):
|
|
|
queue = get_queue_by_index(queue_index)
|
|
queue = get_queue_by_index(queue_index)
|
|
@@ -463,19 +431,7 @@ class BackgroundTaskDeleteView(BaseRQView):
|
|
|
form = ConfirmationForm(request.POST)
|
|
form = ConfirmationForm(request.POST)
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
if form.is_valid():
|
|
|
- # all the RQ queues should use the same connection
|
|
|
|
|
- config = QUEUES_LIST[0]
|
|
|
|
|
- try:
|
|
|
|
|
- job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),)
|
|
|
|
|
- except NoSuchJobError:
|
|
|
|
|
- raise Http404(_("Job {job_id} not found").format(job_id=job_id))
|
|
|
|
|
-
|
|
|
|
|
- queue_index = QUEUES_MAP[job.origin]
|
|
|
|
|
- queue = get_queue_by_index(queue_index)
|
|
|
|
|
-
|
|
|
|
|
- # Remove job id from queue and delete the actual job
|
|
|
|
|
- queue.connection.lrem(queue.key, 0, job.id)
|
|
|
|
|
- job.delete()
|
|
|
|
|
|
|
+ delete_rq_job(job_id)
|
|
|
messages.success(request, _('Job {id} has been deleted.').format(id=job_id))
|
|
messages.success(request, _('Job {id} has been deleted.').format(id=job_id))
|
|
|
else:
|
|
else:
|
|
|
messages.error(request, _('Error deleting job {id}: {error}').format(id=job_id, error=form.errors[0]))
|
|
messages.error(request, _('Error deleting job {id}: {error}').format(id=job_id, error=form.errors[0]))
|
|
@@ -486,17 +442,7 @@ class BackgroundTaskDeleteView(BaseRQView):
|
|
|
class BackgroundTaskRequeueView(BaseRQView):
|
|
class BackgroundTaskRequeueView(BaseRQView):
|
|
|
|
|
|
|
|
def get(self, request, job_id):
|
|
def get(self, request, job_id):
|
|
|
- # all the RQ queues should use the same connection
|
|
|
|
|
- config = QUEUES_LIST[0]
|
|
|
|
|
- try:
|
|
|
|
|
- job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),)
|
|
|
|
|
- except NoSuchJobError:
|
|
|
|
|
- raise Http404(_("Job {id} not found.").format(id=job_id))
|
|
|
|
|
-
|
|
|
|
|
- queue_index = QUEUES_MAP[job.origin]
|
|
|
|
|
- queue = get_queue_by_index(queue_index)
|
|
|
|
|
-
|
|
|
|
|
- requeue_job(job_id, connection=queue.connection, serializer=queue.serializer)
|
|
|
|
|
|
|
+ requeue_rq_job(job_id)
|
|
|
messages.success(request, _('Job {id} has been re-enqueued.').format(id=job_id))
|
|
messages.success(request, _('Job {id} has been re-enqueued.').format(id=job_id))
|
|
|
return redirect(reverse('core:background_task', args=[job_id]))
|
|
return redirect(reverse('core:background_task', args=[job_id]))
|
|
|
|
|
|
|
@@ -505,33 +451,7 @@ class BackgroundTaskEnqueueView(BaseRQView):
|
|
|
|
|
|
|
|
def get(self, request, job_id):
|
|
def get(self, request, job_id):
|
|
|
# all the RQ queues should use the same connection
|
|
# all the RQ queues should use the same connection
|
|
|
- config = QUEUES_LIST[0]
|
|
|
|
|
- try:
|
|
|
|
|
- job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),)
|
|
|
|
|
- except NoSuchJobError:
|
|
|
|
|
- raise Http404(_("Job {id} not found.").format(id=job_id))
|
|
|
|
|
-
|
|
|
|
|
- queue_index = QUEUES_MAP[job.origin]
|
|
|
|
|
- queue = get_queue_by_index(queue_index)
|
|
|
|
|
-
|
|
|
|
|
- try:
|
|
|
|
|
- # _enqueue_job is new in RQ 1.14, this is used to enqueue
|
|
|
|
|
- # job regardless of its dependencies
|
|
|
|
|
- queue._enqueue_job(job)
|
|
|
|
|
- except AttributeError:
|
|
|
|
|
- queue.enqueue_job(job)
|
|
|
|
|
-
|
|
|
|
|
- # Remove job from correct registry if needed
|
|
|
|
|
- if job.get_status() == RQJobStatus.DEFERRED:
|
|
|
|
|
- registry = DeferredJobRegistry(queue.name, queue.connection)
|
|
|
|
|
- registry.remove(job)
|
|
|
|
|
- elif job.get_status() == RQJobStatus.FINISHED:
|
|
|
|
|
- registry = FinishedJobRegistry(queue.name, queue.connection)
|
|
|
|
|
- registry.remove(job)
|
|
|
|
|
- elif job.get_status() == RQJobStatus.SCHEDULED:
|
|
|
|
|
- registry = ScheduledJobRegistry(queue.name, queue.connection)
|
|
|
|
|
- registry.remove(job)
|
|
|
|
|
-
|
|
|
|
|
|
|
+ enqueue_rq_job(job_id)
|
|
|
messages.success(request, _('Job {id} has been enqueued.').format(id=job_id))
|
|
messages.success(request, _('Job {id} has been enqueued.').format(id=job_id))
|
|
|
return redirect(reverse('core:background_task', args=[job_id]))
|
|
return redirect(reverse('core:background_task', args=[job_id]))
|
|
|
|
|
|
|
@@ -539,17 +459,7 @@ class BackgroundTaskEnqueueView(BaseRQView):
|
|
|
class BackgroundTaskStopView(BaseRQView):
|
|
class BackgroundTaskStopView(BaseRQView):
|
|
|
|
|
|
|
|
def get(self, request, job_id):
|
|
def get(self, request, job_id):
|
|
|
- # all the RQ queues should use the same connection
|
|
|
|
|
- config = QUEUES_LIST[0]
|
|
|
|
|
- try:
|
|
|
|
|
- job = RQ_Job.fetch(job_id, connection=get_redis_connection(config['connection_config']),)
|
|
|
|
|
- except NoSuchJobError:
|
|
|
|
|
- raise Http404(_("Job {job_id} not found").format(job_id=job_id))
|
|
|
|
|
-
|
|
|
|
|
- queue_index = QUEUES_MAP[job.origin]
|
|
|
|
|
- queue = get_queue_by_index(queue_index)
|
|
|
|
|
-
|
|
|
|
|
- stopped_jobs = stop_jobs(queue, job_id)[0]
|
|
|
|
|
|
|
+ stopped_jobs = stop_rq_job(job_id)
|
|
|
if len(stopped_jobs) == 1:
|
|
if len(stopped_jobs) == 1:
|
|
|
messages.success(request, _('Job {id} has been stopped.').format(id=job_id))
|
|
messages.success(request, _('Job {id} has been stopped.').format(id=job_id))
|
|
|
else:
|
|
else:
|