mirror of
https://github.com/CCOSTAN/Home-AssistantConfig.git
synced 2025-08-15 18:15:00 +00:00
Finally got around to installing This amazing component by @ludeeus
This commit is contained in:
71
config/custom_components/hacs/hacsbase/task_factory.py
Executable file
71
config/custom_components/hacs/hacsbase/task_factory.py
Executable file
@@ -0,0 +1,71 @@
|
||||
# pylint: disable=missing-docstring,invalid-name
|
||||
import logging
|
||||
import time
|
||||
from datetime import timedelta
|
||||
import asyncio
|
||||
from aiogithubapi import AIOGitHubException
|
||||
|
||||
max_concurrent_tasks = asyncio.Semaphore(15)
|
||||
sleeper = 5
|
||||
|
||||
logger = logging.getLogger("hacs.factory")
|
||||
|
||||
|
||||
class HacsTaskFactory:
|
||||
def __init__(self):
|
||||
self.tasks = []
|
||||
self.running = False
|
||||
|
||||
async def execute(self):
|
||||
if not self.tasks:
|
||||
logger.debug("No tasks to execute")
|
||||
return
|
||||
if self.running:
|
||||
logger.debug("Allready executing tasks")
|
||||
return
|
||||
try:
|
||||
self.running = True
|
||||
logger.info("Processing %s tasks", len(self.tasks))
|
||||
start = time.time()
|
||||
await asyncio.gather(*self.tasks)
|
||||
logger.info(
|
||||
"Task processing of %s tasks completed in %s seconds",
|
||||
len(self.tasks),
|
||||
timedelta(seconds=round(time.time() - start)).seconds,
|
||||
)
|
||||
self.tasks = []
|
||||
self.running = False
|
||||
except RuntimeError:
|
||||
logger.warning("RuntimeError, Clearing current tasks")
|
||||
self.tasks = []
|
||||
self.running = False
|
||||
|
||||
async def safe_common_update(self, repository):
|
||||
async with max_concurrent_tasks:
|
||||
try:
|
||||
await repository.common_update()
|
||||
except AIOGitHubException as exception:
|
||||
logger.error(exception)
|
||||
|
||||
# Due to GitHub ratelimits we need to sleep a bit
|
||||
await asyncio.sleep(sleeper)
|
||||
|
||||
async def safe_update(self, repository):
|
||||
async with max_concurrent_tasks:
|
||||
try:
|
||||
await repository.update_repository()
|
||||
except AIOGitHubException as exception:
|
||||
logger.error(exception)
|
||||
|
||||
# Due to GitHub ratelimits we need to sleep a bit
|
||||
await asyncio.sleep(sleeper)
|
||||
|
||||
async def safe_register(self, hacs, repo, category):
|
||||
async with max_concurrent_tasks:
|
||||
try:
|
||||
await hacs.register_repository(repo, category)
|
||||
except AIOGitHubException as exception:
|
||||
logger.error(exception)
|
||||
|
||||
# Due to GitHub ratelimits we need to sleep a bit
|
||||
await asyncio.sleep(sleeper)
|
Reference in New Issue
Block a user