-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.cjs
989 lines (871 loc) · 28.8 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
'use strict';
const logError = (error, ...args) => {
if (error) {
console.error('[josk] [MongoAdapter] [logError]:', error, ...args);
}
};
/**
* Ensure (create) index on MongoDB collection, catch and log exception if thrown
* @function ensureIndex
* @param {Collection} collection - Mongo's driver Collection instance
* @param {object} keys - Field and value pairs where the field is the index key and the value describes the type of index for that field
* @param {object} opts - Set of options that controls the creation of the index
* @returns {void 0}
*/
const ensureIndex = async (collection, keys, opts) => {
try {
await collection.createIndex(keys, opts);
} catch (e) {
if (e.code === 85) {
let indexName;
const indexes = await collection.indexes();
for (const index of indexes) {
let drop = true;
for (const indexKey of Object.keys(keys)) {
if (typeof index.key[indexKey] === 'undefined') {
drop = false;
break;
}
}
for (const indexKey of Object.keys(index.key)) {
if (typeof keys[indexKey] === 'undefined') {
drop = false;
break;
}
}
if (drop) {
indexName = index.name;
break;
}
}
if (indexName) {
await collection.dropIndex(indexName);
await collection.createIndex(keys, opts);
}
} else {
console.info(`[INFO] [josk] [MongoAdapter] [ensureIndex] Can not set ${Object.keys(keys).join(' + ')} index on "${collection._name}" collection`, { keys, opts, details: e });
}
}
};
/** Class representing MongoDB adapter for JoSk */
class MongoAdapter {
/**
* Create a MongoAdapter instance
* @param {JoSk} joskInstance - JoSk instance
* @param {object} opts - configuration object
* @param {Db} opts.db - Required, Mongo's `Db` instance, like one returned from `MongoClient#db()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts = {}) {
this.name = 'mongo';
this.prefix = opts.prefix || '';
this.lockCollectionName = opts.lockCollectionName || '__JobTasks__.lock';
this.resetOnInit = opts.resetOnInit || false;
if (!opts.db) {
throw new Error('{db} option is required for MongoAdapter', {
description: 'MongoDB database {db} option is required, e.g. returned from `MongoClient.connect` method'
});
}
this.db = opts.db;
this.uniqueName = `__JobTasks__${this.prefix}`;
this.collection = opts.db.collection(this.uniqueName);
ensureIndex(this.collection, {uid: 1}, {background: false, unique: true});
ensureIndex(this.collection, {uid: 1, isDeleted: 1}, {background: false});
ensureIndex(this.collection, {executeAt: 1}, {background: false});
this.lockCollection = opts.db.collection(this.lockCollectionName);
ensureIndex(this.lockCollection, {expireAt: 1}, {background: false, expireAfterSeconds: 1});
ensureIndex(this.lockCollection, {uniqueName: 1}, {background: false, unique: true});
if (this.resetOnInit) {
this.collection.deleteMany({
isInterval: false
}).then(() => {}).catch(logError);
this.lockCollection.deleteMany({
uniqueName: this.uniqueName
}).then(() => {}).catch(logError);
}
}
/**
* @async
* @memberOf MongoAdapter
* @name ping
* @description Check connection to MongoDB
* @returns {Promise<object>}
*/
async ping() {
if (!this.joskInstance) {
const reason = 'JoSk instance not yet assigned to {joskInstance} of Storage Adapter context';
return {
status: reason,
code: 503,
statusCode: 503,
error: new Error(reason),
};
}
try {
const ping = await this.db.command({ ping: 1 });
if (ping?.ok === 1) {
return {
status: 'OK',
code: 200,
statusCode: 200,
};
}
} catch (pingError) {
return {
status: 'Internal Server Error',
code: 500,
statusCode: 500,
error: pingError
};
}
return {
status: 'Service Unavailable',
code: 503,
statusCode: 503,
error: new Error('Service Unavailable')
};
}
async acquireLock() {
const expireAt = new Date(Date.now() + this.joskInstance.zombieTime);
try {
const record = await this.lockCollection.findOne({
uniqueName: this.uniqueName
}, {
projection: {
uniqueName: 1
}
});
if (record?.uniqueName === this.uniqueName) {
return false;
}
const result = await this.lockCollection.insertOne({
uniqueName: this.uniqueName,
expireAt
});
if (result.insertedId) {
return true;
}
return false;
} catch(opError) {
if (opError?.code === 11000) {
return false;
}
this.joskInstance.__errorHandler(opError, '[acquireLock] [opError]', 'Exception inside MongoAdapter#acquireLock() method');
return false;
}
}
async releaseLock() {
await this.lockCollection.deleteOne({ uniqueName: this.uniqueName });
}
async remove(uid) {
try {
const result = await this.collection.findOneAndUpdate({
uid,
isDeleted: false
}, {
$set: {
isDeleted: true
}
}, {
returnNewDocument: false,
projection: {
_id: 1,
isDeleted: 1
}
});
const res = result?._id ? result : result?.value; // mongodb 5 vs. 6 compatibility
if (res?.isDeleted === false) {
const deleteResult = await this.collection.deleteOne({ _id: res._id });
return deleteResult?.deletedCount >= 1;
}
return false;
} catch(opError) {
this.joskInstance.__errorHandler(opError, '[remove] [opError]', 'Exception inside MongoAdapter#remove() method', uid);
return false;
}
}
async add(uid, isInterval, delay) {
const next = Date.now() + delay;
try {
const task = await this.collection.findOne({
uid: uid
});
if (!task) {
await this.collection.insertOne({
uid: uid,
delay: delay,
executeAt: new Date(next),
isInterval: isInterval,
isDeleted: false
});
return true;
}
if (task.isDeleted === false) {
let update = null;
if (task.delay !== delay) {
update = { delay };
}
if (+task.executeAt !== next) {
if (!update) {
update = {};
}
update.executeAt = new Date(next);
}
if (update) {
await this.collection.updateOne({
uid: uid
}, {
$set: update
});
}
return true;
}
return false;
} catch (opError) {
this.joskInstance.__errorHandler(opError, '[add] [opError]', 'Exception inside MongoAdapter#add()', uid);
return false;
}
}
async update(task, nextExecuteAt) {
if (typeof task !== 'object' || typeof task.uid !== 'string') {
this.joskInstance.__errorHandler({ task }, '[MongoAdapter] [update] [task]', 'Task malformed or undefined');
return false;
}
if (!nextExecuteAt instanceof Date) {
this.joskInstance.__errorHandler({ nextExecuteAt }, '[MongoAdapter] [update] [nextExecuteAt]', 'Next execution date is malformed or undefined', task.uid);
return false;
}
try {
const updateResult = await this.collection.updateOne({
uid: task.uid
}, {
$set: {
executeAt: nextExecuteAt
}
});
return updateResult?.modifiedCount >= 1;
} catch (opError) {
this.joskInstance.__errorHandler(opError, '[MongoAdapter] [update] [opError]', 'Exception inside RedisAdapter#update() method', task.uid);
return false;
}
}
async iterate(nextExecuteAt) {
const _ids = [];
const tasks = [];
const cursor = this.collection.find({
executeAt: {
$lte: new Date()
}
}, {
projection: {
_id: 1,
uid: 1,
delay: 1,
isDeleted: 1,
isInterval: 1
}
});
try {
let task;
while (await cursor.hasNext()) {
task = await cursor.next();
_ids.push(task._id);
tasks.push(task);
}
await this.collection.updateMany({
_id: {
$in: _ids
}
}, {
$set: {
executeAt: nextExecuteAt
}
});
} catch (mongoError) {
logError('[iterate] mongoError:', mongoError);
}
for (const task of tasks) {
this.joskInstance.__execute(task);
}
await cursor.close();
}
}
/** Class representing Redis adapter for JoSk */
class RedisAdapter {
/**
* Create a RedisAdapter instance
* @param {object} opts - configuration object
* @param {RedisClient} opts.client - Required, Redis'es `RedisClient` instance, like one returned from `await redis.createClient().connect()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts = {}) {
this.name = 'redis';
this.prefix = opts.prefix || 'default';
this.uniqueName = `josk:${this.prefix}`;
this.lockKey = `${this.uniqueName}:lock`;
this.resetOnInit = opts.resetOnInit || false;
if (!opts.client) {
throw new Error('{client} option is required for RedisAdapter', {
description: 'Redis database requires {client} option, e.g. returned from `redis.createClient()` or `redis.createCluster()` method'
});
}
this.client = opts.client;
if (this.resetOnInit) {
process.nextTick(async () => {
const cursor = this.client.scanIterator({
TYPE: 'hash',
MATCH: this.__getTaskKey('*'),
COUNT: 9999,
});
for await (const key of cursor) {
await this.client.del(key);
}
});
}
}
/**
* @async
* @memberOf RedisAdapter
* @name ping
* @description Check connection to Redis
* @returns {Promise<object>}
*/
async ping() {
if (!this.joskInstance) {
const reason = 'JoSk instance not yet assigned to {joskInstance} of Storage Adapter context';
return {
status: reason,
code: 503,
statusCode: 503,
error: new Error(reason),
};
}
try {
const ping = await this.client.ping();
if (ping === 'PONG') {
return {
status: 'OK',
code: 200,
statusCode: 200,
};
}
throw new Error(`Unexpected response from Redis#ping received: ${ping}`);
} catch (pingError) {
return {
status: 'Internal Server Error',
code: 500,
statusCode: 500,
error: pingError,
};
}
}
async acquireLock() {
const isLocked = await this.client.exists(this.lockKey);
if (isLocked >= 1) {
return false;
}
const res = await this.client.set(this.lockKey, `${Date.now() + this.joskInstance.zombieTime}`, {
PX: this.joskInstance.zombieTime,
NX: true
});
return res === 'OK';
}
async releaseLock() {
await this.client.del(this.lockKey);
}
async remove(uid) {
const taskKey = this.__getTaskKey(uid);
try {
const exists = await this.client.exists(taskKey);
if (!exists) {
return false;
}
await this.client.hSet(taskKey, {
isDeleted: '1'
});
await this.client.del(taskKey);
return true;
} catch (removeError) {
this.joskInstance.__errorHandler(removeError, '[remove] removeError:', 'Exception inside RedisAdapter#remove() method', uid);
return false;
}
}
async add(uid, isInterval, delay) {
const taskKey = this.__getTaskKey(uid);
try {
const exists = await this.client.exists(taskKey);
const next = Date.now() + delay;
if (!exists) {
await this.client.hSet(taskKey, {
uid: uid,
delay: `${delay}`,
executeAt: `${next}`,
isInterval: isInterval ? '1' : '0',
isDeleted: '0'
});
return true;
}
const task = await this.client.hGetAll(taskKey);
if (+task.isDeleted) {
return false;
}
let update = null;
if (+task.delay !== delay) {
update = { delay };
}
if (+task.executeAt !== next) {
if (!update) {
update = {};
}
update.executeAt = next;
}
if (update) {
await this.client.hSet(taskKey, update);
}
return false;
} catch(opError) {
this.joskInstance.__errorHandler(opError, '[add] [exist] [opError]', 'Exception inside RedisAdapter#add() method', uid);
return false;
}
}
async update(task, nextExecuteAt) {
if (typeof task !== 'object' || typeof task.uid !== 'string') {
this.joskInstance.__errorHandler({ task }, '[RedisAdapter] [update] [task]', 'Task malformed or undefined');
return false;
}
if (!nextExecuteAt instanceof Date) {
this.joskInstance.__errorHandler({ nextExecuteAt }, '[RedisAdapter] [update] [nextExecuteAt]', 'Next execution date is malformed or undefined', task.uid);
return false;
}
const taskKey = this.__getTaskKey(task.uid);
try {
const exists = await this.client.exists(taskKey);
if (!exists) {
return false;
}
await this.client.hSet(taskKey, {
executeAt: `${+nextExecuteAt}`
});
return true;
} catch (opError) {
this.joskInstance.__errorHandler(opError, '[RedisAdapter] [update] [opError]', 'Exception inside RedisAdapter#update() method', task.uid);
return false;
}
}
async iterate(nextExecuteAt) {
const now = Date.now();
const nextRetry = +nextExecuteAt;
const cursor = this.client.scanIterator({
TYPE: 'hash',
MATCH: this.__getTaskKey('*'),
COUNT: 9999,
});
for await (const taskKey of cursor) {
const task = await this.client.hGetAll(taskKey);
if (+task.executeAt <= now) {
await this.client.hSet(taskKey, {
executeAt: `${nextRetry}`
});
this.joskInstance.__execute({
uid: task.uid,
delay: +task.delay,
executeAt: +task.executeAt,
isInterval: !!+task.isInterval,
isDeleted: !!+task.isDeleted,
});
}
}
}
__getTaskKey(uid) {
return `${this.uniqueName}:task:${uid}`;
}
}
const prefixRegex = /set(Immediate|Timeout|Interval)$/;
const errors = {
setInterval: {
func: '[josk] [setInterval] the first argument must be a function!',
delay: '[josk] [setInterval] delay must be positive Number!',
uid: '[josk] [setInterval] [uid - task id must be specified (3rd argument)]'
},
setTimeout: {
func: '[josk] [setTimeout] the first argument must be a function!',
delay: '[josk] [setTimeout] delay must be positive Number!',
uid: '[josk] [setTimeout] [uid - task id must be specified (3rd argument)]'
},
setImmediate: {
func: '[josk] [setImmediate] the first argument must be a function!',
uid: '[josk] [setImmediate] [uid - task id must be specified (2nd argument)]'
},
};
/** Class representing a JoSk task runner (cron). */
class JoSk {
/**
* Create a JoSk instance
* @param {object} opts - configuration object
* @param {boolean} [opts.debug] - Enable debug logging
* @param {function} [opts.onError] - Informational hook, called instead of throwing exceptions, see readme for more details
* @param {boolean} [opts.autoClear] - Remove obsolete tasks (any tasks which are not found in the instance memory during runtime, but exists in the database)
* @param {number} [opts.zombieTime] - Time in milliseconds, after this period of time - task will be interpreted as "zombie". This parameter allows to rescue task from "zombie mode" in case when: `ready()` wasn't called, exception during runtime was thrown, or caused by bad logic
* @param {function} [opts.onExecuted] - Informational hook, called when task is finished, see readme for more details
* @param {number} [opts.minRevolvingDelay] - Minimum revolving delay — the minimum delay between tasks executions in milliseconds
* @param {number} [opts.maxRevolvingDelay] - Maximum revolving delay — the maximum delay between tasks executions in milliseconds
*/
constructor(opts = {}) {
this.debug = opts.debug || false;
this.onError = opts.onError || false;
this.autoClear = opts.autoClear || false;
this.zombieTime = opts.zombieTime || 900000;
this.onExecuted = opts.onExecuted || false;
this.isDestroyed = false;
this.minRevolvingDelay = opts.minRevolvingDelay || 128;
this.maxRevolvingDelay = opts.maxRevolvingDelay || 768;
this.nextRevolutionTimeout = null;
if (!opts.adapter || typeof opts.adapter !== 'object') {
throw new Error('{adapter} option is required for JoSk', {
description: 'JoSk requires MongoAdapter, RedisAdapter, or CustomAdapter to connect to an intermediate database'
});
}
this.tasks = {};
this._debug = (...args) => {
this.debug === true && console.info.call(console, '[DEBUG] [josk]', ...args);
};
this.adapter = opts.adapter;
this.adapter.joskInstance = this;
const adapterMethods = ['acquireLock', 'releaseLock', 'remove', 'add', 'update', 'iterate', 'ping'];
for (let i = adapterMethods.length - 1; i >= 0; i--) {
if (typeof this.adapter[adapterMethods[i]] !== 'function') {
throw new Error(`{adapter} is missing {${adapterMethods[i]}} method that is required!`);
}
}
this.__tick();
}
/**
* @async
* @memberOf JoSk
* @name ping
* @description Check package readiness and connection to Storage
* @returns {Promise<object>}
* @throws {mix}
*/
async ping() {
return await this.adapter.ping();
}
/**
* @async
* @memberOf JoSk
* Create recurring task (loop)
* @name setInterval
* @param {function} func - Function (task) to execute
* @param {number} delay - Delay between task execution in milliseconds
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
async setInterval(func, delay, uid) {
if (this.__checkState()) {
return '';
}
if (typeof func !== 'function') {
throw new Error(errors.setInterval.func);
}
if (delay < 0) {
throw new Error(errors.setInterval.delay);
}
if (typeof uid !== 'string') {
throw new Error(errors.setInterval.uid);
}
const timerId = `${uid}setInterval`;
this.tasks[timerId] = func;
await this.__add(timerId, true, delay);
return timerId;
}
/**
* @async
* @memberOf JoSk
* Create delayed task
* @name setTimeout
* @param {function} func - Function (task) to execute
* @param {number} delay - Delay before task execution in milliseconds
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
async setTimeout(func, delay, uid) {
if (this.__checkState()) {
return '';
}
if (typeof func !== 'function') {
throw new Error(errors.setTimeout.func);
}
if (delay < 0) {
throw new Error(errors.setTimeout.delay);
}
if (typeof uid !== 'string') {
throw new Error(errors.setTimeout.uid);
}
const timerId = `${uid}setTimeout`;
this.tasks[timerId] = func;
await this.__add(timerId, false, delay);
return timerId;
}
/**
* @async
* @memberOf JoSk
* Create task, which would get executed immediately and only once across multi-server setup
* @name setImmediate
* @param {function} func - Function (task) to execute
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
async setImmediate(func, uid) {
if (this.__checkState()) {
return '';
}
if (typeof func !== 'function') {
throw new Error(errors.setImmediate.func);
}
if (typeof uid !== 'string') {
throw new Error(errors.setImmediate.uid);
}
const timerId = `${uid}setImmediate`;
this.tasks[timerId] = func;
await this.__add(timerId, false, 0);
return timerId;
}
/**
* @async
* @memberOf JoSk
* Cancel (abort) current interval timer.
* Must be called in a separate event loop from `.setInterval()`
* @name clearInterval
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setInterval()`
* @param {function} [callback] - optional callback
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
*/
async clearInterval(timerId) {
if (typeof timerId === 'object' && timerId instanceof Promise) {
return await this.__remove(await timerId);
}
return await this.__remove(timerId);
}
/**
* @async
* @memberOf JoSk
* Cancel (abort) current timeout timer.
* Must be called in a separate event loop from `.setTimeout()`
* @name clearTimeout
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
* @param {function} [callback] - optional callback
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
*/
async clearTimeout(timerId) {
if (typeof timerId === 'object' && timerId instanceof Promise) {
return await this.__remove(await timerId);
}
return await this.__remove(timerId);
}
/**
* @memberOf JoSk
* Destroy JoSk instance and stop all tasks
* @name destroy
* @returns {boolean} - `true` if instance successfully destroyed, `false` if instance already destroyed
*/
destroy() {
if (!this.isDestroyed) {
this.isDestroyed = true;
if (this.nextRevolutionTimeout) {
clearTimeout(this.nextRevolutionTimeout);
this.nextRevolutionTimeout = null;
}
return true;
}
return false;
}
__checkState() {
if (this.isDestroyed) {
if (this.onError) {
const reason = 'JoSk instance destroyed';
this.onError(reason, {
description: 'invoking methods of destroyed JoSk instance',
error: new Error(reason),
uid: null
});
} else {
this._debug('[__checkState] [warn] invoking methods of destroyed JoSk instance, call cause no action');
}
return true;
}
return false;
}
async __remove(timerId) {
if (typeof timerId !== 'string') {
return false;
}
const isRemoved = await this.adapter.remove(timerId);
if (isRemoved && this.tasks?.[timerId]) {
delete this.tasks[timerId];
}
return isRemoved;
}
async __add(uid, isInterval, delay) {
if (this.isDestroyed) {
return;
}
await this.adapter.add(uid, isInterval, delay);
}
async __execute(task) {
if (this.isDestroyed || task.isDeleted === true) {
return;
}
if (!task || typeof task !== 'object' || typeof task.uid !== 'string') {
if (this.onError) {
this.onError('JoSk#__execute received malformed task', {
description: 'Something went wrong with one of your tasks - malformed or undefined',
error: null,
task: task,
uid: task.uid,
});
} else {
this._debug('[__execute] received malformed task', task);
}
return;
}
let executionsQty = 0;
if (this.tasks && typeof this.tasks[task.uid] === 'function') {
if (this.tasks[task.uid].isMissing === true) {
return;
}
const ready = async (readyArg1) => {
executionsQty++;
if (executionsQty >= 2) {
const error = new Error(`[josk] [${task.uid}] Resolution method is overspecified. Specify a callback *or* return a Promise. Task resolution was called more than once!`);
if (typeof readyArg1 === 'function') {
readyArg1(error, false);
return false;
}
throw error;
}
const date = new Date();
const timestamp = +date;
if (typeof readyArg1 === 'function') {
readyArg1(void 0, true);
}
if (task.isInterval === true) {
if (typeof readyArg1 === 'object' && readyArg1 instanceof Date && +readyArg1 >= timestamp) {
await this.adapter.update(task, readyArg1);
} else if (typeof readyArg1 === 'number' && readyArg1 >= timestamp) {
await this.adapter.update(task, new Date(readyArg1));
} else {
await this.adapter.update(task, new Date(timestamp + task.delay));
}
}
if (this.onExecuted) {
this.onExecuted(task.uid.replace(prefixRegex, ''), {
uid: task.uid,
date: date,
delay: task.delay,
timestamp: timestamp
});
}
return true;
};
let hasError;
let returnedPromise;
try {
if (task.isInterval === false) {
const originalTask = this.tasks[task.uid];
let isRemoved = false;
try {
isRemoved = await this.__remove(task.uid);
} catch (removeError) {
this._debug(`[${task.uid}] [__execute] [__remove] has thrown an exception; Check connection with StorageAdapter; removeError:`, removeError);
}
if (isRemoved === true) {
returnedPromise = originalTask(ready);
}
} else {
returnedPromise = this.tasks[task.uid](ready);
}
if (returnedPromise && returnedPromise instanceof Promise) {
await returnedPromise;
} else {
return;
}
} catch (taskExecError) {
hasError = true;
this.__errorHandler(taskExecError, 'Exception during task execution', 'An exception was thrown during task execution', task.uid);
}
if ((returnedPromise && returnedPromise instanceof Promise) || (executionsQty === 0 && hasError)) {
try {
await ready();
} catch (readyErr) {
this._debug(`[${task.uid}] [__execute] [ready] has thrown an exception; readyErr:`, readyErr);
}
}
return;
}
await this.adapter.update(task, new Date(Date.now() + this.zombieTime));
this.tasks[task.uid] = function () { };
this.tasks[task.uid].isMissing = true;
if (this.autoClear) {
try {
await this.__remove(task.uid);
this._debug(`[FYI] [${task.uid}] task was auto-cleared`);
} catch (removeError) {
this._debug(`[${task.uid}] [__execute] [this.autoClear] [__remove] has thrown an exception; removeError:`, removeError);
}
} else if (this.onError) {
this.onError('One of your tasks is missing', {
description: `Something went wrong with one of your tasks - is missing.
Try to use different instances.
It's safe to ignore this message.
If this task is obsolete - simply remove it with \`JoSk#clearTimeout('${task.uid}')\`,
or enable autoClear with \`new JoSk({autoClear: true})\``,
error: null,
uid: task.uid
});
} else {
this._debug(`[__execute] [${task.uid}] Something went wrong with one of your tasks is missing.
Try to use different instances.
It's safe to ignore this message.
If this task is obsolete - simply remove it with \`JoSk#clearTimeout(\'${task.uid}\')\`,
or enable autoClear with \`new JoSk({autoClear: true})\``);
}
}
async __iterate() {
if (this.isDestroyed) {
return;
}
const nextExecuteAt = new Date(Date.now() + this.zombieTime);
try {
const isAcquired = await this.adapter.acquireLock();
if (isAcquired) {
await this.adapter.iterate(nextExecuteAt);
await this.adapter.releaseLock();
}
this.__tick();
} catch (runError) {
this.__errorHandler(runError, '[__iterate] runError:', 'adapter.iterate has returned an error', null);
}
}
__tick() {
if (this.isDestroyed) {
return;
}
this.nextRevolutionTimeout = setTimeout(this.__iterate.bind(this), Math.round((Math.random() * this.maxRevolvingDelay) + this.minRevolvingDelay));
}
__errorHandler(error, title, description, uid) {
if (error) {
if (this.onError) {
this.onError(title, { description, error, uid });
} else {
console.error(title, { description, error, uid });
}
}
}
}
exports.JoSk = JoSk;
exports.MongoAdapter = MongoAdapter;
exports.RedisAdapter = RedisAdapter;