def get_session_from_dynamo(sessions_table_lock_client=False): logger.info("Getting available Session from DynamoDB") if not sessions_table_lock_client: sessions_table_lock_client = LockerClient('Sessions') timeout = 20000 # Setting the timeout to 20 seconds on a row lock random_session_number = str(random.randint( 1, 100)) # A number between 1 and 100 try: for i in range(0, 20): lock_response = sessions_table_lock_client.acquire( random_session_number, timeout) if lock_response: # no lock on connection number, return it logger.info("Successfully retrieved session from DynamoDB") return random_session_number, sessions_table_lock_client.guid else: # connection number is locked, retry in 5 seconds time.sleep(5) continue # if reached here, 20 retries with 5 seconds between retry - ended logger.info("Connection limit has been reached") return False, "" except Exception as e: print(f"Failed to retrieve session from DynamoDB: {str(e)}") raise Exception(f"Exception on get_session_from_dynamo:{str(e)}")
def create_session_table(): try: sessionsTableLock = LockerClient('PTASessions') sessionsTableLock.create_lock_table() except Exception as e: print("Failed to create 'PTASessions' table in DynamoDB. Exception: {0}".format(e)) return None print("Table 'PTASessions' created successfully") return True
def create_session_table(): logger.trace(caller_name='create_session_table') try: logger.info('Locking Dynamo Table') sessions_table_lock = LockerClient('Sessions') sessions_table_lock.create_lock_table() except Exception as e: logger.error( f"Failed to create 'Sessions' table in DynamoDB. Exception: {str(e)}" ) return None logger.info("Table 'Sessions' created successfully") return True
def get_available_session_from_dynamo(): sessionsTableLockClient = LockerClient('PTASessions') timeout = 20000 # Setting the timeout to 20 seconds on a row lock randomSessionNumber = str(random.randint(1, 100)) # A number between 1 and 100 try: for i in range(0, 20): lockResponse = sessionsTableLockClient.acquire(randomSessionNumber, timeout) if lockResponse: # no lock on connection number, return it return randomSessionNumber, sessionsTableLockClient.guid else: # connection number is locked, retry in 5 seconds time.sleep(5) continue # if reached here, 20 retries with 5 seconds between retry - ended print("No available connection after many retries") return False, "" except Exception as e: print("Exception on get_available_session_from_dynamo:{0}".format(e)) raise Exception("Exception on get_available_session_from_dynamo:{0}".format(e))
def release_session_on_dynamo(sessionId, sessionGuid): try: sessionsTableLockClient = LockerClient('Sessions') sessionsTableLockClient.locked = True sessionsTableLockClient.guid = sessionGuid sessionsTableLockClient.release(sessionId) except Exception: return False return True
def release_session_on_dynamo(session_id, session_guid, sessions_table_lock_client=False): logger.trace(session_id, session_guid, caller_name='release_session_on_dynamo') logger.info('Releasing session lock from DynamoDB') try: if not sessions_table_lock_client: sessions_table_lock_client = LockerClient('Sessions') sessions_table_lock_client.locked = True sessions_table_lock_client.guid = session_guid sessions_table_lock_client.release(session_id) except Exception as e: logger.error(f'Failed to release session lock from DynamoDB: {str(e)}') return False return True