-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Viktor Hartenberger
committed
Apr 14, 2024
1 parent
e329305
commit 8460079
Showing
2 changed files
with
59 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace KnightBus.PostgreSql; | ||
using static PostgresConstants; | ||
|
||
public static class Query | ||
{ | ||
public static string GetMessages(PostgresQueueName queueName) => @$" | ||
WITH cte AS | ||
( | ||
SELECT message_id | ||
FROM {SchemaName}.{QueuePrefix}_{queueName} | ||
WHERE visibility_timeout <= clock_timestamp() | ||
ORDER BY message_id ASC | ||
LIMIT ($1) | ||
FOR UPDATE SKIP LOCKED | ||
) | ||
UPDATE {SchemaName}.{QueuePrefix}_{queueName} t | ||
SET | ||
visibility_timeout = clock_timestamp() + ($2), | ||
read_count = read_count + 1 | ||
FROM cte | ||
WHERE t.message_id = cte.message_id | ||
RETURNING *; | ||
"; | ||
|
||
public static string CompleteMessage(PostgresQueueName queueName) => @$" | ||
DELETE FROM {SchemaName}.{QueuePrefix}_{queueName} | ||
WHERE message_id = ($1); | ||
"; | ||
|
||
public static string AbandonByError(PostgresQueueName queueName) => @$" | ||
UPDATE {SchemaName}.{QueuePrefix}_{queueName} | ||
SET properties = ($1), visibility_timeout = now() | ||
WHERE message_id = ($2); | ||
"; | ||
|
||
public static string DeadLetterMessage(PostgresQueueName queueName) => @$" | ||
WITH DeadLetter AS ( | ||
DELETE FROM {SchemaName}.{QueuePrefix}_{queueName} | ||
WHERE message_id = ($1) | ||
RETURNING message_id, enqueued_at, message, properties | ||
) | ||
INSERT INTO {SchemaName}.{DlQueuePrefix}_{queueName} (message_id, enqueued_at, created_at, message, properties) | ||
SELECT message_id, enqueued_at, now(), message, properties | ||
FROM DeadLetter; | ||
"; | ||
|
||
public static string PeekDeadLetterMessage(PostgresQueueName queueName) => @$" | ||
SELECT message_id, enqueued_at, created_at, message, properties | ||
FROM {SchemaName}.{DlQueuePrefix}_{queueName} | ||
ORDER BY message_id ASC | ||
LIMIT ($1); | ||
"; | ||
} |