Skip to content

PGQ (PostgreSQL Queue) Class

Kajago edited this page Jun 17, 2023 · 1 revision

The PGQ class represents a queue in a PostgreSQL database. It allows for the publishing and subscribing of messages to and from the queue. This class manages the connection to the PostgreSQL database, the creation of the database and queue (if they do not already exist), and the insertion and retrieval of messages from the queue.

Methods

  • __init__(self, host, user, password, port, dbname="pgq", qname="message_queue"): The constructor of the PGQ class. It sets up the necessary variables to establish a connection to a PostgreSQL database. It also sets up a logger for logging purposes.
  • connect(self): Connects to the PostgreSQL database and creates a cursor object for executing PostgreSQL commands. It also ensures that the database and queue are set up.
  • disconnect(self): Closes the cursor and the connection to the PostgreSQL database.
  • pub(self, payload): Inserts a new message into the queue with the specified payload.
  • sub(self, callback: Callable[[str], None]): Subscribes to the queue and continuously processes new messages as they arrive. When a new message is processed, the specified callback function is called with the message's payload.
  • rollback_timeout_messages(self, timeout: int): Updates the status of all messages that have been in the 'PROCESSING' state for longer than the specified timeout to 'PENDING'.
Clone this wiki locally