2012-06-22 09:32:39 +02:00
|
|
|
"""
|
|
|
|
Celery tasks for djeuscan
|
|
|
|
"""
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
from itertools import islice
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
from celery.task import task, periodic_task
|
|
|
|
from celery.task.schedules import crontab
|
2012-05-30 22:54:55 +02:00
|
|
|
from celery.task.sets import TaskSet
|
|
|
|
|
2012-06-26 18:31:35 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
from djeuscan.models import Package, RefreshPackageQuery
|
2012-06-22 09:32:39 +02:00
|
|
|
from djeuscan.processing.regen_rrds import regen_rrds
|
|
|
|
from djeuscan.processing.update_counters import update_counters
|
|
|
|
from djeuscan.processing.scan_metadata import scan_metadata
|
|
|
|
from djeuscan.processing.scan_portage import scan_portage
|
|
|
|
from djeuscan.processing.scan_upstream import scan_upstream
|
|
|
|
from djeuscan.processing.update_portage_trees import update_portage_trees
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
2012-06-09 16:12:17 +02:00
|
|
|
class TaskFailedException(Exception):
|
|
|
|
"""
|
|
|
|
Exception for failed tasks
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
def _chunks(it, n):
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
|
|
|
Chunk generator, takes an iterator and the desired size of the chunk
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
for first in it:
|
|
|
|
yield [first] + list(islice(it, n - 1))
|
|
|
|
|
|
|
|
|
2012-06-26 18:31:35 +02:00
|
|
|
def _run_in_chunks(task, packages, kwargs=None,
|
|
|
|
concurrently=settings.TASKS_CONCURRENTLY,
|
|
|
|
n=settings.TASKS_SUBTASK_PACKAGES):
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
2012-06-22 09:32:39 +02:00
|
|
|
Launches a TaskSet at a time with <concurrently> subtasks.
|
|
|
|
Each subtask has <n> packages to handle
|
2012-06-12 13:05:58 +02:00
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
output = []
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
chunk_generator = _chunks(iter(packages), n)
|
|
|
|
done = False
|
|
|
|
|
|
|
|
while not done:
|
|
|
|
tasks = []
|
|
|
|
for _ in range(concurrently):
|
|
|
|
try:
|
|
|
|
chunk = chunk_generator.next()
|
|
|
|
except StopIteration:
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
tasks.append(task.subtask((chunk, ), kwargs))
|
|
|
|
job = TaskSet(tasks=tasks)
|
2012-06-08 14:21:11 +02:00
|
|
|
result = job.apply_async()
|
2012-06-09 16:12:17 +02:00
|
|
|
# TODO: understand why this causes timeout
|
2012-06-29 17:25:45 +02:00
|
|
|
output.extend(list(result.join()))
|
2012-06-08 14:21:11 +02:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
@task
|
|
|
|
def regen_rrds_task():
|
2012-06-22 09:32:39 +02:00
|
|
|
"""
|
|
|
|
Regenerate RRDs
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
return regen_rrds()
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-22 09:32:39 +02:00
|
|
|
def update_counters_task(fast=True):
|
|
|
|
"""
|
|
|
|
Updates counters
|
|
|
|
"""
|
|
|
|
return update_counters(fast=fast)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-22 09:32:39 +02:00
|
|
|
def _scan_metadata_task(packages):
|
|
|
|
"""
|
|
|
|
Scans metadata for the given set of packages
|
|
|
|
"""
|
|
|
|
logger = _scan_metadata_task.get_logger()
|
|
|
|
logger.info("Starting metadata scanning subtask for %d packages...",
|
|
|
|
len(packages))
|
2012-05-31 09:11:40 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
result = scan_metadata(
|
|
|
|
packages=packages,
|
|
|
|
logger=logger,
|
|
|
|
)
|
2012-06-12 13:05:58 +02:00
|
|
|
if not result:
|
2012-06-22 09:32:39 +02:00
|
|
|
raise TaskFailedException
|
2012-06-12 13:05:58 +02:00
|
|
|
return result
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
2012-06-05 13:30:31 +02:00
|
|
|
@task
|
|
|
|
def scan_metadata_list_task(query):
|
2012-06-22 09:32:39 +02:00
|
|
|
"""
|
|
|
|
Runs a parallel metadata scan for packages in the query list (space
|
|
|
|
separated string). Task used only from the web interface.
|
|
|
|
"""
|
2012-06-29 17:25:45 +02:00
|
|
|
return _run_in_chunks(_scan_metadata_task, [p for p in query.split()])
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
@task
|
|
|
|
def scan_metadata_all_task():
|
2012-06-22 09:32:39 +02:00
|
|
|
"""
|
|
|
|
Runs a parallel metadata scan for all packages
|
|
|
|
"""
|
2012-06-29 17:25:45 +02:00
|
|
|
return _run_in_chunks(_scan_metadata_task, Package.objects.all())
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-27 10:15:30 +02:00
|
|
|
def _scan_portage_task(packages, no_log=False, purge_packages=False,
|
2012-06-22 09:32:39 +02:00
|
|
|
purge_versions=False, prefetch=False):
|
|
|
|
"""
|
|
|
|
Scans portage for the given set of packages
|
|
|
|
"""
|
|
|
|
logger = _scan_portage_task.get_logger()
|
2012-06-29 17:25:45 +02:00
|
|
|
if packages:
|
|
|
|
logger.info("Starting portage scanning subtask for %d packages...",
|
|
|
|
len(packages))
|
|
|
|
else:
|
|
|
|
logger.info("Starting portage scanning for all packages...")
|
2012-06-22 09:32:39 +02:00
|
|
|
|
|
|
|
result = scan_portage(
|
|
|
|
packages=packages,
|
2012-06-27 10:15:30 +02:00
|
|
|
no_log=no_log,
|
2012-06-22 09:32:39 +02:00
|
|
|
purge_packages=purge_packages,
|
|
|
|
purge_versions=purge_versions,
|
|
|
|
prefetch=prefetch,
|
|
|
|
logger=logger,
|
|
|
|
)
|
|
|
|
if not result:
|
|
|
|
raise TaskFailedException
|
|
|
|
return result
|
2012-06-05 13:30:31 +02:00
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
@task
|
2012-06-27 10:15:30 +02:00
|
|
|
def scan_portage_list_task(query, no_log=False, purge_packages=False,
|
2012-06-22 09:32:39 +02:00
|
|
|
purge_versions=False, prefetch=False):
|
|
|
|
"""
|
|
|
|
Runs a parallel portage scan for packages in the query list (space
|
|
|
|
separated string). Task used only from the web interface.
|
|
|
|
"""
|
2012-06-27 10:15:30 +02:00
|
|
|
kwargs = {"no_log": no_log, "purge_packages": purge_packages,
|
2012-06-22 09:32:39 +02:00
|
|
|
"purge_versions": purge_versions, "prefetch": prefetch}
|
2012-06-29 17:25:45 +02:00
|
|
|
return _run_in_chunks(_scan_portage_task, [p for p in query.split()],
|
|
|
|
kwargs)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-27 10:15:30 +02:00
|
|
|
def scan_portage_all_task(no_log=False, purge_packages=False,
|
2012-06-22 09:32:39 +02:00
|
|
|
purge_versions=False, prefetch=False):
|
|
|
|
"""
|
2012-06-29 17:25:45 +02:00
|
|
|
Runs a syncronous portage scan for all packages
|
2012-06-22 09:32:39 +02:00
|
|
|
"""
|
2012-06-29 17:25:45 +02:00
|
|
|
return _scan_portage_task(
|
|
|
|
packages=None,
|
|
|
|
no_log=no_log,
|
|
|
|
purge_packages=purge_packages,
|
|
|
|
purge_versions=purge_versions,
|
|
|
|
prefetch=prefetch,
|
|
|
|
)
|
2012-05-31 09:11:40 +02:00
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
@task
|
|
|
|
def _scan_upstream_task(packages, purge_versions=False):
|
|
|
|
"""
|
|
|
|
Scans upstream for the given set of packages
|
|
|
|
"""
|
|
|
|
logger = _scan_upstream_task.get_logger()
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
logger.info("Starting upstream scanning subtask for %d packages...",
|
|
|
|
len(packages))
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
result = scan_upstream(
|
|
|
|
packages=packages,
|
|
|
|
purge_versions=purge_versions,
|
|
|
|
logger=logger,
|
|
|
|
)
|
|
|
|
if not result:
|
|
|
|
raise TaskFailedException
|
2012-06-09 16:12:17 +02:00
|
|
|
return result
|
2012-06-05 13:30:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-22 09:32:39 +02:00
|
|
|
def scan_upstream_list_task(query, purge_versions=False):
|
|
|
|
"""
|
|
|
|
Runs a parallel upstream scan for packages in the query list (space
|
|
|
|
separated string). Task used only from the web interface.
|
|
|
|
"""
|
2012-06-05 13:30:31 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
kwargs = {"purge_versions": purge_versions}
|
2012-06-29 17:25:45 +02:00
|
|
|
return _run_in_chunks(_scan_upstream_task, [p for p in query.split()],
|
|
|
|
kwargs)
|
2012-06-05 13:30:31 +02:00
|
|
|
|
2012-05-30 22:54:55 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
@task
|
|
|
|
def scan_upstream_all_task(purge_versions=False):
|
|
|
|
"""
|
|
|
|
Runs a parallel portage scan for all packages
|
|
|
|
"""
|
|
|
|
kwargs = {"purge_versions": purge_versions}
|
2012-06-30 10:50:17 +02:00
|
|
|
return _run_in_chunks(
|
|
|
|
_scan_upstream_task,
|
|
|
|
Package.objects.all().order_by('?'),
|
|
|
|
kwargs
|
|
|
|
)
|
2012-05-30 22:54:55 +02:00
|
|
|
|
|
|
|
|
2012-06-04 11:06:47 +02:00
|
|
|
@task
|
2012-06-22 09:32:39 +02:00
|
|
|
def update_portage_trees_task():
|
|
|
|
"""
|
|
|
|
Update portage tree
|
|
|
|
"""
|
|
|
|
logger = update_portage_trees_task.get_logger()
|
|
|
|
update_portage_trees(logger=logger)
|
2012-06-04 11:06:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2012-06-22 09:32:39 +02:00
|
|
|
def update_task(update_portage_trees=True, scan_portage=True,
|
|
|
|
scan_metadata=True, scan_upstream=True, update_counter=True):
|
|
|
|
"""
|
|
|
|
Update the whole euscan system
|
|
|
|
"""
|
|
|
|
if update_portage_trees:
|
|
|
|
update_portage_trees_task()
|
|
|
|
if scan_portage:
|
|
|
|
scan_portage_all_task(prefetch=True, purge_packages=True,
|
|
|
|
purge_versions=True)
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
# metadata and upstream scan can run concurrently, launch them
|
|
|
|
# asynchronously and wait for them to finish
|
|
|
|
metadata_job = None
|
|
|
|
if scan_metadata:
|
|
|
|
metadata_job = scan_metadata_all_task().delay()
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
upstream_job = None
|
|
|
|
if scan_upstream:
|
|
|
|
upstream_job = scan_upstream_all_task().delay()
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
if metadata_job:
|
|
|
|
metadata_job.wait()
|
|
|
|
if upstream_job:
|
|
|
|
upstream_job.wait()
|
2012-06-04 11:06:47 +02:00
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
update_counters(fast=False)
|
2012-06-04 11:06:47 +02:00
|
|
|
|
|
|
|
|
2012-06-22 11:40:40 +02:00
|
|
|
@task
|
|
|
|
def scan_package_task(package):
|
2012-06-30 10:42:26 +02:00
|
|
|
_scan_portage_task([package], purge_packages=True, purge_versions=True)
|
2012-06-22 11:40:40 +02:00
|
|
|
_scan_metadata_task([package])
|
|
|
|
_scan_upstream_task([package])
|
|
|
|
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
@periodic_task(run_every=crontab(minute="*/1"))
|
2012-06-22 09:32:39 +02:00
|
|
|
def consume_refresh_package_request():
|
|
|
|
"""
|
|
|
|
Satisfies user requests for package refreshing, runs every minute
|
|
|
|
"""
|
2012-06-08 14:21:11 +02:00
|
|
|
try:
|
2012-06-27 10:15:30 +02:00
|
|
|
obj = RefreshPackageQuery.objects.all().order_by('-priority')[0]
|
|
|
|
except IndexError:
|
2012-06-08 14:21:11 +02:00
|
|
|
return {}
|
|
|
|
else:
|
2012-06-22 11:40:40 +02:00
|
|
|
result = scan_package_task(obj.query)
|
2012-06-08 14:21:11 +02:00
|
|
|
obj.delete()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2012-06-22 09:32:39 +02:00
|
|
|
@periodic_task(run_every=crontab(hour=03, minute=00, day_of_week=1))
|
|
|
|
def update_periodic_task():
|
|
|
|
"""
|
|
|
|
Runs a whole update once a week
|
|
|
|
"""
|
|
|
|
update_task()
|
|
|
|
|
|
|
|
|
2012-06-08 14:21:11 +02:00
|
|
|
admin_tasks = [
|
2012-06-03 09:43:49 +02:00
|
|
|
regen_rrds_task,
|
|
|
|
update_counters_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_metadata_list_task,
|
2012-06-03 09:43:49 +02:00
|
|
|
scan_metadata_all_task,
|
|
|
|
scan_portage_all_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_portage_list_task,
|
2012-06-03 09:43:49 +02:00
|
|
|
scan_upstream_all_task,
|
2012-06-05 13:30:31 +02:00
|
|
|
scan_upstream_list_task,
|
2012-06-27 10:15:30 +02:00
|
|
|
update_portage_trees_task,
|
2012-06-22 11:40:40 +02:00
|
|
|
update_task,
|
|
|
|
scan_package_task,
|
2012-06-03 09:43:49 +02:00
|
|
|
]
|