async def startup(self): try: await self.connect_websocket() except Exception as e: console.print("[blue] Connection attempt as Provider failed") self.current_retries += 1 if self.auto_reconnect: sleeping_time = (self.current_retries + 1) console.print(f"[blue] Trying to Reconnect as Provider in {sleeping_time} seconds") await asyncio.sleep(sleeping_time) await self.startup() else: console.print("[blue]Provider: No reconnecting attempt envisioned. Shutting Down!") console.print_exception() console.print("[blue] Sucessfully Established A Connection to Provider Endpoint") self.consumer_task = create_task( self.consumer() ) self.producer_task = create_task( self.producer() ) self.worker_task = create_task( self.workers() ) done, self.pending = await asyncio.wait( [self.consumer_task, self.worker_task, self.producer_task], return_when=asyncio.FIRST_EXCEPTION ) try: for task in done: if task.exception(): raise task.exception() except ConnectionClosedError: console.print("[blue] Provider Connection was closed. Trying Reconnect") except: console.print_exception() logger.error(f"Provider: Lost connection inbetween everything :( {[ task.exception() for task in done]}") logger.error(f'Provider: Reconnecting') if self.connection: await self.connection.close() for task in self.pending: task.cancel() console.log("Trying to Reconnect") self.current_retries = 0 # reset retries after one successfull connection await self.startup() # Attempt to ronnect again
def log_table(self): table = Table.grid() table.add_column() table.add_column() table.add_row() arkitekt_table = Table(title="Arkitekt", show_header=False) for key, value in self.config.dict().items(): arkitekt_table.add_row(key, str(value)) herre_table = Table(title="Herre", show_header=False) for key, value in self.auth.config.dict().items(): if "secret" in key: continue herre_table.add_row(key, str(value)) extensions_table = Table.grid() row = [] for key, value in self._extensions.items(): if isinstance(value, dict): extensions_table.add_column() sub_table = Table(title=key, show_header=False) for key, value in value.items(): sub_table.add_row(key, str(value)) row.append(sub_table) extensions_table.add_row(*row) table.add_row("Welcome to Arnheim") table.add_row(herre_table, arkitekt_table) table.add_row() if self.client_type in [ClientType.PROVIDER, ClientType.HOST]: table.add_row("We are Connected as a [bold]Host[/bold]") if self.client_type in [ClientType.PROVIDER]: table.add_row("We are Connected as a [bold]Provider[/bold]") table.add_row() table.add_row("[green]Connected :pile_of_poo:") table.add_row(extensions_table) console.print(Panel(table, title="Arkitekt"))
async def startup(self): try: await self.connect_websocket() except Exception as e: console.print("[green] Connection attempt as Postman failed") self.current_retries += 1 self.current_retries += 1 if self.auto_reconnect: sleeping_time = (self.current_retries + 1) console.print( f"[green] Trying to Reconnect as Postman in {sleeping_time} seconds" ) await asyncio.sleep(sleeping_time) await self.startup() else: return console.print("[green] Successfully established Postman Connection") self.receiving_task = create_task(self.receiving()) self.sending_task = create_task(self.sending()) self.callback_task = create_task(self.callbacks()) done, self.pending = await asyncio.wait( [self.callback_task, self.receiving_task, self.sending_task], return_when=asyncio.FIRST_EXCEPTION) try: for task in done: if task.exception(): raise task.exception() except ConnectionClosedError: console.print( "[green] Postman Connection was closed. Trying Reconnect") except: console.print_exception() logger.debug( f"Postman: Lost connection inbetween everything :( {[ task.exception() for task in done]}" ) logger.error(f'Postman: Trying to reconnect Postman') if self.connection: await self.connection.close() for task in self.pending: task.cancel() self.current_retries = 0 # reset retries after one successfull connection await self.startup()
def fetchToken(self, loop=None) -> str: # Getting token self.legacy_app_client = LegacyApplicationClient(self.client_id, scope=self.scope) console.print( "[yellow]Try to avoid authenticating with User and Password. Use Implicit Flow if possible!" ) if not self.username: self.username = console.input( "[green]Enter your [b]Username: "******"[green]Now for the [b]Password: "******"[bold green]Authenticating..."): token = self.oauth.fetch_token(token_url=self.token_url, username=self.username, password=self.password, client_id=self.client_id, client_secret=self.client_secret) return token
def template(self, node: Node, policy: PodPolicy = MultiplePodPolicy(), auto_provide=False, on_provide=None, on_unprovide=None, bypass_shrink=False, bypass_expand=False, **implementation_details): console.print("[blue] Registered to Provide") console.print(node) def real_decorator(function_or_actor): assert callable(function_or_actor) or issubclass( function_or_actor, Actor), "Please only decorate functions or subclasses of Actor" # TODO: Check if function has same parameters as node template = Template.objects.update_or_create( **{ "node": node.id, "params": implementation_details, "policy": policy.dict() }) if isactor(function_or_actor): actorClass = function_or_actor else: is_coroutine = inspect.iscoroutinefunction(function_or_actor) is_asyncgen = inspect.isasyncgenfunction(function_or_actor) is_generatorfunction = inspect.isgeneratorfunction( function_or_actor) is_function = inspect.isfunction(function_or_actor) class_attributes = { "assign": staticmethod(function_or_actor), "expandInputs": not bypass_expand, "shrinkOutputs": not bypass_shrink } if is_coroutine: actorClass = type(f"GeneratedActor{template.id}", (FunctionalFuncActor, ), class_attributes) elif is_asyncgen: actorClass = type(f"GeneratedActor{template.id}", (FunctionalGenActor, ), class_attributes) elif is_generatorfunction: actorClass = type(f"GeneratedActor{template.id}", (FunctionalThreadedGenActor, ), class_attributes) elif is_function: actorClass = type(f"GeneratedActor{template.id}", (FunctionalThreadedFuncActor, ), class_attributes) else: raise Exception( f"Unknown type of function {function_or_actor}") self.register_actor(str(template.id), actorClass) return actorClass return real_decorator