-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Queue struct and expose functionality (#5)
- Loading branch information
Showing
6 changed files
with
187 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
export const PGMQ_SCHEMA = "pgmq" | ||
const QUEUE_PREFIX = "q" | ||
const ARCHIVE_PREFIX = "a" | ||
|
||
export function createQueueQuery(name: string) { | ||
return ` | ||
CREATE TABLE IF NOT EXISTS ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${name} | ||
( | ||
msg_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, | ||
read_ct INT DEFAULT 0 NOT NULL, | ||
enqueued_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL, | ||
vt TIMESTAMP WITH TIME ZONE NOT NULL, | ||
message JSONB | ||
); | ||
CREATE TABLE IF NOT EXISTS ${PGMQ_SCHEMA}.${ARCHIVE_PREFIX}_${name} | ||
( | ||
msg_id BIGINT PRIMARY KEY, | ||
read_ct INT DEFAULT 0 NOT NULL, | ||
enqueued_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL, | ||
archived_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL, | ||
vt TIMESTAMP WITH TIME ZONE NOT NULL, | ||
message JSONB | ||
);` | ||
} | ||
|
||
export function deleteQueueQuery(name: string) { | ||
return ` | ||
DROP TABLE IF EXISTS ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${name}; | ||
DROP TABLE IF EXISTS ${PGMQ_SCHEMA}.${ARCHIVE_PREFIX}_${name};` | ||
} | ||
|
||
export function sendQuery(queue: string, vt: number) { | ||
return `INSERT INTO ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${queue} (vt, message) | ||
VALUES ((now() + interval '${vt} seconds'), $1::jsonb) | ||
RETURNING msg_id;` | ||
} | ||
|
||
export function readQuery(queue: string, vt: number) { | ||
return `WITH cte AS | ||
(SELECT msg_id | ||
FROM ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${queue} | ||
ORDER BY msg_id | ||
LIMIT 1 FOR UPDATE SKIP LOCKED) | ||
UPDATE ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${queue} t | ||
SET vt = now() + interval '${vt} seconds', | ||
read_ct = read_ct + 1 | ||
FROM cte | ||
WHERE t.msg_id = cte.msg_id | ||
RETURNING *;` | ||
} | ||
|
||
export function archiveQuery(queue: string, id: number) { | ||
return `WITH archived AS ( | ||
DELETE FROM ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${queue} | ||
WHERE msg_id = ${id} | ||
RETURNING msg_id, vt, read_ct, enqueued_at, message) | ||
INSERT | ||
INTO ${PGMQ_SCHEMA}.${ARCHIVE_PREFIX}_${queue} (msg_id, vt, read_ct, enqueued_at, message) | ||
SELECT msg_id, vt, read_ct, enqueued_at, message | ||
FROM archived | ||
RETURNING msg_id;` | ||
} | ||
|
||
export function deleteQuery(queue: string, id: number) { | ||
return `DELETE | ||
FROM ${PGMQ_SCHEMA}.${QUEUE_PREFIX}_${queue} | ||
WHERE msg_id = ${id} | ||
RETURNING msg_id;` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Pool } from "pg" | ||
import { archiveQuery, deleteQuery, readQuery } from "./queries" | ||
import { parseDbMessage } from "./message" | ||
|
||
export class Queue { | ||
private pool: Pool | ||
private readonly name: string | ||
|
||
constructor(connectionPool: Pool, name: string) { | ||
this.pool = connectionPool | ||
this.name = name | ||
} | ||
|
||
public async readMessage<T>(vt = 0) { | ||
const query = readQuery(this.name, vt) | ||
const conn = await this.pool.connect() | ||
const result = await conn.query(query) | ||
return parseDbMessage<T>(result.rows[0]) | ||
} | ||
|
||
/** | ||
* Delete a message from the queue | ||
* @param id - the id of the message to delete | ||
* @return the id of the message that was created | ||
*/ | ||
public async deleteMessage(id: number) { | ||
const query = deleteQuery(this.name, id) | ||
const connection = await this.pool.connect() | ||
const msg = await connection.query(query) | ||
connection.release() | ||
return parseInt(msg.rows[0].msg_id) | ||
} | ||
|
||
/** | ||
* Archives a message from the queue to its matching archive | ||
* @param id - the id of the message to delete | ||
* @return the id of the message that was created | ||
*/ | ||
public async archiveMessage(id: number): Promise<number> { | ||
const query = archiveQuery(this.name, id) | ||
const connection = await this.pool.connect() | ||
const msg = await connection.query(query) | ||
connection.release() | ||
return parseInt(msg.rows[0].msg_id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters