repeat.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { __rest } from "tslib";
  2. import { parseExpression } from 'cron-parser';
  3. import { createHash } from 'crypto';
  4. import { QueueBase } from './queue-base';
  5. export class Repeat extends QueueBase {
  6. constructor(name, opts, Connection) {
  7. super(name, opts, Connection);
  8. this.repeatStrategy =
  9. (opts.settings && opts.settings.repeatStrategy) || getNextMillis;
  10. this.repeatKeyHashAlgorithm =
  11. (opts.settings && opts.settings.repeatKeyHashAlgorithm) || 'md5';
  12. }
  13. async updateRepeatableJob(name, data, opts, { override }) {
  14. var _a, _b;
  15. // Backwards compatibility for repeatable jobs for versions <= 3.0.0
  16. const repeatOpts = Object.assign({}, opts.repeat);
  17. (_a = repeatOpts.pattern) !== null && _a !== void 0 ? _a : (repeatOpts.pattern = repeatOpts.cron);
  18. delete repeatOpts.cron;
  19. // Check if we reached the limit of the repeatable job's iterations
  20. const iterationCount = repeatOpts.count ? repeatOpts.count + 1 : 1;
  21. if (typeof repeatOpts.limit !== 'undefined' &&
  22. iterationCount > repeatOpts.limit) {
  23. return;
  24. }
  25. // Check if we reached the end date of the repeatable job
  26. let now = Date.now();
  27. const { endDate } = repeatOpts;
  28. if (endDate && now > new Date(endDate).getTime()) {
  29. return;
  30. }
  31. const prevMillis = opts.prevMillis || 0;
  32. now = prevMillis < now ? now : prevMillis;
  33. const nextMillis = await this.repeatStrategy(now, repeatOpts, name);
  34. const { every, pattern } = repeatOpts;
  35. const hasImmediately = Boolean((every || pattern) && repeatOpts.immediately);
  36. const offset = hasImmediately && every ? now - nextMillis : undefined;
  37. if (nextMillis) {
  38. // We store the undecorated opts.jobId into the repeat options
  39. if (!prevMillis && opts.jobId) {
  40. repeatOpts.jobId = opts.jobId;
  41. }
  42. const legacyRepeatKey = getRepeatConcatOptions(name, repeatOpts);
  43. const newRepeatKey = (_b = opts.repeat.key) !== null && _b !== void 0 ? _b : this.hash(legacyRepeatKey);
  44. let repeatJobKey;
  45. if (override) {
  46. repeatJobKey = await this.scripts.addRepeatableJob(newRepeatKey, nextMillis, {
  47. name,
  48. endDate: endDate ? new Date(endDate).getTime() : undefined,
  49. tz: repeatOpts.tz,
  50. pattern,
  51. every,
  52. }, legacyRepeatKey);
  53. }
  54. else {
  55. const client = await this.client;
  56. repeatJobKey = await this.scripts.updateRepeatableJobMillis(client, newRepeatKey, nextMillis, legacyRepeatKey);
  57. }
  58. const { immediately } = repeatOpts, filteredRepeatOpts = __rest(repeatOpts, ["immediately"]);
  59. return this.createNextJob(name, nextMillis, repeatJobKey, Object.assign(Object.assign({}, opts), { repeat: Object.assign({ offset }, filteredRepeatOpts) }), data, iterationCount, hasImmediately);
  60. }
  61. }
  62. async createNextJob(name, nextMillis, repeatJobKey, opts, data, currentCount, hasImmediately) {
  63. //
  64. // Generate unique job id for this iteration.
  65. //
  66. const jobId = this.getRepeatJobKey(name, nextMillis, repeatJobKey, data);
  67. const now = Date.now();
  68. const delay = nextMillis + (opts.repeat.offset ? opts.repeat.offset : 0) - now;
  69. const mergedOpts = Object.assign(Object.assign({}, opts), { jobId, delay: delay < 0 || hasImmediately ? 0 : delay, timestamp: now, prevMillis: nextMillis, repeatJobKey });
  70. mergedOpts.repeat = Object.assign(Object.assign({}, opts.repeat), { count: currentCount });
  71. return this.Job.create(this, name, data, mergedOpts);
  72. }
  73. // TODO: remove legacy code in next breaking change
  74. getRepeatJobKey(name, nextMillis, repeatJobKey, data) {
  75. if (repeatJobKey.split(':').length > 2) {
  76. return this.getRepeatJobId({
  77. name: name,
  78. nextMillis: nextMillis,
  79. namespace: this.hash(repeatJobKey),
  80. jobId: data === null || data === void 0 ? void 0 : data.id,
  81. });
  82. }
  83. return this.getRepeatDelayedJobId({
  84. customKey: repeatJobKey,
  85. nextMillis,
  86. });
  87. }
  88. async removeRepeatable(name, repeat, jobId) {
  89. var _a;
  90. const repeatConcatOptions = getRepeatConcatOptions(name, Object.assign(Object.assign({}, repeat), { jobId }));
  91. const repeatJobKey = (_a = repeat.key) !== null && _a !== void 0 ? _a : this.hash(repeatConcatOptions);
  92. const legacyRepeatJobId = this.getRepeatJobId({
  93. name,
  94. nextMillis: '',
  95. namespace: this.hash(repeatConcatOptions),
  96. jobId: jobId !== null && jobId !== void 0 ? jobId : repeat.jobId,
  97. key: repeat.key,
  98. });
  99. return this.scripts.removeRepeatable(legacyRepeatJobId, repeatConcatOptions, repeatJobKey);
  100. }
  101. async removeRepeatableByKey(repeatJobKey) {
  102. const data = this.keyToData(repeatJobKey);
  103. const legacyRepeatJobId = this.getRepeatJobId({
  104. name: data.name,
  105. nextMillis: '',
  106. namespace: this.hash(repeatJobKey),
  107. jobId: data.id,
  108. });
  109. return this.scripts.removeRepeatable(legacyRepeatJobId, '', repeatJobKey);
  110. }
  111. async getRepeatableData(client, key, next) {
  112. const jobData = await client.hgetall(this.toKey('repeat:' + key));
  113. if (jobData) {
  114. return {
  115. key,
  116. name: jobData.name,
  117. endDate: parseInt(jobData.endDate) || null,
  118. tz: jobData.tz || null,
  119. pattern: jobData.pattern || null,
  120. every: jobData.every || null,
  121. next,
  122. };
  123. }
  124. return this.keyToData(key, next);
  125. }
  126. keyToData(key, next) {
  127. const data = key.split(':');
  128. const pattern = data.slice(4).join(':') || null;
  129. return {
  130. key,
  131. name: data[0],
  132. id: data[1] || null,
  133. endDate: parseInt(data[2]) || null,
  134. tz: data[3] || null,
  135. pattern,
  136. next,
  137. };
  138. }
  139. async getRepeatableJobs(start = 0, end = -1, asc = false) {
  140. const client = await this.client;
  141. const key = this.keys.repeat;
  142. const result = asc
  143. ? await client.zrange(key, start, end, 'WITHSCORES')
  144. : await client.zrevrange(key, start, end, 'WITHSCORES');
  145. const jobs = [];
  146. for (let i = 0; i < result.length; i += 2) {
  147. jobs.push(this.getRepeatableData(client, result[i], parseInt(result[i + 1])));
  148. }
  149. return Promise.all(jobs);
  150. }
  151. async getRepeatableCount() {
  152. const client = await this.client;
  153. return client.zcard(this.toKey('repeat'));
  154. }
  155. hash(str) {
  156. return createHash(this.repeatKeyHashAlgorithm).update(str).digest('hex');
  157. }
  158. getRepeatDelayedJobId({ nextMillis, customKey, }) {
  159. return `repeat:${customKey}:${nextMillis}`;
  160. }
  161. getRepeatJobId({ name, nextMillis, namespace, jobId, key, }) {
  162. const checksum = key !== null && key !== void 0 ? key : this.hash(`${name}${jobId || ''}${namespace}`);
  163. return `repeat:${checksum}:${nextMillis}`;
  164. }
  165. }
  166. function getRepeatConcatOptions(name, repeat) {
  167. const endDate = repeat.endDate ? new Date(repeat.endDate).getTime() : '';
  168. const tz = repeat.tz || '';
  169. const pattern = repeat.pattern;
  170. const suffix = (pattern ? pattern : String(repeat.every)) || '';
  171. const jobId = repeat.jobId ? repeat.jobId : '';
  172. return `${name}:${jobId}:${endDate}:${tz}:${suffix}`;
  173. }
  174. export const getNextMillis = (millis, opts) => {
  175. const pattern = opts.pattern;
  176. if (pattern && opts.every) {
  177. throw new Error('Both .pattern and .every options are defined for this repeatable job');
  178. }
  179. if (opts.every) {
  180. return (Math.floor(millis / opts.every) * opts.every +
  181. (opts.immediately ? 0 : opts.every));
  182. }
  183. const currentDate = opts.startDate && new Date(opts.startDate) > new Date(millis)
  184. ? new Date(opts.startDate)
  185. : new Date(millis);
  186. const interval = parseExpression(pattern, Object.assign(Object.assign({}, opts), { currentDate }));
  187. try {
  188. if (opts.immediately) {
  189. return new Date().getTime();
  190. }
  191. else {
  192. return interval.next().getTime();
  193. }
  194. }
  195. catch (e) {
  196. // Ignore error
  197. }
  198. };
  199. //# sourceMappingURL=repeat.js.map