[PR #17938] [MERGED] 7848 Add RQ API #15234

Closed
opened 2025-12-30 00:20:46 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/17938
Author: @arthanson
Created: 11/5/2024
Status: Merged
Merged: 11/26/2024
Merged by: @jeremystretch

Base: featureHead: 7848-rq-api


📝 Commits (10+)

  • 1ab38c3 7848 Add Background Tasks (RQ) to API
  • c8f13ad 7848 Tasks
  • 72f9746 7848 cleanup
  • a327c91 Merge branch 'feature' into 7848-rq-api
  • 21a61dc 7848 add worker support
  • b9f4f93 7848 switch to APIView
  • 088ab82 7848 Task detail view
  • afb18d4 7848 Task enqueue, requeue, stop
  • d26e2a1 7848 Task enqueue, requeue, stop
  • 38132a5 7848 Task enqueue, requeue, stop

📊 Changes

9 files changed (+612 additions, -99 deletions)

View changed files

📝 netbox/core/api/schema.py (+3 -0)
📝 netbox/core/api/serializers.py (+1 -0)
netbox/core/api/serializers_/tasks.py (+87 -0)
📝 netbox/core/api/urls.py (+4 -1)
📝 netbox/core/api/views.py (+161 -0)
📝 netbox/core/tests/test_api.py (+169 -1)
netbox/core/utils.py (+155 -0)
📝 netbox/core/views.py (+7 -97)
📝 netbox/netbox/api/pagination.py (+25 -0)

📄 Description

Fixes: #7848

Here is a testing command to add some tasks for testing the API response, create a temporary create_task management command within core.

Used APIView on the API's and not ViewSet as they don't fit with a ViewSet / only one operation per API can't Get, List, Delete, etc. and the URL structure doesn't directly conform with the pk used by ViewSet, however the side-effect of this is it doesn't appear in the browsable API.

from django_rq.management.commands.rqworker import Command as _Command

from datetime import datetime, timedelta
from django_rq import get_queue
from rq.job import JobStatus
from rq.registry import (
    DeferredJobRegistry,
    FailedJobRegistry,
    FinishedJobRegistry,
    ScheduledJobRegistry,
    StartedJobRegistry
    )


class Command(_Command):
    # Dummy worker functions
    @staticmethod
    def dummy_job_default():
        return "Job finished"

    @staticmethod
    def dummy_job_high():
        return "Job finished"

    @staticmethod
    def dummy_job_failing():
        raise Exception("Job failed")

    def handle(self, *args, **options):
        queue = get_queue('default')

        job = queue.enqueue(self.dummy_job_default)
        job.set_status(JobStatus.FINISHED)
        registry = FinishedJobRegistry(queue.name, queue.connection)
        registry.add(job, -1)

        job = queue.enqueue(self.dummy_job_default)
        job.set_status(JobStatus.FAILED)
        registry = FailedJobRegistry(queue.name, queue.connection)
        registry.add(job, -1)

        job = queue.enqueue(self.dummy_job_default)
        job.set_status(JobStatus.DEFERRED)
        registry = DeferredJobRegistry(queue.name, queue.connection)
        registry.add(job, -1)

        job = queue.enqueue(self.dummy_job_default)
        job.set_status(JobStatus.STARTED)
        registry = StartedJobRegistry(queue.name, queue.connection)
        registry.add(job, -1)

        job = queue.enqueue(self.dummy_job_default)
        job.set_status(JobStatus.SCHEDULED)
        when = datetime.now() + timedelta(hours=8)
        registry = ScheduledJobRegistry(queue.name, queue.connection)
        registry.schedule(job, when)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/17938 **Author:** [@arthanson](https://github.com/arthanson) **Created:** 11/5/2024 **Status:** ✅ Merged **Merged:** 11/26/2024 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `feature` ← **Head:** `7848-rq-api` --- ### 📝 Commits (10+) - [`1ab38c3`](https://github.com/netbox-community/netbox/commit/1ab38c3a45a647993f902e5feb81bfe845ec67ef) 7848 Add Background Tasks (RQ) to API - [`c8f13ad`](https://github.com/netbox-community/netbox/commit/c8f13ad2a5989922fd1ce4a344078e720db5554a) 7848 Tasks - [`72f9746`](https://github.com/netbox-community/netbox/commit/72f974604dfa0599806690c360c5ddae617dc918) 7848 cleanup - [`a327c91`](https://github.com/netbox-community/netbox/commit/a327c916c057a64504de97db1674d718f770239b) Merge branch 'feature' into 7848-rq-api - [`21a61dc`](https://github.com/netbox-community/netbox/commit/21a61dcb66184214b820c716ec4d633a5b9cbaf3) 7848 add worker support - [`b9f4f93`](https://github.com/netbox-community/netbox/commit/b9f4f93ca050622b14d73748dfac8e6ebe92d9e9) 7848 switch to APIView - [`088ab82`](https://github.com/netbox-community/netbox/commit/088ab820cdbdefac36de0489712b205ce3f3ffd3) 7848 Task detail view - [`afb18d4`](https://github.com/netbox-community/netbox/commit/afb18d4ea9724bb61e8052f7d9e72a7d088109f4) 7848 Task enqueue, requeue, stop - [`d26e2a1`](https://github.com/netbox-community/netbox/commit/d26e2a1746960aed50c9a928a0b2ba0418c7d1b8) 7848 Task enqueue, requeue, stop - [`38132a5`](https://github.com/netbox-community/netbox/commit/38132a55f32df9654055123a759f7365e9e97762) 7848 Task enqueue, requeue, stop ### 📊 Changes **9 files changed** (+612 additions, -99 deletions) <details> <summary>View changed files</summary> 📝 `netbox/core/api/schema.py` (+3 -0) 📝 `netbox/core/api/serializers.py` (+1 -0) ➕ `netbox/core/api/serializers_/tasks.py` (+87 -0) 📝 `netbox/core/api/urls.py` (+4 -1) 📝 `netbox/core/api/views.py` (+161 -0) 📝 `netbox/core/tests/test_api.py` (+169 -1) ➕ `netbox/core/utils.py` (+155 -0) 📝 `netbox/core/views.py` (+7 -97) 📝 `netbox/netbox/api/pagination.py` (+25 -0) </details> ### 📄 Description ### Fixes: #7848 Here is a testing command to add some tasks for testing the API response, create a temporary create_task management command within core. Used APIView on the API's and not ViewSet as they don't fit with a ViewSet / only one operation per API can't Get, List, Delete, etc. and the URL structure doesn't directly conform with the pk used by ViewSet, however the side-effect of this is it doesn't appear in the browsable API. ``` from django_rq.management.commands.rqworker import Command as _Command from datetime import datetime, timedelta from django_rq import get_queue from rq.job import JobStatus from rq.registry import ( DeferredJobRegistry, FailedJobRegistry, FinishedJobRegistry, ScheduledJobRegistry, StartedJobRegistry ) class Command(_Command): # Dummy worker functions @staticmethod def dummy_job_default(): return "Job finished" @staticmethod def dummy_job_high(): return "Job finished" @staticmethod def dummy_job_failing(): raise Exception("Job failed") def handle(self, *args, **options): queue = get_queue('default') job = queue.enqueue(self.dummy_job_default) job.set_status(JobStatus.FINISHED) registry = FinishedJobRegistry(queue.name, queue.connection) registry.add(job, -1) job = queue.enqueue(self.dummy_job_default) job.set_status(JobStatus.FAILED) registry = FailedJobRegistry(queue.name, queue.connection) registry.add(job, -1) job = queue.enqueue(self.dummy_job_default) job.set_status(JobStatus.DEFERRED) registry = DeferredJobRegistry(queue.name, queue.connection) registry.add(job, -1) job = queue.enqueue(self.dummy_job_default) job.set_status(JobStatus.STARTED) registry = StartedJobRegistry(queue.name, queue.connection) registry.add(job, -1) job = queue.enqueue(self.dummy_job_default) job.set_status(JobStatus.SCHEDULED) when = datetime.now() + timedelta(hours=8) registry = ScheduledJobRegistry(queue.name, queue.connection) registry.schedule(job, when) ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:20:46 +01:00
adam closed this issue 2025-12-30 00:20:46 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15234