# hapi-cron-cluster

> A Hapi plugin to setup cron jobs that will call predefined server routes at specified times with leader election (cluster mode)

Latest version **2.1.2** (published 2019-01-23) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install hapi-cron-cluster
pnpm add hapi-cron-cluster
yarn add hapi-cron-cluster
bun add hapi-cron-cluster
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2019-01-23 |
| First published | 2017-11-29 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 5 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Meg4mi |
| Maintainers | meg4mi |
| Keywords | hapi, plugin, cron, cluster, mongodb, redis |

## Links

- npm: https://www.npmjs.com/package/hapi-cron-cluster
- Repository: https://github.com/Meg4mi/hapi-cron-cluster
- Homepage: https://github.com/Meg4mi/hapi-cron-cluster#readme
- Issues: https://github.com/Meg4mi/hapi-cron-cluster/issues
- npm.io page: https://npm.io/package/hapi-cron-cluster

## Dependencies (5)

- [cron](https://npm.io/package/cron.md) ^1.6.0
- [hoek](https://npm.io/package/hoek.md) ^5.0.2
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [redislock](https://npm.io/package/redislock.md) ^1.3.0
- [mongo-leader](https://npm.io/package/mongo-leader.md) 0.0.15

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2019-01-23
- 1.1.1 (version1.1.1) — 2017-12-03
- 1.1.0 (version1.1.0) — 2017-12-02
- 1.0.1 (version1.0.1) — 2017-11-29
- 2.1.1 — 2018-12-07
- 2.1.0 — 2017-12-03
- 2.0.3 — 2017-11-29
- 2.0.2 — 2017-11-29
- 2.0.1 — 2017-11-29
- 1.0.0 — 2017-11-29
- 2.0.0 — 2017-11-29

## README

# hapi-cron-cluster
A Hapi plugin to setup cron jobs that will call predefined server routes at specified times with leader election (cluster mode)

# Based and fork from (credits)
https://github.com/antonsamper/hapi-cron

## Requirements
This plugin is compatible with **hapi** v17+ and requires Node v8+.
If you need a version compatible with **hapi** v16 please install version [1.1.1](https://github.com/meg4mi/hapi-cron-cluster/releases/tag/v1.1.1).
The plugin need to connect to mongo or redis (for leader election)
`hapi-cron-cluster@1.1.1`

## Installation
Add `hapi-cron-cluster` as a dependency to your project:

```bash
$ npm install --save hapi-cron-cluster
or
$ npm install --save hapi-cron-cluster@1.1.1  (for hapi v16)
```


## Usage
```javascript
const Hapi = require('hapi');
const HapiCron = require('hapi-cron-cluster');

const server = new Hapi.Server();

async function allSystemsGo() {

    try {
        await server.register({
            plugin: HapiCron,
            options: {
                lock: {
                    url: 'mongodb://localhost/test', // 'redis://localhost'
                    key: 'lockTest',
                    ttl: 5000,
                    retry: 1000
                },
                jobs: [{
                    name: 'testcron',
                    time: '*/10 * * * * *',
                    timezone: 'Europe/London',
                    request: {
                        method: 'GET',
                        url: '/test-url'
                    },
                    onComplete: (res) => {
                        console.info('hapi cron has run');
                    }
                }]
            }
        });

        server.route({
            method: 'GET',
            path: '/test-url',
            handler: function (request, h) {
                return 'hello world'
            }
        });

        await server.start();
    }
    catch (err) {
        console.info('there was an error');
    }
}

allSystemsGo();
```

## Options
* `lock` - object that contains params for cluster leader election
* `lock.url` - mongodb or redis url
* `lock.key` - group key for leader election
* `lock.ttl` - time to live when lock acquire
* `lock.retry` - wait time brefore retrying to get the lock
* `jobs.name` - A unique name for the cron job
* `jobs.time` - A valid cron value. [See cron configuration](#cron-configuration)
* `jobs.timezone` - A valid [timezone](https://momentjs.com/timezone/)
* `jobs.request` - The request object containing the route url path. Other [options](https://hapijs.com/api#serverinjectoptions-callback) can also be passed into the request object 
    * `url` - Route path to request
    * `method` - Request method (defaults to `GET`) - `optional`
* `onComplete` - Function to run after the route has been requested. The function will contain the response from the request - `optional`


## Cron configuration
This plugin uses the [node-cron](https://github.com/kelektiv/node-cron) module to setup the cron job. 


### Available cron patterns:
```
Asterisk. E.g. *
Ranges. E.g. 1-3,5
Steps. E.g. */2
```
    

[Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but the node cron module allows six fields, with 1 second as the finest granularity.

### Cron Ranges
When specifying your cron values you'll need to make sure that your values fall within the ranges. For instance, some cron's use a 0-7 range for the day of week where both 0 and 7 represent Sunday. We do not.

 * Seconds: 0-59
 * Minutes: 0-59
 * Hours: 0-23
 * Day of Month: 1-31
 * Months: 0-11
 * Day of Week: 0-6

---
_Source: https://npm.io/package/hapi-cron-cluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
