def sync_task(task_id): sync = SyncTask.objects.get(id=task_id) all_origin_dirs = [] for origin_model in list(source_cls_factory.values()): origins = origin_model.objects.all() for origin in origins: try: origin.read() all_origin_dirs.append(origin.name) except Exception as e: message = 'Origin package[type={origin_type}, id={origin_id}] read error: {error}'.format( origin_type=origin.type, origin_id=origin.id, error=e) logger.error(message) sync.finish_task(FAILED, message) return False caches = CachePackageSource.objects.all() for cache in caches: try: cache.write(all_origin_dirs) except Exception as e: SyncTask.objects.filter(id=task_id).update(status=FAILED) message = 'Cache package[type={origin_type}, id={origin_id}] write error: {error}'.format( origin_type=cache.type, origin_id=cache.id, error=e) logger.error(message) sync.finish_task(FAILED, message) return False sync.finish_task(SUCCEEDED) return True
def get_source_models(): origin_models = source_cls_factory.values() source_models = origin_models + [CachePackageSource] return source_models