def get_queue_name(self, queue_id): sql = "SELECT * FROM queue WHERE id=%s" cursor.execute(sql, (queue_id)) queue = cursor.fetchone() queue_name = queue.get('name') return queue_name
def _delete_queue(self, queue_id): queue = self.sqs.get_queue_by_name(QueueName=self.get_queue_name()) queue.delete() sql = "DELETE FROM queue WHERE id=%s" cursor.execute(sql, (queue_id)) connection.commit() connection.close() return {"message": "queue is successfully deleted"}
def acknowledge_message(self, user_id, queue_id, message_id): try: sql = "INSERT INTO acknowledgement (user_id, queue_id, message_id) VALUES (%s, %s, %s)" cursor.execute(sql, (user_id, queue_id, message_id)) connection.commit() return {"message": "Successfully acknowledged"} except: return {"message": "Failed"}
def post(self, *args, **kwargs): data = tornado.escape.json_decode(self.request.body) user_id = data.get('user_id') string = data.get('message') sql = "Insert INTO status (user_id, value) VALUES (%s, %s)" cursor.execute(sql, (user_id, string)) connection.commit() self.write("status updated")
def send_message(self, user_id, payload, queue_id): queue = self.sqs.get_queue_by_name( QueueName=self.get_queue_name(queue_id)) response = queue.send_message(MessageBody=payload) sql = "INSERT INTO message (user_id, queue_id) VALUES (%s, %s)" cursor.execute(sql, (user_id, queue_id)) connection.commit() connection.close() return response
def _is_queue_active(self, queue_id): sql = "SELECT acknowledgement.message_id, queue.id_of_last_msg_acknowledged FROM acknowledgement INNER JOIN queue ON acknowledgement.queue_id=queue.id WHERE queue.id=%s ORDER BY acknowledgement.message_id" cursor.execute(sql, (queue_id)) messages = cursor.fetchall() if messages[0].get('message_id') == messages[0].get( 'id_of_last_msg_acknowledged' ) or abs(messages[0].get('message_id') - messages[0].get('id_of_last_msg_acknowledged')) <= 1: return True else: return False
def create_queue(self, user_id): queue_name = str("Dev-" + user_id + "-" + create_timestamp()) queue = self.sqs.create_queue(QueueName=queue_name) queue_url = queue.url queue_info = { "queue_name": queue_name, "queue_url": str(queue_url), "time_created": str(create_timestamp()), "id_of_last_read_message": 2, "no_of_messages_sent": 0, 'id_of_last_message': 0, 'id_of_last_message_acknowledge': 0 } sql = "INSERT INTO queue (user_id, url, no_of_msg_sent, id_of_last_msg, name) VALUES (%s, %s, %s, %s, %s)" cursor.execute( sql, (user_id, queue_info['queue_url'], 0, 0, queue_info['queue_name'])) connection.commit() return queue_info