diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index e2bed71..dada701 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -200,3 +200,16 @@ When valid returns message with user id: "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 diff --git a/tubearchivist/api/src/task_processor.py b/tubearchivist/api/src/task_processor.py new file mode 100644 index 0000000..f13b953 --- /dev/null +++ b/tubearchivist/api/src/task_processor.py @@ -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} diff --git a/tubearchivist/api/urls.py b/tubearchivist/api/urls.py index b19f5c7..e059f75 100644 --- a/tubearchivist/api/urls.py +++ b/tubearchivist/api/urls.py @@ -11,6 +11,7 @@ from api.views import ( PlaylistApiListView, PlaylistApiVideoView, PlaylistApiView, + TaskApiView, VideoApiListView, VideoApiView, VideoProgressView, @@ -81,4 +82,9 @@ urlpatterns = [ DownloadApiView.as_view(), name="api-download", ), + path( + "task/", + TaskApiView.as_view(), + name="api-task", + ), ] diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index a92cf60..88b8a0e 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -1,6 +1,7 @@ """all API views""" from api.src.search_processor import SearchProcess +from api.src.task_processor import TaskHandler from home.src.download.queue import PendingInteract from home.src.es.connect import ElasticWrap from home.src.index.generic import Pagination @@ -446,3 +447,18 @@ class LoginApiView(ObtainAuthToken): print(f"returning token for user with 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)