Skip to content

Commit

Permalink
Merge pull request #6 from wonderlic/develop
Browse files Browse the repository at this point in the history
Native async/await and some other ES6 stuff
  • Loading branch information
wonderlic-stephene authored Jan 25, 2024
2 parents f8e3b61 + 3dad6c0 commit 54635ce
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/*
.idea/*
.env
.npmrc
package-lock.json
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/*
node_modules/*
test/*
.npmrc
package-lock.json
78 changes: 44 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

## Package Dependency Notice

NOTE: Requires MongoDB server version 5+ and MongoDB nodejs driver version 4+
NOTE: This package is compatible with the MongoDB nodejs driver versions 4 through 6 and supports MongoDB server versions 5 through 7.

## Usage

### Installation

Expand All @@ -17,6 +19,7 @@ npm install mongo-message-queue --save
### Require & Instantiate

Example:

```javascript
const MessageQueue = require('mongo-message-queue');

Expand All @@ -28,6 +31,7 @@ const mQueue = new MessageQueue();
Set the .databasePromise property to a function that returns a promise that (eventually) returns a database connection.

Example:

```javascript
const MongoClient = require('mongodb').MongoClient;

Expand All @@ -44,74 +48,80 @@ mQueue.databasePromise = function() {

Use the .registerWorker method to provide a processing method for a specific type of message in the queue.

Each registered worker will handle a specific type of item in the queue as specified by the first parameter. Registering an additional worker for the same type will override the processing method that is used for that type.
Each registered worker will handle a specific type of item in the queue as specified by the first parameter. Registering an additional worker for the same type will override the processing method that is used for that type.

The second parameter of the .registerWorker method is used to specify the processing method for work of that type. When work shows up in the message queue, the provided processing method will be called and provided the queueItem as the first parameter.
* Use queueItem.message to get access to the enqueued item.
* Use queueItem.retryCount to get access to the number of retries that have occurred.
* This property is not available during the first processing attempt.
The second parameter of the .registerWorker method is used to specify the processing method for work of that type. When work shows up in the message queue, the provided processing method will be called and provided the queueItem as the first parameter.

- Use queueItem.message to get access to the enqueued item.
- Use queueItem.retryCount to get access to the number of retries that have occurred.
- This property is not available during the first processing attempt.

The processing method should return a promise (chain) that (eventually) returns a status of "Completed", "Rejected", or "Retry".
* If the returned status is "Rejected"...
* Optionally set queueItem.rejectionReason to indicate why this should no longer be processed.
* Optionally set queueItem.releasedReason to indicate why this didn't process this time around.
* If the returned status is "Retry"...
* Optionally set queueItem.releasedReason to indicate why this didn't process this time around.
* Optionally set queueItem.nextReceivableTime to indicate when you want this to get picked up and re-processed.
* If you don't set queueItem.nextReceivableTime this message will be available for re-processing immediately.

Registering a worker will cause the message queue to immediately start polling for work of the specified type. By default, the message queue looks for new messages at least once every second (configurable by overriding the .pollingInterval property). Polling also occurs immediately following the processing of a previous queue message as long as there is still available work in the queue. Polling will continue to occur until the .stopPolling() method is called.
- If the returned status is "Rejected"...
- Optionally set queueItem.rejectionReason to indicate why this should no longer be processed.
- Optionally set queueItem.releasedReason to indicate why this didn't process this time around.
- If the returned status is "Retry"...
- Optionally set queueItem.releasedReason to indicate why this didn't process this time around.
- Optionally set queueItem.nextReceivableTime to indicate when you want this to get picked up and re-processed.
- If you don't set queueItem.nextReceivableTime this message will be available for re-procesing immediately.

Registering a worker will cause the message queue to immediately start polling for work of the specified type. By default, the message queue looks for new messages at least once every second (configurable by overridding the .pollingInterval property). Polling also occurs immediately following the processing of a previous queue message as long as there is still available work in the queue. Polling will continue to occur until the .stopPolling() method is called.

Example:

```javascript
mQueue.registerWorker('doSomething', function(queueItem) {
mQueue.registerWorker('doSomething', function (queueItem) {
// Return a promise to do something here...
return database.collection('somecollection')
.updateOne({_id: queueItem.message.id}, {status: queueItem.message.status})
.then(function(result) {
return "Completed";
})
.catch(function(err) {
queueItem.releasedReason = err.message;
if ((queueItem.retryCount || 0) < 5) {
queueItem.nextReceivableTime = new Date(Date.now() + (30 * 1000)); // Retry after 30 seconds...
return "Retry";
} else {
queueItem.rejectionReason = "Gave up after 5 retries.";
return "Rejected";
}
});
});
.then(function (result) {
return 'Completed';
})
.catch(function (err) {
queueItem.releasedReason = err.message;
if ((queueItem.retryCount || 0) < 5) {
queueItem.nextReceivableTime = new Date(Date.now() + 30 * 1000); // Retry after 30 seconds...
return 'Retry';
} else {
queueItem.rejectionReason = 'Gave up after 5 retries.';
return 'Rejected';
}
});
});
```

### Send work to the message queue

Enqueue a message to eventually be picked up and processed by one of the workers.

Example:

```javascript
mQueue.enqueue('doSomething', { id: 123, status: 'done' });
mQueue.enqueue('doSomething', {id: 123, status: 'done'});
```

You can also enqueue a message to be picked up in the future (instead of being immediately available for pickup) by passing in an optional options object with a future nextReceivableTime as the third parameter.

Example:

```javascript
mQueue.enqueue('doSomething', { id: 123, status: 'done' }, { nextReceivableTime: new Date(Date.now() + (30 * 1000)) });
mQueue.enqueue('doSomething', {id: 123, status: 'done'}, {nextReceivableTime: new Date(Date.now() + 30 * 1000)});
```

You can also enqueue a message and try to process it immediately with a locally registered worker. If there is not a worker for the specified type registered locally, it will get picked up and processed later when there is an available worker.
You can also enqueue a message and try to process it immediately with a locally registered worker. If there is not a worker for the specified type registered locally, it will get picked up and processed later when there is an available worker.

Example:

```javascript
mQueue.enqueueAndProcess('doSomething', { id: 123, status: 'done' });
mQueue.enqueueAndProcess('doSomething', {id: 123, status: 'done'});
```

## License

(The MIT License)

Copyright (c) 2014-2022 Wonderlic, Inc. <SoftwareDevelopment@wonderlic.com>
Copyright (c) 2014-2024 Wonderlic, Inc. <SoftwareDevelopment@wonderlic.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
Loading

0 comments on commit 54635ce

Please sign in to comment.