1 | var Decode = require('jwt-decode');
|
2 |
|
3 | var assign = require('lodash.assign');
|
4 | var defaultsDeep = require('lodash.defaultsdeep');
|
5 |
|
6 |
|
7 | module.exports = CronJob;
|
8 |
|
9 |
|
10 | /**
|
11 | * Creates an object representing a CronJob
|
12 | *
|
13 | * @constructor
|
14 | */
|
15 | function CronJob (sandbox, job) {
|
16 | /**
|
17 | * @property name - The name of the cron job
|
18 | * @property schedule - The cron schedule of the job
|
19 | * @property next_scheduled_at - The next time this job is scheduled
|
20 | */
|
21 | assign(this, job);
|
22 |
|
23 | /**
|
24 | * @property claims - The claims embedded in the Webtask's token
|
25 | */
|
26 | if (job.token) {
|
27 | this.claims = Decode(job.token);
|
28 | }
|
29 | else {
|
30 | this.claims = {
|
31 | jtn: job.name,
|
32 | ten: this.container,
|
33 | };
|
34 | }
|
35 |
|
36 | /**
|
37 | * @property sandbox - The {@see Sandbox} instance used to create this Webtask instance
|
38 | */
|
39 | this.sandbox = sandbox;
|
40 |
|
41 | /**
|
42 | * @property url - The public url that can be used to invoke webtask that the cron job runs
|
43 | */
|
44 | Object.defineProperty(this, 'url', {
|
45 | enumerable: true,
|
46 | get: function () {
|
47 | return this.sandbox.url + '/api/run/' + this.container + '/' + this.name;
|
48 | }
|
49 | });
|
50 | }
|
51 |
|
52 | /**
|
53 | * Refresh this job's metadata
|
54 | *
|
55 | * @param {Function} [cb] - Optional callback function for node-style callbacks
|
56 | * @returns {Promise} A Promise that will be fulfilled with the this cron job instance
|
57 | */
|
58 | CronJob.prototype.refresh = function (cb) {
|
59 | var promise = this.sandbox.getCronJob({
|
60 | container: this.container,
|
61 | name: this.name,
|
62 | handleMetadata: handleMetadata.bind(this),
|
63 | });
|
64 |
|
65 | return cb ? promise.nodeify(cb) : promise;
|
66 |
|
67 | function handleMetadata(job) {
|
68 | assign(this, job);
|
69 |
|
70 | this.claims = Decode(job.token);
|
71 |
|
72 | return this;
|
73 | }
|
74 | };
|
75 |
|
76 | /**
|
77 | * Remove this cron job from the webtask cluster
|
78 | *
|
79 | * Note that this will not revoke the underlying webtask token, so the underlying webtask will remain functional.
|
80 | *
|
81 | * @param {Function} [cb] - Optional callback function for node-style callbacks
|
82 | * @returns {Promise} A Promise that will be fulfilled with the token
|
83 | */
|
84 | CronJob.prototype.remove = function (cb) {
|
85 | var promise = this.sandbox.removeCronJob({
|
86 | container: this.container,
|
87 | name: this.name,
|
88 | });
|
89 |
|
90 | return cb ? promise.nodeify(cb) : promise;
|
91 | };
|
92 |
|
93 | /**
|
94 | * Get the history of this cron job
|
95 | *
|
96 | * @param {Object} options - Options for retrieving the cron job.
|
97 | * @param {String} [options.offset] - The offset to use when paging through results.
|
98 | * @param {String} [options.limit] - The limit to use when paging through results.
|
99 | * @param {Function} [cb] - Optional callback function for node-style callbacks.
|
100 | * @returns {Promise} A Promise that will be fulfilled with an Array of cron job results.
|
101 | */
|
102 | CronJob.prototype.getHistory = function (options, cb) {
|
103 | if (typeof options === 'function') {
|
104 | cb = options;
|
105 | options = {};
|
106 | }
|
107 |
|
108 | if (!options) options = {};
|
109 |
|
110 | options = defaultsDeep(options, {
|
111 | container: this.container,
|
112 | name: this.name,
|
113 | offset: 0,
|
114 | limit: 10,
|
115 | });
|
116 |
|
117 | var promise = this.sandbox.getCronJobHistory(options);
|
118 |
|
119 | return cb ? promise.nodeify(cb) : promise;
|
120 | };
|
121 |
|
122 |
|
123 | /**
|
124 | * Inspect an existing webtask to optionally get code and/or secrets
|
125 | *
|
126 | * @param {Object} options - Options for inspecting the webtask.
|
127 | * @param {Boolean} [options.fetch_code] - Fetch the code associated with the webtask.
|
128 | * @param {Boolean} [options.decrypt] - Decrypt the webtask's secrets.
|
129 | * @param {Function} [cb] - Optional callback function for node-style callbacks.
|
130 | * @returns {Promise} A Promise that will be fulfilled with an Array of cron job results.
|
131 | */
|
132 | CronJob.prototype.inspect = function (options, cb) {
|
133 | options.token = this.token;
|
134 |
|
135 | var promise = this.sandbox.inspectToken(options);
|
136 |
|
137 | return cb ? promise.nodeify(cb) : promise;
|
138 | };
|
139 |
|
140 | /**
|
141 | * Set the cron job's state
|
142 | *
|
143 | * @param {Object} options - Options for updating the webtask.
|
144 | * @param {Boolean} options.state - Set the cron job's state to this.
|
145 | * @param {Function} [cb] - Optional callback function for node-style callbacks.
|
146 | * @returns {Promise} A Promise that will be fulfilled with an Array of cron job results.
|
147 | */
|
148 | CronJob.prototype.setJobState = function (options, cb) {
|
149 | options.token = this.token;
|
150 |
|
151 | var self = this;
|
152 | var promise = this.sandbox.setCronJobState({
|
153 | container: this.container,
|
154 | name: this.name,
|
155 | state: options.state,
|
156 | })
|
157 | .tap(function (job) {
|
158 | assign(self, job);
|
159 | });
|
160 |
|
161 | return cb ? promise.nodeify(cb) : promise;
|
162 | };
|