Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Adding support for promises #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ queue.add('Hello, World!', (err, id) => {
})
```

Using Promises

```js
// Message with payload 'Hello, World!' added.
// 'id' is returned, useful for logging.
var id = await queue.add('Hello, World!');
```

Get a message from the queue:

```js
Expand All @@ -53,6 +61,16 @@ queue.get((err, msg) => {
})
```

Using Promises

```js
var msg = await queue.get();
console.log('msg.id=' + msg.id)
console.log('msg.ack=' + msg.ack)
console.log('msg.payload=' + msg.payload) // 'Hello, World!'
console.log('msg.tries=' + msg.tries)
```

Ping a message to keep it's visibility open for long-running tasks

```js
Expand All @@ -62,6 +80,14 @@ queue.ping(msg.ack, (err, id) => {
})
```

Using Promises

```js
// Visibility window now increased for this message id.
// 'id' is returned, useful for logging.
var id = await queue.ping(msg.ack);
```

Ack a message (and remove it from the queue):

```js
Expand All @@ -71,6 +97,14 @@ queue.ack(msg.ack, (err, id) => {
})
```

Using Promises

```js
// This msg removed from queue for this ack.
// The 'id' of the message is returned, useful for logging.
var id = await queue.ack(msg.ack);
```

By default, all old messages - even processed ones - are left in MongoDB. This is so that
you can go and analyse them if you want. However, you can call the following function
to remove processed messages:
Expand All @@ -81,6 +115,14 @@ queue.clean((err) => {
})
```

Using Promises

```js
// All processed (ie. acked) messages have been deleted
await queue.clean();
})
```

And if you haven't already, you should call this to make sure indexes have
been added in MongoDB. Of course, if you've called this once (in some kind
one-off script) you don't need to call it in your program. Of course, check
Expand All @@ -92,6 +134,13 @@ queue.createIndexes((err, indexName) => {
})
```

Using Promises

```js
// The indexes needed have been added to MongoDB.
var indexName = await queue.createIndexes();
```

## Creating a Queue ##

To create a queue, call the exported function with the `MongoClient`, the name
Expand Down
Loading