Skip to content

Commit

Permalink
Merge pull request #398 from linder3hs/docs/refactor-docs
Browse files Browse the repository at this point in the history
docs: migrate to es6 and add how to get tasks in node-cron
  • Loading branch information
merencia committed Nov 8, 2023
2 parents 2adca44 + 3b1dcfe commit f275e55
Show file tree
Hide file tree
Showing 2 changed files with 3,373 additions and 35 deletions.
109 changes: 77 additions & 32 deletions README.md
Expand Up @@ -7,16 +7,15 @@
[![Build Status](https://travis-ci.org/node-cron/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)
[![Dependency Status](https://david-dm.org/node-cron/node-cron.svg)](https://david-dm.org/merencia/node-cron)
[![devDependency Status](https://david-dm.org/node-cron/node-cron/dev-status.svg)](https://david-dm.org/merencia/node-cron#info=devDependencies)
[![Backers on Open Collective](https://opencollective.com/node-cron/backers/badge.svg)](#backers)
[![Sponsors on Open Collective](https://opencollective.com/node-cron/sponsors/badge.svg)](#sponsors)
[![Backers on Open Collective](https://opencollective.com/node-cron/backers/badge.svg)](#backers)
[![Sponsors on Open Collective](https://opencollective.com/node-cron/sponsors/badge.svg)](#sponsors)

The node-cron module is tiny task scheduler in pure JavaScript for node.js based on [GNU crontab](https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html). This module allows you to schedule task in node.js using full crontab syntax.

**Need a job scheduler with support for worker threads and cron syntax?** Try out the [Bree](https://github.com/breejs/bree) job scheduler!

[![NPM](https://nodei.co/npm/node-cron.png?downloads=true&downloadRank=true&stars=false)](https://nodei.co/npm/node-cron/)


## Getting Started

Install node-cron using npm:
Expand All @@ -27,8 +26,20 @@ npm install --save node-cron

Import node-cron and schedule a task:

- commonjs

```javascript
const cron = require('node-cron');

cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
```

- es6 (module)

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('* * * * *', () => {
console.log('running a task every minute');
Expand All @@ -55,22 +66,21 @@ This is a quick reference to cron syntax and also shows the options supported by

### Allowed values

| field | value |
|--------------|---------------------|
| second | 0-59 |
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names) |
| day of week | 0-7 (or names, 0 or 7 are sunday) |

| field | value |
| ------------ | --------------------------------- |
| second | 0-59 |
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names) |
| day of week | 0-7 (or names, 0 or 7 are sunday) |

#### Using multiples values

You may use multiples values separated by comma:

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('1,2,4,5 * * * *', () => {
console.log('running every minute 1, 2, 4 and 5');
Expand All @@ -82,7 +92,7 @@ cron.schedule('1,2,4,5 * * * *', () => {
You may also define a range of values:

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('1-5 * * * *', () => {
console.log('running every minute to 1 from 5');
Expand All @@ -94,7 +104,7 @@ cron.schedule('1-5 * * * *', () => {
Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: `1-10/2` that is the same as `2,4,6,8,10`. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use `*/2`.

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('*/2 * * * *', () => {
console.log('running a task every two minutes');
Expand All @@ -106,7 +116,7 @@ cron.schedule('*/2 * * * *', () => {
For month and week day you also may use names or short names. e.g:

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('* * * January,September Sunday', () => {
console.log('running on Sundays of January and September');
Expand All @@ -116,7 +126,7 @@ cron.schedule('* * * January,September Sunday', () => {
Or with short names:

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('* * * Jan,Sep Sun', () => {
console.log('running on Sundays of January and September');
Expand Down Expand Up @@ -144,7 +154,7 @@ Arguments:
**Example**:

```js
var cron = require('node-cron');
import cron from 'node-cron';

cron.schedule('0 1 * * *', () => {
console.log('Running a job at 01:00 at America/Sao_Paulo timezone');
Expand All @@ -161,9 +171,9 @@ Arguments:
Starts the scheduled task.

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

var task = cron.schedule('* * * * *', () => {
const task = cron.schedule('* * * * *', () => {
console.log('stopped task');
}, {
scheduled: false
Expand All @@ -177,9 +187,9 @@ task.start();
The task won't be executed unless re-started.

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

var task = cron.schedule('* * * * *', () => {
const task = cron.schedule('* * * * *', () => {
console.log('will execute every minute until stopped');
});

Expand All @@ -191,12 +201,50 @@ task.stop();
Validate that the given string is a valid cron expression.

```javascript
var cron = require('node-cron');
import cron from 'node-cron';

const valid = cron.validate('59 * * * *');
const invalid = cron.validate('60 * * * *');
```

### Naming tasks

You can name your tasks to make it easier to identify them in the logs.

```javascript
import cron from 'node-cron';

const task = cron.schedule('* * * * *', () => {
console.log('will execute every minute until stopped');
}, {
name: 'my-task'
});
```

### List tasks

You can list all the tasks that are currently running.

```javascript
import cron from 'node-cron';

var valid = cron.validate('59 * * * *');
var invalid = cron.validate('60 * * * *');
const tasks = cron.getTasks();

for (let [key, value] of tasks.entries()) {
console.log("key", key)
console.log("value", value)
}
```

value is an object with the following properties:

- _events
- _eventsCount
- _maxListeners
- options
- _task
- etc...

## Issues

Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
Expand All @@ -205,9 +253,9 @@ Feel free to submit issues and enhancement requests [here](https://github.com/me

In general, we follow the "fork-and-pull" Git workflow.

- Fork the repo on GitHub;
- Commit changes to a branch in your fork;
- Pull request "upstream" with your changes;
- Fork the repo on GitHub;
- Commit changes to a branch in your fork;
- Pull request "upstream" with your changes;

NOTE: Be sure to merge the latest from "upstream" before making a pull request!

Expand All @@ -225,7 +273,6 @@ Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com

<a href="https://opencollective.com/node-cron#backers" target="_blank"><img src="https://opencollective.com/node-cron/backers.svg?width=890"></a>


## Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/node-cron#sponsor)]
Expand All @@ -241,8 +288,6 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
<a href="https://opencollective.com/node-cron/sponsor/8/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/node-cron/sponsor/9/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/9/avatar.svg"></a>



## License

node-cron is under [ISC License](https://github.com/merencia/node-cron/blob/master/LICENSE.md).

0 comments on commit f275e55

Please sign in to comment.