Skip to content

Commit

Permalink
Remove laterjs
Browse files Browse the repository at this point in the history
  • Loading branch information
xpepermint committed Apr 24, 2019
1 parent 72442e6 commit 5482ca1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ We can now create our first job.

```js
const job = await collection.insert({
sleepUntil: new Date('2016-01-01'),
sleepUntil: new Date('2016-01-01'), // ISO 8601 format (can include timezone)
});
```

Expand Down Expand Up @@ -141,21 +141,21 @@ const job = await collection.insert({
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| autoRemoveFieldPath | String | No | autoRemove | The `autoRemove` field path.
| collection | Object | Yes | - | MongoDB collection object.
| condition | Object | No | null | Additional query condition.
| onStart | Function/Promise | No | - | A method which is triggered when the cron is started.
| onStop | Function/Promise | No | - | A method which is triggered when the cron is stopped.
| idleDelay | Integer | No | 0 | A variable which tells how many milliseconds the worker should wait before checking for new jobs after all jobs has been processed.
| intervalFieldPath | String | No | interval | The `interval` field path.
| lockDuration | Integer | No | 600000 | A number of milliseconds for which each job gets locked for (we have to make sure that the job completes in that time frame).
| nextDelay | Integer | No | 0 | A variable which tells how fast the next job can be processed.
| onDocument | Function/Promise | No | - | A method which is triggered when a document should be processed.
| onIdle | Function/Promise | No | - | A method which is triggered when all jobs in a collection have been processed.
| onError | Function/Promise | No | - | A method which is triggered in case of an error.
| nextDelay | Integer | No | 0 | A variable which tells how fast the next job can be processed.
| onIdle | Function/Promise | No | - | A method which is triggered when all jobs in a collection have been processed.
| onStart | Function/Promise | No | - | A method which is triggered when the cron is started.
| onStop | Function/Promise | No | - | A method which is triggered when the cron is stopped.
| repeatUntilFieldPath | String | No | repeatUntil | The `repeatUntil` field path.
| reprocessDelay | Integer | No | 0 | A variable which tells how many milliseconds the worker should wait before processing the same job again in case the job is a recurring job.
| idleDelay | Integer | No | 0 | A variable which tells how many milliseconds the worker should wait before checking for new jobs after all jobs has been processed.
| lockDuration | Integer | No | 600000 | A number of milliseconds for which each job gets locked for (we have to make sure that the job completes in that time frame).
| sleepUntilFieldPath | String | No | sleepUntil | The `sleepUntil` field path.
| intervalFieldPath | String | No | interval | The `interval` field path.
| repeatUntilFieldPath | String | No | repeatUntil | The `repeatUntil` field path.
| autoRemoveFieldPath | String | No | autoRemove | The `autoRemove` field path.

```js
import { MongoClient } from 'mongodb';
Expand Down
49 changes: 38 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"@hayspec/cli": "^0.8.3",
"@hayspec/spec": "^0.8.3",
"@types/dot-object": "^1.7.0",
"@types/later": "^1.2.5",
"@types/moment": "^2.13.0",
"@types/mongodb": "^3.1.25",
"@types/node": "^11.13.7",
Expand All @@ -75,9 +74,9 @@
"typescript": "^3.4.5"
},
"dependencies": {
"cron-parser": "^2.11.0",
"dot-object": "^1.7.1",
"es6-sleep": "^2.0.2",
"later": "^1.2.0",
"moment": "^2.24.0"
}
}
21 changes: 10 additions & 11 deletions src/cron.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as parser from 'cron-parser';
import * as dot from 'dot-object';
import { promise as sleep } from 'es6-sleep';
import * as later from 'later';
import * as moment from 'moment';
import { Collection } from 'mongodb';

Expand Down Expand Up @@ -32,7 +32,7 @@ export class MongoCron {
protected running = false;
protected processing = false;
protected idle = false;
public readonly config: MongoCronCfg;
protected readonly config: MongoCronCfg;

/**
* Class constructor.
Expand Down Expand Up @@ -177,21 +177,20 @@ export class MongoCron {
* job has expired.
* @param doc Mongo document.
*/
protected getNextStart(doc) {
protected getNextStart(doc: any): Date {
if (!dot.pick(this.config.intervalFieldPath, doc)) { // not recurring job
return null;
}

const available = moment(dot.pick(this.config.sleepUntilFieldPath, doc)); // first available next date
const future = moment(available).add(this.config.reprocessDelay, 'milliseconds'); // date when the next start is possible

try { // new date
const schedule = later.parse.cron(dot.pick(this.config.intervalFieldPath, doc), true);
const dates = later.schedule(schedule)
.next(2, future.toDate(), dot.pick(this.config.repeatUntilFieldPath, doc))
.filter((d) => d >= future.toDate());
const next = dates[0];
return next instanceof Date ? next : null;
try {
const interval = parser.parseExpression(dot.pick(this.config.intervalFieldPath, doc), {
currentDate: future.toDate(),
endDate: dot.pick(this.config.repeatUntilFieldPath, doc),
});
return interval.next().toDate();
} catch (err) {
return null;
}
Expand All @@ -202,7 +201,7 @@ export class MongoCron {
* if `autoRemove` is set to `true`.
* @param doc Mongo document.
*/
public async reschedule(doc) {
public async reschedule(doc: any): Promise<void> {
const nextStart = this.getNextStart(doc);
const _id = doc._id;

Expand Down

0 comments on commit 5482ca1

Please sign in to comment.