-
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.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Message } from './message'; | ||
import { RedisClient } from 'redis'; | ||
|
||
class MessageQueue { | ||
constructor(redisUrl) { | ||
this.redisClient = new RedisClient(redisUrl); | ||
this.queueName = 'cosmia:messages'; | ||
} | ||
|
||
async enqueue(message) { | ||
// Add a message to the queue | ||
await this.redisClient.lpush(this.queueName, message.toJSON()); | ||
} | ||
|
||
async dequeue() { | ||
// Retrieve and remove the next message from the queue | ||
const messageJSON = await this.redisClient.rpop(this.queueName); | ||
if (messageJSON) { | ||
return new Message(messageJSON); | ||
} | ||
return null; | ||
} | ||
|
||
async size() { | ||
// Return the number of messages in the queue | ||
return await this.redisClient.llen(this.queueName); | ||
} | ||
|
||
async peek() { | ||
// Return the next message in the queue without removing it | ||
const messageJSON = await this.redisClient.lindex(this.queueName, 0); | ||
if (messageJSON) { | ||
return new Message(messageJSON); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export default MessageQueue; |