Skip to content

Commit

Permalink
πŸ“¦ v2.1.0
Browse files Browse the repository at this point in the history
- β˜„οΈ Meteor: `v2.3.0`
- πŸ“¦ NPM: `v2.1.0`
- πŸ‘¨β€πŸ”¬ Merge NPM and Atmosphere packages into single repository and
codebase, this should offload maintenance routine
- πŸ‘¨β€πŸ’» Improve used MongoDB indexes
- πŸ‘¨β€πŸ’» Minor codebase enhancements
- πŸ‘·β€β™‚οΈ Minor auto-tests enhancements
- πŸ‘·β€β™‚οΈ Support for `[email protected]`
  • Loading branch information
dr-dimitru committed Jun 15, 2019
1 parent b14860f commit 59a91c0
Show file tree
Hide file tree
Showing 6 changed files with 402 additions and 1,257 deletions.
77 changes: 77 additions & 0 deletions .versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
lmieulet:[email protected]
local-test:ostrio:[email protected]
[email protected]
[email protected]
[email protected]
meteorhacks:[email protected]
meteortesting:[email protected]
meteortesting:[email protected]
meteortesting:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
ostrio:[email protected]
practicalmeteor:[email protected]_1
practicalmeteor:[email protected]_2
practicalmeteor:[email protected]_7
practicalmeteor:[email protected]
practicalmeteor:[email protected]_2
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
77 changes: 69 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# JoSk

<a href="https://www.patreon.com/bePatron?u=20396046">
<img src="https://c5.patreon.com/external/logo/[email protected]" width="160">
</a>

Simple package with similar API to native `setTimeout` and `setInterval` methods, but synced between all running NodeJS instances via MongoDB Collection.

Multi-instance task manager for Node.js. This package has the support of cluster or multi-thread NodeJS instances. This package will help you to make sure only one process of each task is running.

__This is a server-only package.__

- [__Meteor__ (Atmosphere) version](https://github.com/VeliovGroup/Meteor-CRON-jobs)
- [Install](https://github.com/VeliovGroup/josk#install)
- [Install Meteor](https://github.com/VeliovGroup/josk#install-meteor)
- [API](https://github.com/VeliovGroup/josk#api)
- [Constructor](https://github.com/VeliovGroup/josk#initialization)
- [setInterval](https://github.com/VeliovGroup/josk#setintervalfunc-delay)
Expand All @@ -34,15 +38,48 @@ npm install josk --save
npm install josk@=1.1.0 --save
```

Looking for Meteor (*Atmosphere*) version? - Go to [`ostrio:cron-jobs` package](https://github.com/VeliovGroup/Meteor-CRON-jobs)

```js
const JoSk = require('josk');

//ES6 Style:
import JoSk from 'josk';
```

## Install Meteor:

```shell
meteor add ostrio:cron-jobs
```

```js
import JoSk from 'meteor/ostrio:cron-jobs';
```

### Known Meteor Issues:

```log
Error: Can't wait without a fiber
```

Can be easily solved via "bounding to Fiber":

```js
const bound = Meteor.bindEnvironment((callback) => {
callback();
});

const db = Collection.rawDatabase();
const job = new JoSk({db: db});

const task = (ready) => {
bound(() => { // <-- use "bound" inside of a task
ready();
});
};

job.setInterval(task, 60 * 60 * 1000, 'task');
```

## Notes:

This package is perfect when you have multiple servers for load-balancing, durability, an array of micro-services or any other solution with multiple running copies of code when you need to run repeating tasks, and you need to run it only once per app, not per server.
Expand Down Expand Up @@ -76,12 +113,22 @@ Accuracy - Delay of each task depends on MongoDB and "de-synchronization delay".
### Initialization:

```js
MongoClient.connect(url, (error, client) => {
MongoClient.connect('url', (error, client) => {
// To avoid "DB locks" β€” it's a good idea to use separate DB from "main" application DB
const db = client.db('dbName');
const job = new JoSk({db: db});
});
```

#### Initialization in Meteor:

```js
// Meteor.users.rawDatabase() is available in most Meteor setups
// If this is not your case, you can use `rawDatabase` form any other collection
const db = Meteor.users.rawDatabase();
const job = new JoSk({db: db});
```

Note: This library relies on job ID, so you can not pass same job (with the same ID). Always use different `uid`, even for the same task:

```js
Expand Down Expand Up @@ -267,7 +314,22 @@ job.clearTimeout(timer);
npm install --save-dev

# Before run tests you need to have running MongoDB
MONGO_URL="mongodb://127.0.0.1:27017/testCollectionName" npm test
MONGO_URL="mongodb://127.0.0.1:27017/npm-josk-test-001" npm test

# Be patient, tests are taking around 2 mins
```

### Running Tests in Meteor environment

```shell
# Default
meteor test-packages ./ --driver-package=meteortesting:mocha

# With custom port
meteor test-packages ./ --driver-package=meteortesting:mocha --port 8888

# With local MongoDB and custom port
MONGO_URL="mongodb://127.0.0.1:27017/meteor-josk-test-001" meteor test-packages ./ --driver-package=meteortesting:mocha --port 8888

# Be patient, tests are taking around 2 mins
```
Expand All @@ -278,6 +340,5 @@ MONGO_URL="mongodb://127.0.0.1:27017/testCollectionName" npm test

## Support our open source contribution:

This project wouldn't be possible without [ostr.io](https://ostr.io).

Using [ostr.io](https://ostr.io) you are not only [protecting domain names](https://ostr.io/info/domain-names-protection), [monitoring websites and servers](https://ostr.io/info/monitoring), using [Prerendering for better SEO](https://ostr.io/info/prerendering) of your JavaScript website, but support our Open Source activity, and great packages like this one are available for free.
- [Become a patron](https://www.patreon.com/bePatron?u=20396046) β€” support my open source contributions with monthly donation
- Use [ostr.io](https://ostr.io) β€” [Monitoring](https://snmp-monitoring.com), [Analytics](https://ostr.io/info/web-analytics), [WebSec](https://domain-protection.info), [Web-CRON](https://web-cron.info) and [Pre-rendering](https://prerendering.com) for a website
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ module.exports = class JoSk {
}

this.collection = opts.db.collection(`__JobTasks__${this.prefix}`);
this.collection.createIndex({uid: 1}, {background: true, unique: true}, (indexError) => {
this.collection.createIndex({uid: 1}, {background: false, unique: true}, (indexError) => {
if (indexError) {
_debug(indexError);
}
});
this.collection.createIndex({executeAt: 1, inProgress: 1}, {background: true}, (indexError) => {
this.collection.createIndex({executeAt: 1}, {background: false}, (indexError) => {
if (indexError) {
_debug(indexError);
}
Expand All @@ -63,10 +63,10 @@ module.exports = class JoSk {
}

let setNext;
let _date;
this.tasks = {};
let _date = new Date();
const runTasks = () => {
_date = new Date();
_date = new Date();
try {
this.collection.findOneAndUpdate({
executeAt: {
Expand Down
Loading

0 comments on commit 59a91c0

Please sign in to comment.