def get_event_by_id(cursor, event_id): cursor.execute( """ SELECT * FROM events WHERE id=%s """, [event_id], ) return Event.from_dict(cursor.fetchone())
def get_next_event(cursor, topics): LOGGER.debug("Getting next event for topics: %s", topics) query = sql.SQL( """ SELECT * FROM events WHERE status='PENDING' AND process_after < now() AND topic in ({}) ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1 """ ).format(sql.SQL(", ").join(sql.Literal(topic) for topic in topics)) cursor.execute(query) data = cursor.fetchone() event = Event.from_dict(data) if data else None LOGGER.debug("Next event: %s", event) return event
def create_event(cursor, event): LOGGER.debug("Creating event: %s", event) # jsonb columns cannot handle certain data types without casting # so make sure payload is always a JSON object by nesting it payload = dict(payload=event.payload) if event.process_after: cursor.execute( """ INSERT INTO events (topic, payload, process_after) VALUES (%s, %s, %s) RETURNING * """, [event.topic, payload, event.process_after], ) else: cursor.execute( """ INSERT INTO events (topic, payload) VALUES (%s, %s) RETURNING * """, [event.topic, payload], ) return Event.from_dict(cursor.fetchone())