API: add run task view
This commit is contained in:
parent
71b3654942
commit
eb7313fe6b
|
@ -200,3 +200,16 @@ When valid returns message with user id:
|
||||||
"user": 1
|
"user": 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Task View
|
||||||
|
Start a background task
|
||||||
|
POST /api/task/
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"run": "task_name"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
List of valid task names:
|
||||||
|
- **download_pending**: Start the download queue
|
||||||
|
- **rescan_pending**: Rescan your subscriptions
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
"""
|
||||||
|
Functionality:
|
||||||
|
- process tasks from API
|
||||||
|
- validate
|
||||||
|
- handover to celery
|
||||||
|
"""
|
||||||
|
|
||||||
|
from home.src.ta.ta_redis import RedisArchivist
|
||||||
|
from home.tasks import download_pending, update_subscribed
|
||||||
|
|
||||||
|
|
||||||
|
class TaskHandler:
|
||||||
|
"""handle tasks from api"""
|
||||||
|
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def run_task(self):
|
||||||
|
"""map data and run"""
|
||||||
|
task_name = self.data["run"]
|
||||||
|
try:
|
||||||
|
to_run = self.exec_map(task_name)
|
||||||
|
except KeyError as err:
|
||||||
|
print(f"invalid task name {task_name}")
|
||||||
|
raise ValueError from err
|
||||||
|
|
||||||
|
response = to_run()
|
||||||
|
response.update({"task": task_name})
|
||||||
|
return response
|
||||||
|
|
||||||
|
def exec_map(self, task_name):
|
||||||
|
"""map dict key and return function to execute"""
|
||||||
|
exec_map = {
|
||||||
|
"download_pending": self._download_pending,
|
||||||
|
"rescan_pending": self._rescan_pending,
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec_map[task_name]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _rescan_pending():
|
||||||
|
"""look for new items in subscribed channels"""
|
||||||
|
print("rescan subscribed channels")
|
||||||
|
update_subscribed.delay()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _download_pending():
|
||||||
|
"""start the download queue"""
|
||||||
|
print("download pending")
|
||||||
|
running = download_pending.delay()
|
||||||
|
print("set task id: " + running.id)
|
||||||
|
RedisArchivist().set_message("dl_queue_id", running.id, expire=False)
|
||||||
|
return {"success": True}
|
|
@ -11,6 +11,7 @@ from api.views import (
|
||||||
PlaylistApiListView,
|
PlaylistApiListView,
|
||||||
PlaylistApiVideoView,
|
PlaylistApiVideoView,
|
||||||
PlaylistApiView,
|
PlaylistApiView,
|
||||||
|
TaskApiView,
|
||||||
VideoApiListView,
|
VideoApiListView,
|
||||||
VideoApiView,
|
VideoApiView,
|
||||||
VideoProgressView,
|
VideoProgressView,
|
||||||
|
@ -81,4 +82,9 @@ urlpatterns = [
|
||||||
DownloadApiView.as_view(),
|
DownloadApiView.as_view(),
|
||||||
name="api-download",
|
name="api-download",
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"task/",
|
||||||
|
TaskApiView.as_view(),
|
||||||
|
name="api-task",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""all API views"""
|
"""all API views"""
|
||||||
|
|
||||||
from api.src.search_processor import SearchProcess
|
from api.src.search_processor import SearchProcess
|
||||||
|
from api.src.task_processor import TaskHandler
|
||||||
from home.src.download.queue import PendingInteract
|
from home.src.download.queue import PendingInteract
|
||||||
from home.src.es.connect import ElasticWrap
|
from home.src.es.connect import ElasticWrap
|
||||||
from home.src.index.generic import Pagination
|
from home.src.index.generic import Pagination
|
||||||
|
@ -446,3 +447,18 @@ class LoginApiView(ObtainAuthToken):
|
||||||
print(f"returning token for user with id {user.pk}")
|
print(f"returning token for user with id {user.pk}")
|
||||||
|
|
||||||
return Response({"token": token.key, "user_id": user.pk})
|
return Response({"token": token.key, "user_id": user.pk})
|
||||||
|
|
||||||
|
|
||||||
|
class TaskApiView(ApiBaseView):
|
||||||
|
"""resolves to /api/task/
|
||||||
|
POST: start a new background task
|
||||||
|
"""
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
"""handle post request"""
|
||||||
|
|
||||||
|
data = request.data
|
||||||
|
print(data)
|
||||||
|
response = TaskHandler(data).run_task()
|
||||||
|
|
||||||
|
return Response(response)
|
||||||
|
|
Loading…
Reference in New Issue