1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Source: goldwasher-schedule.js</title>
|
6 |
|
7 | <script src="scripts/prettify/prettify.js"> </script>
|
8 | <script src="scripts/prettify/lang-css.js"> </script>
|
9 | |
10 |
|
11 |
|
12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
14 | </head>
|
15 |
|
16 | <body>
|
17 |
|
18 | <div id="main">
|
19 |
|
20 | <h1 class="page-title">Source: goldwasher-schedule.js</h1>
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | <section>
|
27 | <article>
|
28 | <pre class="prettyprint source linenums"><code>'use strict';
|
29 |
|
30 | var R = require('ramda');
|
31 | var gn = require('goldwasher-needle');
|
32 | var nodeSchedule = require('node-schedule');
|
33 | var util = require('util');
|
34 | var events = require('events');
|
35 |
|
36 | /**
|
37 | * Gets default options and merges with user options if any.
|
38 | * @param options
|
39 | * @returns {*}
|
40 | */
|
41 | var getDefaults = function(options) {
|
42 | var defaults = {
|
43 | rule: { second: 1 }
|
44 | };
|
45 |
|
46 | return R.merge(defaults, options);
|
47 | };
|
48 |
|
49 | var Schedule = function(targets, userOptions) {
|
50 | var _this = this;
|
51 | _this.jobs = [];
|
52 | _this.running = 0;
|
53 | _this.targets = targets;
|
54 | _this.options = userOptions ? getDefaults(userOptions) : getDefaults({});
|
55 |
|
56 | events.EventEmitter.call(this);
|
57 | return _this;
|
58 | };
|
59 |
|
60 | util.inherits(Schedule, events.EventEmitter);
|
61 |
|
62 | /**
|
63 | * Start the scheduler
|
64 | */
|
65 | Schedule.prototype.start = function() {
|
66 | var _this = this;
|
67 | _this.emit('start');
|
68 |
|
69 | R.forEach(function(target) {
|
70 | var options = R.merge(_this.options, target);
|
71 | var job = nodeSchedule.scheduleJob(options.rule, function() {
|
72 | _this.running++;
|
73 | options.start = Date.now();
|
74 |
|
75 | _this.emit('running', options);
|
76 |
|
77 | gn(
|
78 | options.url,
|
79 | options,
|
80 | function(error, result, response, body) {
|
81 | _this.running--;
|
82 | options.end = Date.now();
|
83 |
|
84 | if (error) {
|
85 | _this.emit('error', error, options);
|
86 | }
|
87 |
|
88 | _this.emit('result', result, options, response, body);
|
89 | });
|
90 | });
|
91 |
|
92 | _this.jobs.push(job);
|
93 | }, _this.targets);
|
94 | };
|
95 |
|
96 | /**
|
97 | * stop the scheduler
|
98 | */
|
99 | Schedule.prototype.stop = function() {
|
100 | var _this = this;
|
101 | R.forEach(function(job) {
|
102 | job.cancel();
|
103 | }, _this.jobs);
|
104 |
|
105 | _this.emit('stop');
|
106 | };
|
107 |
|
108 | module.exports = function(targets, userOptions) {
|
109 | return new Schedule(targets, userOptions);
|
110 | };
|
111 | </code></pre>
|
112 | </article>
|
113 | </section>
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | </div>
|
119 |
|
120 | <nav>
|
121 | <h2><a href="index.html">Index</a></h2>
|
122 | </nav>
|
123 |
|
124 | <br clear="both">
|
125 |
|
126 | <footer>
|
127 | Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Sat Oct 03 2015 20:03:58 GMT+0200 (CEST)
|
128 | </footer>
|
129 |
|
130 | <script> prettyPrint(); </script>
|
131 | <script src="scripts/linenumber.js"> </script>
|
132 | </body>
|
133 | </html>
|