async def _acquire_management_token(self) -> float: try: resp = await self._session.post( "https://%s/oauth/token" % self._domain, headers={ "content-type": "application/x-www-form-urlencoded", }, data={ "grant_type": "client_credentials", "client_id": self._client_id, "client_secret": self._client_secret, "audience": "https://%s/api/v2/" % self._domain, }) data = await resp.json() self._mgmt_token = data["access_token"] self._mgmt_event.set() expires_in = int(data["expires_in"]) except Exception as e: self.log.exception("Failed to renew the mgmt Auth0 token") raise GracefulExit() from e self.log.info( "Acquired new Auth0 management token %s...%s for the next %s", self._mgmt_token[:12], self._mgmt_token[-12:], timedelta(seconds=expires_in)) expires_in -= 5 * 60 # 5 minutes earlier if expires_in < 0: expires_in = 0 return expires_in
async def handle(request): async with client.ClientSession() as session: response = await session.post( url=f"https://id.twitch.tv/oauth2/token", params={ "client_id": config["credentials"]["twitch"]["client_id"], "client_secret": config["credentials"]["twitch"]["client_secret"], "code": request.query["code"], "grant_type": "authorization_code", "redirect_uri": "http://127.0.0.1:3333/", }, ) content = await response.json() with secrets_file.open("wt") as fobj: fobj.write(json.dumps(content, indent=4)) raise GracefulExit() return web.Response(text="received!")
def maybe_bail(app): if not app["front"] and not app["back"]: log.info("No remaining clients, gracefully bailing.") raise GracefulExit() else: log.info(f"Not closing since {len(app['front'])} front and " + f"{len(app['back'])} back connections remains.")
async def connect_to_sdb(): try: db = databases.Database(sdb_conn, **(sdb_options or {})) await db.connect() except Exception: self.log.exception("Failed to connect to the state DB at %s", sdb_conn) raise GracefulExit() from None self.log.info("Connected to the server state DB on %s", sdb_conn) self.sdb = db
async def default_user(self) -> User: """Return the user of unauthorized, public requests.""" if self._default_user is not None: return self._default_user self._default_user = await self.get_user(self._default_user_id) if self._default_user is None: message = "Failed to fetch the default user (%s) details. " \ "Try changing ATHENIAN_DEFAULT_USER" % self._default_user_id self.log.error(message) raise GracefulExit(message) return self._default_user
def onOneway(self, runId: str, command: ControlCommand) -> CommandResponse: """ Overrides the base class onOneway method to handle commands from a CSW component. Args: runId (str): unique id for this command command (ControlCommand): contains the command Returns: CommandResponse a subclass of CommandResponse (only Accepted, Invalid or Locked are allowed) """ n = len(command.paramSet) print(f"MyComponentHandlers Received oneway {str(command)} with {n} params.") raise GracefulExit()
async def shutdown(request): self.c.log("Daemon", "http_server", "GET /shutdown, shutting down daemon") raise GracefulExit()
def raise_graceful_exit(): # why is it so hard to stop? raise GracefulExit()
async def gracefulShutdown(cls: web.Application) -> None: # TODO: this does not work raise GracefulExit()
async def shutdown(request): logger = logging.getLogger("Daemon.http_server") logger.info("GET /shutdown, shutting down daemon") raise GracefulExit()