This commit is contained in:
ArabCoders 2024-03-29 21:05:26 +03:00
parent 050012a1b2
commit f0dec94a60
3 changed files with 36 additions and 20 deletions

View file

@ -41,7 +41,8 @@ class DownloadQueue:
LOG.info(f'Using {self.config.max_workers} workers for downloading.')
asyncio.create_task(
self.__download_pool() if self.config.max_workers > 1 else self.__download()
self.__download_pool() if self.config.max_workers > 1 else self.__download(),
name='download_pool' if self.config.max_workers > 1 else 'download_worker'
)
async def __add_entry(
@ -172,7 +173,9 @@ class DownloadQueue:
itemDownload = self.queue.put(dlInfo)
self.event.set()
asyncio.create_task(self.notifier.emit(NotifyEvent, itemDownload.info))
asyncio.create_task(
self.notifier.emit(NotifyEvent, itemDownload.info),
name=f'notifier-{NotifyEvent}-{itemDownload.info.id}')
return {
'status': 'ok'
@ -227,16 +230,15 @@ class DownloadQueue:
started = time.perf_counter()
LOG.debug(f'extract_info: checking {url=}')
with ThreadPoolExecutor(1) as executor:
entry = await asyncio.wait_for(
fut=asyncio.get_running_loop().run_in_executor(
executor,
ExtractInfo,
mergeConfig(self.config.ytdl_options, ytdlp_config),
url,
bool(self.config.ytdl_debug)
),
timeout=self.config.extract_info_timeout)
entry = await asyncio.wait_for(
fut=asyncio.get_running_loop().run_in_executor(
None,
ExtractInfo,
mergeConfig(self.config.ytdl_options, ytdlp_config),
url,
bool(self.config.ytdl_debug)
),
timeout=self.config.extract_info_timeout)
if not entry:
return {'status': 'error', 'msg': 'Unable to extract info check logs.'}
@ -277,17 +279,18 @@ class DownloadQueue:
itemMessage = f"{id=} {item.info.id=} {item.info.title=}"
if item.started() is True:
if item.running():
LOG.debug(f'Canceling {itemMessage}')
item.cancel()
LOG.info(f'Cancelled {itemMessage}')
item.close()
else:
item.close()
LOG.debug(f'Deleting from queue {itemMessage}')
self.queue.delete(id)
asyncio.create_task(self.notifier.canceled(id))
asyncio.create_task(self.notifier.canceled(id), name=f'notifier-c-{id}')
self.done.put(item)
asyncio.create_task(self.notifier.completed(item))
asyncio.create_task(self.notifier.completed(item), name=f'notifier-d-{id}')
LOG.info(f'Deleted from queue {itemMessage}')
status[id] = 'ok'
@ -309,7 +312,7 @@ class DownloadQueue:
itemMessage = f"{id=} {item.info.id=} {item.info.title=}"
LOG.debug(f'Deleting completed download {itemMessage}')
self.done.delete(id)
asyncio.create_task(self.notifier.cleared(id))
asyncio.create_task(self.notifier.cleared(id), name=f'notifier-c-{id}')
LOG.info(f'Deleted completed download {itemMessage}')
status[id] = 'ok'
@ -408,12 +411,12 @@ class DownloadQueue:
self.queue.delete(key=id)
if entry.is_canceled() is True:
asyncio.create_task(self.notifier.canceled(id))
asyncio.create_task(self.notifier.canceled(id), name=f'notifier-c-{id}')
entry.info.status = 'canceled'
entry.info.error = 'Canceled by user.'
self.done.put(value=entry)
asyncio.create_task(self.notifier.completed(entry.info))
asyncio.create_task(self.notifier.completed(entry.info), name=f'notifier-d-{id}')
self.event.set()

View file

@ -106,6 +106,7 @@ class Main:
self.start()
def start(self):
start: str = f'YTPTube v{self.config.version} - listening on http://{self.config.host}:{self.config.port}'
web.run_app(
self.app,
host=self.config.host,
@ -113,8 +114,7 @@ class Main:
reuse_port=True,
loop=self.loop,
access_log=None,
print=lambda _: print(
f'YTPTube v{self.config.version} - listening on http://{self.config.host}:{self.config.port}'),
print=lambda _: LOG.info(start)
)
async def connect(self, sid, _):

View file

@ -148,6 +148,19 @@ const deleteItem = (type, item) => {
headers: {
'Content-Type': 'application/json'
}
}).then(resp => resp.json()).then(json => {
for (const key in items) {
const itemId = items[key];
if (itemId in json && json[itemId] === 'ok') {
if (true === (itemId in completed)) {
delete completed[itemId];
}
if (true === (itemId in downloading)) {
toast.info('Download canceled: ' + downloading[itemId]?.title);
delete downloading[itemId];
}
}
}
}).catch((error) => {
console.log(error);
toast.error('Failed to delete/cancel item/s. ' + error);