async def refresh(log=True): # pragma: no cover: used in production only async with utils.wait_for_redis(): services = parse_torrc(settings.TORRC_FILE, log=log) services_dict = { service.name: dataclass_asdict(service) for service in services } anonymous_services_dict = { service.name: { "name": service.name, "hostname": service.hostname } for service in services } onion_host = services_dict.get("BitcartCC Merchants API", "") if onion_host: onion_host = onion_host["hostname"] or "" await settings.redis_pool.hmset_dict( REDIS_KEY, { "onion_host": onion_host, "services_dict": json.dumps(services_dict), "anonymous_services_dict": json.dumps(anonymous_services_dict), }, )
async def refresh_pending_deployments(): with log_errors(): now = utils.now().timestamp() async with utils.wait_for_redis(): to_delete = [] async for key, value in settings.redis_pool.ihscan(REDIS_KEY): with log_errors(): key = key.decode("utf-8") value = value.decode("utf-8") value = json.loads(value) if value else value # Remove stale deployments if "created" not in value or now - value[ "created"] >= KEY_TTL: to_delete.append(key) try: ssh_settings = SSHSettings(**value["ssh_settings"]) except ValidationError: continue # Mark all current instance deployments as complete as we can't do it from the worker task if ssh_settings == settings.SSH_SETTINGS: value["finished"] = True value["success"] = True value[ "output"] = "No output available. Current instance has been restarted" await set_task(key, value) if to_delete: await settings.redis_pool.hdel(REDIS_KEY, *to_delete)
async def add_onion_host(request: Request, call_next): response = await call_next(request) async with utils.wait_for_redis(): host = request.headers.get("host", "").split(":")[0] onion_host = await tor_ext.get_data("onion_host", "") if onion_host and not tor_ext.is_onion(host): response.headers["Onion-Location"] = onion_host + request.url.path return response
async def refresh(): from api import schemes, utils async with utils.wait_for_redis(): if settings.UPDATE_URL and (await utils.get_setting(schemes.Policy )).check_updates: logger.info("Checking for updates...") latest_tag = await get_update_data() if latest_tag and VERSION != latest_tag: logger.info(f"New update available: {latest_tag}") await settings.redis_pool.hset(REDIS_KEY, "new_update_tag", latest_tag) else: logger.info("No updates found") await settings.redis_pool.hdel(REDIS_KEY, "new_update_tag" ) # clean after previous checks
async def create_new_task(script, ssh_settings, is_manual): async with utils.wait_for_redis(): deploy_id = utils.unique_id() data = { "id": deploy_id, "script": script, "ssh_settings": ssh_settings.dict(), "success": is_manual, "finished": is_manual, "created": utils.now().timestamp(), "output": script if is_manual else "", } await set_task(deploy_id, data) if not is_manual: await events.event_handler.publish("deploy_task", {"id": deploy_id}) return data
async def get_task(task_id): async with utils.wait_for_redis(): data = await settings.redis_pool.hget(REDIS_KEY, task_id, encoding="utf-8") return json.loads(data) if data else data
async def set_task(task_id, data): async with utils.wait_for_redis(): await settings.redis_pool.hmset_dict(REDIS_KEY, {task_id: json.dumps(data)})