make update_subscribed stoppable

This commit is contained in:
simon 2023-03-22 17:01:34 +07:00
parent 5ffc2046d4
commit db0e362b7d
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 28 additions and 8 deletions

View File

@ -126,10 +126,14 @@ class ChannelSubscription:
if not self.task: if not self.task:
continue continue
self.task.send_progress( if self.task:
message_lines=[f"Scanning Channel {idx + 1}/{total}"], self.task.send_progress(
progress=(idx + 1) / total, message_lines=[f"Scanning Channel {idx + 1}/{total}"],
) progress=(idx + 1) / total,
)
if self.task.is_stopped():
self.task.send_progress(["Received Stop signal."])
break
return missing_videos return missing_videos
@ -261,10 +265,14 @@ class PlaylistSubscription:
if not self.task: if not self.task:
continue continue
self.task.send_progress( if self.task:
message_lines=[f"Scanning Playlists {idx + 1}/{total}"], self.task.send_progress(
progress=(idx + 1) / total, message_lines=[f"Scanning Playlists {idx + 1}/{total}"],
) progress=(idx + 1) / total,
)
if self.task.is_stopped():
self.task.send_progress(["Received Stop signal."])
break
return missing_videos return missing_videos

View File

@ -40,6 +40,13 @@ class TaskManager:
return bool([i for i in tasks if i.get("status") == "PENDING"]) return bool([i for i in tasks if i.get("status") == "PENDING"])
def is_stopped(self, task_id):
"""check if task_id has received STOP command"""
task = self.get_task(task_id)
print(task)
return task.get("command") == "STOP"
def get_pending(self, task_name): def get_pending(self, task_name):
"""get all pending tasks of task_name""" """get all pending tasks of task_name"""
tasks = self.get_tasks_by_name(task_name) tasks = self.get_tasks_by_name(task_name)

View File

@ -53,6 +53,7 @@ class BaseTask(Task):
"title": "Rescan your Subscriptions", "title": "Rescan your Subscriptions",
"group": "download:scan", "group": "download:scan",
"api-start": True, "api-start": True,
"api-stop": True,
}, },
"download_pending": { "download_pending": {
"title": "Downloading", "title": "Downloading",
@ -146,6 +147,10 @@ class BaseTask(Task):
key = f"message:{message.get('group')}:{task_id.split('-')[0]}" key = f"message:{message.get('group')}:{task_id.split('-')[0]}"
return message, key return message, key
def is_stopped(self):
"""check if task is stopped"""
return TaskManager().is_stopped(self.request.id)
@shared_task(name="update_subscribed", bind=True, base=BaseTask) @shared_task(name="update_subscribed", bind=True, base=BaseTask)
def update_subscribed(self): def update_subscribed(self):