You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.5KB

5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # Node Cron
  2. [![npm](https://img.shields.io/npm/l/node-cron.svg)](https://github.com/merencia/node-cron/blob/master/LICENSE.md)
  3. [![npm](https://img.shields.io/npm/v/node-cron.svg)](https://img.shields.io/npm/v/node-cron.svg)
  4. [![Coverage Status](https://coveralls.io/repos/github/node-cron/node-cron/badge.svg?branch=master)](https://coveralls.io/github/node-cron/node-cron?branch=master)
  5. [![Code Climate](https://codeclimate.com/github/node-cron/node-cron/badges/gpa.svg)](https://codeclimate.com/github/merencia/node-cron)
  6. [![Build Status](https://travis-ci.org/node-cron/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)
  7. [![Dependency Status](https://david-dm.org/node-cron/node-cron.svg)](https://david-dm.org/merencia/node-cron)
  8. [![devDependency Status](https://david-dm.org/node-cron/node-cron/dev-status.svg)](https://david-dm.org/merencia/node-cron#info=devDependencies)
  9. [![Backers on Open Collective](https://opencollective.com/node-cron/backers/badge.svg)](#backers)
  10. [![Sponsors on Open Collective](https://opencollective.com/node-cron/sponsors/badge.svg)](#sponsors)
  11. 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.
  12. **Need a job scheduler with support for worker threads and cron syntax?** Try out the [Bree](https://github.com/breejs/bree) job scheduler!
  13. [![NPM](https://nodei.co/npm/node-cron.png?downloads=true&downloadRank=true&stars=false)](https://nodei.co/npm/node-cron/)
  14. ## Getting Started
  15. Install node-cron using npm:
  16. ```console
  17. $ npm install --save node-cron
  18. ```
  19. Import node-cron and schedule a task:
  20. ```javascript
  21. var cron = require('node-cron');
  22. cron.schedule('* * * * *', () => {
  23. console.log('running a task every minute');
  24. });
  25. ```
  26. ## Cron Syntax
  27. This is a quick reference to cron syntax and also shows the options supported by node-cron.
  28. ### Allowed fields
  29. ```
  30. # ┌────────────── second (optional)
  31. # │ ┌──────────── minute
  32. # │ │ ┌────────── hour
  33. # │ │ │ ┌──────── day of month
  34. # │ │ │ │ ┌────── month
  35. # │ │ │ │ │ ┌──── day of week
  36. # │ │ │ │ │ │
  37. # │ │ │ │ │ │
  38. # * * * * * *
  39. ```
  40. ### Allowed values
  41. | field | value |
  42. |--------------|---------------------|
  43. | second | 0-59 |
  44. | minute | 0-59 |
  45. | hour | 0-23 |
  46. | day of month | 1-31 |
  47. | month | 1-12 (or names) |
  48. | day of week | 0-7 (or names, 0 or 7 are sunday) |
  49. #### Using multiples values
  50. You may use multiples values separated by comma:
  51. ```javascript
  52. var cron = require('node-cron');
  53. cron.schedule('1,2,4,5 * * * *', () => {
  54. console.log('running every minute 1, 2, 4 and 5');
  55. });
  56. ```
  57. #### Using ranges
  58. You may also define a range of values:
  59. ```javascript
  60. var cron = require('node-cron');
  61. cron.schedule('1-5 * * * *', () => {
  62. console.log('running every minute to 1 from 5');
  63. });
  64. ```
  65. #### Using step values
  66. 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`.
  67. ```javascript
  68. var cron = require('node-cron');
  69. cron.schedule('*/2 * * * *', () => {
  70. console.log('running a task every two minutes');
  71. });
  72. ```
  73. #### Using names
  74. For month and week day you also may use names or short names. e.g:
  75. ```javascript
  76. var cron = require('node-cron');
  77. cron.schedule('* * * January,September Sunday', () => {
  78. console.log('running on Sundays of January and September');
  79. });
  80. ```
  81. Or with short names:
  82. ```javascript
  83. var cron = require('node-cron');
  84. cron.schedule('* * * Jan,Sep Sun', () => {
  85. console.log('running on Sundays of January and September');
  86. });
  87. ```
  88. ## Cron methods
  89. ### Schedule
  90. Schedules given task to be executed whenever the cron expression ticks.
  91. Arguments:
  92. - **expression** `string`: Cron expression
  93. - **function** `Function`: Task to be executed
  94. - **options** `Object`: Optional configuration for job scheduling.
  95. #### Options
  96. - **scheduled**: A `boolean` to set if the created task is scheduled. Default `true`;
  97. - **timezone**: The timezone that is used for job scheduling. See [moment-timezone](https://momentjs.com/timezone) for valid values.
  98. **Example**:
  99. ```js
  100. var cron = require('node-cron');
  101. cron.schedule('0 1 * * *', () => {
  102. console.log('Running a job at 01:00 at America/Sao_Paulo timezone');
  103. }, {
  104. scheduled: true,
  105. timezone: "America/Sao_Paulo"
  106. });
  107. ```
  108. ## ScheduledTask methods
  109. ### Start
  110. Starts the scheduled task.
  111. ```javascript
  112. var cron = require('node-cron');
  113. var task = cron.schedule('* * * * *', () => {
  114. console.log('stopped task');
  115. }, {
  116. scheduled: false
  117. });
  118. task.start();
  119. ```
  120. ### Stop
  121. The task won't be executed unless re-started.
  122. ```javascript
  123. var cron = require('node-cron');
  124. var task = cron.schedule('* * * * *', () => {
  125. console.log('will execute every minute until stopped');
  126. });
  127. task.stop();
  128. ```
  129. ### Destroy
  130. The task will be stopped and completely destroyed.
  131. ```javascript
  132. var cron = require('node-cron');
  133. var task = cron.schedule('* * * * *', () => {
  134. console.log('will not execute anymore, nor be able to restart');
  135. });
  136. task.destroy();
  137. ```
  138. ### Validate
  139. Validate that the given string is a valid cron expression.
  140. ```javascript
  141. var cron = require('node-cron');
  142. var valid = cron.validate('59 * * * *');
  143. var invalid = cron.validate('60 * * * *');
  144. ```
  145. ## Issues
  146. Feel free to submit issues and enhancement requests [here](https://github.com/merencia/node-cron/issues).
  147. ## Contributing
  148. In general, we follow the "fork-and-pull" Git workflow.
  149. - Fork the repo on GitHub;
  150. - Commit changes to a branch in your fork;
  151. - Pull request "upstream" with your changes;
  152. NOTE: Be sure to merge the latest from "upstream" before making a pull request!
  153. Please do not contribute code you did not write yourself, unless you are certain you have the legal ability to do so. Also ensure all contributed code can be distributed under the ISC License.
  154. ## Contributors
  155. This project exists thanks to all the people who contribute.
  156. <a href="https://github.com/node-cron/node-cron/graphs/contributors"><img src="https://opencollective.com/node-cron/contributors.svg?width=890&button=false" /></a>
  157. ## Backers
  158. Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/node-cron#backer)]
  159. <a href="https://opencollective.com/node-cron#backers" target="_blank"><img src="https://opencollective.com/node-cron/backers.svg?width=890"></a>
  160. ## Sponsors
  161. 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)]
  162. <a href="https://opencollective.com/node-cron/sponsor/0/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/0/avatar.svg"></a>
  163. <a href="https://opencollective.com/node-cron/sponsor/1/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/1/avatar.svg"></a>
  164. <a href="https://opencollective.com/node-cron/sponsor/2/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/2/avatar.svg"></a>
  165. <a href="https://opencollective.com/node-cron/sponsor/3/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/3/avatar.svg"></a>
  166. <a href="https://opencollective.com/node-cron/sponsor/4/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/4/avatar.svg"></a>
  167. <a href="https://opencollective.com/node-cron/sponsor/5/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/5/avatar.svg"></a>
  168. <a href="https://opencollective.com/node-cron/sponsor/6/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/6/avatar.svg"></a>
  169. <a href="https://opencollective.com/node-cron/sponsor/7/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/7/avatar.svg"></a>
  170. <a href="https://opencollective.com/node-cron/sponsor/8/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/8/avatar.svg"></a>
  171. <a href="https://opencollective.com/node-cron/sponsor/9/website" target="_blank"><img src="https://opencollective.com/node-cron/sponsor/9/avatar.svg"></a>
  172. ## License
  173. node-cron is under [ISC License](https://github.com/merencia/node-cron/blob/master/LICENSE.md).