Skip to content

Commit

Permalink
Update Readme + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterra committed Mar 12, 2019
1 parent 44f4ae3 commit 7596462
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const scheduler = new Scheduler(connection, options);
Schedules an event.

```javascript
const event = { name: 'breakfast', collection: 'meals', after: new Date(), data: 'Fry' }
const event = { name: 'abandonedShoppingCart', after: new Date(), data: 'Fry' }
scheduler.schedule(event)
```

Expand All @@ -75,7 +75,7 @@ scheduler.schedule(event)

**Event Fields**
* name \<String> - Name of event that should be fired.
* cron \<String> - A cron string representing a frequency this should fire on. _(optional)_
* cron \<String> - A cron string representing a frequency this should fire on **(Override 'after')**. _(optional)_
* collection \<Object> - Info about the documents this event corresponds to. _(optional)_
* id \<ObjectId> - Value of the \_id field of the document this event corresponds to. _(optional)_
* after \<Date> - Time that the event should be triggered at, if left blank it will trigger the next time the scheduler polls. _(optional)_
Expand All @@ -89,7 +89,7 @@ scheduler.schedule(event)
Event handler.

```javascript
scheduler.on('breakfast', (meal, event) => {
scheduler.on('abandonedShoppingCart', (meal, event) => {
console.log(`${event.data} the ${meal.ingredients}`);
// Assuming the document {ingredients: "Bacon and Eggs"} is in the meals collection
// prints "Fry the Bacon and Eggs"
Expand Down Expand Up @@ -121,7 +121,7 @@ scheduler.list((err, events) => {
Find an event by name.

```javascript
scheduler.findByName('breakfast', (err, event) => {
scheduler.findByName('abandonedShoppingCart', (err, event) => {
// Do something with event
});
```
Expand All @@ -137,10 +137,10 @@ scheduler.findByName('breakfast', (err, event) => {
Find an event by id in storage object and by name.

```javascript
const event = { name: 'breakfast' id: '5a5dfd6c4879489ce958df0c', after: new Date() };
const event = { name: 'abandonedShoppingCart' id: '5a5dfd6c4879489ce958df0c', after: new Date() };
scheduler.schedule(event);

scheduler.findByStorageId('5a5dfd6c4879489ce958df0c', 'breakfast', (err, event) => {
scheduler.findByStorageId('5a5dfd6c4879489ce958df0c', 'abandonedShoppingCart', (err, event) => {
// Do something with event
});
```
Expand All @@ -157,7 +157,7 @@ scheduler.findByStorageId('5a5dfd6c4879489ce958df0c', 'breakfast', (err, event)
Remove an event.

```javascript
scheduler.remove('breakfast', null, null, (err, event) => {
scheduler.remove('abandonedShoppingCart', null, null, (err, event) => {
// Event has been removed
});
```
Expand Down
17 changes: 9 additions & 8 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
emitPerDoc: false,
queryFields: 'name collection id after'
});

const doc = {
status: details.status || 'ready',
event: details.name,
Expand All @@ -45,41 +45,42 @@ module.exports = {
data: details.data,
options
};

const queryFields = translateQueryfields(options.queryFields);
const query = _.transform(queryFields, (memo, f) => {
memo[f] = _.get(doc, f);
}, {});

if (details.cron) {
doc.cron = details.cron;
doc.conditions.after = (parser.parseExpression(details.cron).next()).toDate();
}

return { doc, query };
},

buildEvent: (doc) => {
if (!doc) {
return;
}

doc.conditions.query = doc.conditions.query || {};

if (typeof doc.conditions.query === 'string') {
doc.conditions.query = JSON.parse(doc.conditions.query);
}

if(doc.storage && doc.storage.id) {
_.extend(doc.conditions.query, {_id: doc.storage.id});
_.extend(doc.conditions.query, { _id: doc.storage.id });
}

return doc;
},

shouldExit: (err, result) => {
return !!err || !!(result.lastErrorObject && result.lastErrorObject.err);
},

buildError: (err, result) => {
return err || new SchedulerError(result.lastErrorObject.err);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function Scheduler(connection, options = {}) {
if (!event) {
return;
}

if (!event.storage.collection) {
return emit(event, null, poll);
}
Expand Down Expand Up @@ -188,7 +189,8 @@ function Scheduler(connection, options = {}) {
}

function schedule(details, cb) {
const info = helper.buildSchedule(details), callback = cb || function() {};
const info = helper.buildSchedule(details);
const callback = cb || function() {};
const collection = db.collection(collectionName);

collection.findOneAndReplace(info.query, info.doc, { upsert: true }, callback);
Expand All @@ -202,16 +204,16 @@ function Scheduler(connection, options = {}) {

function findByName(name, cb) {
const collection = db.collection(collectionName);
const filter = { event: name };
const lookup = { event: name };

collection.findOne(filter, cb);
collection.findOne(lookup, cb);
}

function findByStorageId(id, name, cb) {
const collection = db.collection(collectionName);
const filter = { 'storage.id': id.toString(), event: name };
const lookup = { 'storage.id': id.toString(), event: name };

collection.findOne(filter, cb);
collection.findOne(lookup, cb);
}

function remove(name, id, after, cb) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongo-scheduler-more",
"description": "Persistent event scheduler using mongo as storage",
"version": "1.2.1",
"version": "1.2.2",
"keywords": [
"job",
"jobs",
Expand Down

0 comments on commit 7596462

Please sign in to comment.