tubearchivist/tubearchivist/home/tasks.py

319 lines
9.4 KiB
Python
Raw Normal View History

2021-09-05 17:10:14 +00:00
"""
Functionality:
- initiate celery app
- collect tasks
- user config changes won't get applied here
because tasks are initiated at application start
2021-09-05 17:10:14 +00:00
"""
import json
2021-09-05 17:10:14 +00:00
import os
from celery import Celery, shared_task
from home.apps import StartupCheck
2022-01-22 15:13:37 +00:00
from home.src.download.queue import PendingList
from home.src.download.subscriptions import (
ChannelSubscription,
PlaylistSubscription,
)
2022-08-10 14:03:54 +00:00
from home.src.download.thumbnails import ThumbFilesystem, ThumbValidator
2022-01-22 15:13:37 +00:00
from home.src.download.yt_dlp_handler import VideoDownloader
from home.src.es.backup import ElasticBackup
from home.src.es.index_setup import ElasitIndexWrap
2022-01-22 15:13:37 +00:00
from home.src.index.channel import YoutubeChannel
2022-12-11 05:03:21 +00:00
from home.src.index.filesystem import ImportFolderScanner, scan_filesystem
from home.src.index.reindex import Reindex, ReindexManual, ReindexOutdated
from home.src.index.video_constants import VideoTypeEnum
2022-12-21 07:24:24 +00:00
from home.src.ta.config import AppConfig, ReleaseVersion, ScheduleBuilder
from home.src.ta.helper import clear_dl_cache
2022-01-22 15:13:37 +00:00
from home.src.ta.ta_redis import RedisArchivist, RedisQueue
from home.src.ta.urlparser import Parser
2021-09-05 17:10:14 +00:00
CONFIG = AppConfig().config
2021-09-30 11:03:23 +00:00
REDIS_HOST = os.environ.get("REDIS_HOST")
REDIS_PORT = os.environ.get("REDIS_PORT") or 6379
2021-09-05 17:10:14 +00:00
2021-12-02 08:54:29 +00:00
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
2021-09-30 11:03:23 +00:00
app = Celery("tasks", broker=f"redis://{REDIS_HOST}:{REDIS_PORT}")
2021-10-27 11:07:35 +00:00
app.config_from_object("django.conf:settings", namespace="ta:")
2021-09-05 17:10:14 +00:00
app.autodiscover_tasks()
app.conf.timezone = os.environ.get("TZ") or "UTC"
2021-09-05 17:10:14 +00:00
2021-12-02 08:54:29 +00:00
@shared_task(name="update_subscribed")
2021-09-05 17:10:14 +00:00
def update_subscribed():
2021-09-21 09:25:22 +00:00
"""look for missing videos and add to pending"""
message = {
"status": "message:rescan",
"level": "info",
"title": "Rescanning channels and playlists.",
"message": "Looking for new videos.",
}
RedisArchivist().set_message("message:rescan", message, expire=True)
2021-12-02 12:11:45 +00:00
have_lock = False
my_lock = RedisArchivist().get_lock("rescan")
try:
have_lock = my_lock.acquire(blocking=False)
if have_lock:
channel_handler = ChannelSubscription()
missing_from_channels = channel_handler.find_missing()
playlist_handler = PlaylistSubscription()
missing_from_playlists = playlist_handler.find_missing()
if missing_from_channels or missing_from_playlists:
channel_videos = [
{"type": "video", "vid_type": vid_type, "url": vid_id}
for vid_id, vid_type in missing_from_channels
]
playlist_videos = [
{
"type": "video",
"vid_type": VideoTypeEnum.VIDEOS,
"url": i,
}
for i in missing_from_playlists
]
pending_handler = PendingList(
youtube_ids=channel_videos + playlist_videos
)
2022-03-18 11:27:25 +00:00
pending_handler.parse_url_list()
pending_handler.add_to_pending()
2021-12-02 12:11:45 +00:00
else:
print("Did not acquire rescan lock.")
finally:
if have_lock:
my_lock.release()
2021-09-05 17:10:14 +00:00
2021-12-02 08:54:29 +00:00
@shared_task(name="download_pending")
2021-09-05 17:10:14 +00:00
def download_pending():
2021-09-21 09:25:22 +00:00
"""download latest pending videos"""
have_lock = False
my_lock = RedisArchivist().get_lock("downloading")
try:
have_lock = my_lock.acquire(blocking=False)
if have_lock:
downloader = VideoDownloader()
downloader.add_pending()
downloader.run_queue()
else:
print("Did not acquire download lock.")
finally:
if have_lock:
my_lock.release()
2021-09-05 17:10:14 +00:00
@shared_task
def download_single(pending_video):
2021-09-21 09:25:22 +00:00
"""start download single video now"""
queue = RedisQueue(queue_name="dl_queue")
to_add = {
"youtube_id": pending_video["youtube_id"],
"vid_type": pending_video["vid_type"],
}
queue.add_priority(json.dumps(to_add))
print(f"Added to queue with priority: {to_add}")
# start queue if needed
have_lock = False
my_lock = RedisArchivist().get_lock("downloading")
try:
have_lock = my_lock.acquire(blocking=False)
if have_lock:
key = "message:download"
mess_dict = {
"status": key,
"level": "info",
"title": "Download single video",
"message": "processing",
}
RedisArchivist().set_message(key, mess_dict, expire=True)
VideoDownloader().run_queue()
else:
print("Download queue already running.")
finally:
# release if only single run
if have_lock and not queue.get_next():
my_lock.release()
2021-09-05 17:10:14 +00:00
@shared_task
def extrac_dl(youtube_ids):
2021-09-21 09:25:22 +00:00
"""parse list passed and add to pending"""
2022-03-18 11:27:25 +00:00
pending_handler = PendingList(youtube_ids=youtube_ids)
pending_handler.parse_url_list()
pending_handler.add_to_pending()
2021-12-02 08:54:29 +00:00
@shared_task(name="check_reindex")
def check_reindex(data=False, extract_videos=False):
2021-09-21 09:25:22 +00:00
"""run the reindex main command"""
if data:
ReindexManual(extract_videos=extract_videos).extract_data(data)
have_lock = False
reindex_lock = RedisArchivist().get_lock("reindex")
try:
have_lock = reindex_lock.acquire(blocking=False)
if have_lock:
if not data:
ReindexOutdated().add_outdated()
Reindex().reindex_all()
else:
print("Did not acquire reindex lock.")
finally:
if have_lock:
reindex_lock.release()
2021-09-13 15:17:36 +00:00
@shared_task
def run_manual_import():
2021-09-21 09:25:22 +00:00
"""called from settings page, to go through import folder"""
print("starting media file import")
have_lock = False
my_lock = RedisArchivist().get_lock("manual_import")
try:
have_lock = my_lock.acquire(blocking=False)
if have_lock:
ImportFolderScanner().scan()
else:
print("Did not acquire lock form import.")
finally:
if have_lock:
my_lock.release()
2021-09-18 13:02:54 +00:00
2021-12-02 08:54:29 +00:00
@shared_task(name="run_backup")
2021-12-13 14:20:48 +00:00
def run_backup(reason="auto"):
2021-09-21 09:25:22 +00:00
"""called from settings page, dump backup to zip file"""
2022-08-12 05:03:09 +00:00
have_lock = False
my_lock = RedisArchivist().get_lock("run_backup")
try:
have_lock = my_lock.acquire(blocking=False)
if have_lock:
ElasticBackup(reason=reason).backup_all_indexes()
2022-08-12 05:03:09 +00:00
else:
print("Did not acquire lock for backup task.")
finally:
if have_lock:
my_lock.release()
print("backup finished")
@shared_task
2021-12-14 12:05:58 +00:00
def run_restore_backup(filename):
2021-09-21 09:25:22 +00:00
"""called from settings page, dump backup to zip file"""
ElasitIndexWrap().reset()
ElasticBackup().restore(filename)
2021-09-21 09:25:22 +00:00
print("index restore finished")
2021-09-24 16:37:26 +00:00
def kill_dl(task_id):
"""kill download worker task by ID"""
2021-10-11 08:26:31 +00:00
if task_id:
app.control.revoke(task_id, terminate=True)
_ = RedisArchivist().del_message("dl_queue_id")
RedisQueue(queue_name="dl_queue").clear()
2021-09-24 16:37:26 +00:00
clear_dl_cache(CONFIG)
2021-09-24 16:37:26 +00:00
# notify
mess_dict = {
"status": "message:download",
2021-09-24 16:37:26 +00:00
"level": "error",
"title": "Canceling download process",
"message": "Canceling download queue now.",
2021-09-24 16:37:26 +00:00
}
RedisArchivist().set_message("message:download", mess_dict, expire=True)
@shared_task
def rescan_filesystem():
2021-10-08 08:10:44 +00:00
"""check the media folder for mismatches"""
scan_filesystem()
2022-08-10 14:03:54 +00:00
ThumbValidator().download_missing()
2021-12-02 08:54:29 +00:00
@shared_task(name="thumbnail_check")
def thumbnail_check():
"""validate thumbnails"""
2022-08-10 14:03:54 +00:00
ThumbValidator().download_missing()
2021-12-02 08:54:29 +00:00
@shared_task
def re_sync_thumbs():
"""sync thumbnails to mediafiles"""
2022-08-10 14:03:54 +00:00
ThumbFilesystem().sync()
@shared_task
def subscribe_to(url_str):
"""take a list of urls to subscribe to"""
to_subscribe_list = Parser(url_str).parse()
for idx, item in enumerate(to_subscribe_list):
to_sub_id = item["url"]
if item["type"] == "playlist":
2022-08-10 14:03:54 +00:00
PlaylistSubscription().process_url_str([item])
continue
if item["type"] == "video":
vid_details = PendingList().get_youtube_details(to_sub_id)
channel_id_sub = vid_details["channel_id"]
elif item["type"] == "channel":
channel_id_sub = to_sub_id
else:
raise ValueError("failed to subscribe to: " + to_sub_id)
ChannelSubscription().change_subscribe(
channel_id_sub, channel_subscribed=True
)
# notify for channels
key = "message:subchannel"
message = {
"status": key,
"level": "info",
"title": "Subscribing to Channels",
"message": f"Processing {idx + 1} of {len(to_subscribe_list)}",
}
RedisArchivist().set_message(key, message=message, expire=True)
@shared_task
def index_channel_playlists(channel_id):
"""add all playlists of channel to index"""
2022-01-22 10:48:54 +00:00
channel = YoutubeChannel(channel_id)
# notify
key = "message:playlistscan"
mess_dict = {
"status": key,
"level": "info",
"title": "Looking for playlists",
2023-01-16 02:22:47 +00:00
"message": f"{channel_id}: Channel scan in progress",
}
RedisArchivist().set_message(key, mess_dict, expire=True)
channel.index_channel_playlists()
2021-12-02 08:54:29 +00:00
2022-12-21 07:24:24 +00:00
@shared_task(name="version_check")
def version_check():
"""check for new updates"""
ReleaseVersion().check()
# load new defaults then start the schedule here
StartupCheck().sync_redis_state()
app.conf.beat_schedule = ScheduleBuilder().build_schedule()