-
Notifications
You must be signed in to change notification settings - Fork 20
/
hustle.js
1056 lines (937 loc) · 25.3 KB
/
hustle.js
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
990
991
992
993
994
995
996
997
998
999
1000
(function(window, undefined) {
"use strict";
var version = '0.3.0';
var internal_db_version = 5;
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange
if(!indexedDB) throw 'IndexedDB required';
// define error(s) used by tcrypt
var extend_error = function(extend, errname)
{
var err = function() {
var tmp = extend.apply(this, arguments);
tmp.name = this.name = errname;
this.stack = tmp.stack
this.message = tmp.message
return this;
};
err.prototype = Object.create(extend.prototype, { constructor: { value: err } });
return err;
}
var HustleError = extend_error(window.Error, 'HustleError');
var HustleDBClosed = extend_error(HustleError, 'HustleDBClosed');
var HustleDBOpened = extend_error(HustleError, 'HustleDBOpened');
var HustleBadTube = extend_error(HustleError, 'HustleBadTube');
var HustleBadID = extend_error(HustleError, 'HustleBadID');
var HustleNotice = extend_error(window.Error, 'HustleNotice');
var HustleNotFound = extend_error(HustleNotice, 'HustleNotFound');
var Hustle = function(qoptions)
{
qoptions || (qoptions = {});
if(!qoptions.tubes) qoptions.tubes = [];
// database version. should change every time the tubes change
var db_version = qoptions.db_version ? qoptions.db_version : 1;
// how often we do DB maintenance
var maintenance_delay = qoptions.maintenance_delay ? qoptions.maintenance_delay : 1000;
// define some system db vars
var db_name = qoptions.db_name ? qoptions.db_name : 'hustle';
// our reserved tables
var tbl = {
ids: '_ids',
reserved: '_reserved',
delayed: '_delayed',
buried: '_buried'
};
// always add a default tube
if(qoptions.tubes.indexOf('default') < 0) qoptions.tubes.push('default');
var db = null;
// ---------------------------------------------------------------------
// database-related functions
// ---------------------------------------------------------------------
var check_db = function()
{
if(!db) throw new HustleDBClosed('Closed database being operated on. Did you call Hustle.open()?');
return true;
};
// will be filled in later
var do_maintenance;
/**
* helper function, creates a table if it doesn't exist, otherwise grabs
* it. returns the store.
*/
var update_table_schema = function(e, tablename, options)
{
options || (options = {});
var store = null;
var udb = e.target.result;
var keypath = options.keypath ? options.keypath : 'id';
var autoinc = options.autoincrement ? options.autoincrement : false;
// grab an existing object store or create a new one
if(udb.objectStoreNames.contains(tablename))
{
store = e.currentTarget.transaction.objectStore(tablename);
}
else
{
store = udb.createObjectStore(tablename, {keyPath: keypath, autoIncrement: autoinc});
}
if(options.indexes)
{
var keys = Object.keys(options.indexes);
for(var i = 0; i < keys.length; i++)
{
(function(key, idx) {
var index_val = idx.index ? idx.index : key;
var unique = idx.unique ? true : false;
try
{
store.createIndex(key, index_val, { unique: unique });
}
// index probably exists already
// TODO: check store.indexNames
catch(e) {}
})(keys[i], options.indexes[keys[i]]);
}
}
return store;
};
/**
* open the queue database and make sure the schema is ship-shape
*/
var open = function(options)
{
options || (options = {});
if(db) throw new HustleDBOpened('db is already open');
var version = (internal_db_version * 1000) + db_version;
var req = indexedDB.open(db_name, version);
req.onerror = function(e)
{
if(options.error) options.error(e);
}
req.onsuccess = function(e)
{
db = req.result;
if(options.success) options.success(e);
do_maintenance();
};
req.onupgradeneeded = function(e)
{
var store = null;
var tubes = qoptions.tubes;
update_table_schema(e, tbl.ids);
update_table_schema(e, tbl.reserved, {
indexes: { expire: { index: 'expire', unique: false } }
});
update_table_schema(e, tbl.delayed, {
indexes: { activate: { index: 'activate', unique: false } }
});
update_table_schema(e, tbl.buried, {
indexes: { id: { unique: false } }
});
for(var i = 0; i < tubes.length; i++)
{
if([tbl.reserved, tbl.buried].indexOf(tubes[i]) >= 0) continue;
update_table_schema(e, tubes[i], {
indexes: { priority: { index: ['priority', 'id'], unique: false } }
});
}
};
};
/**
* close the queue database
*/
var close = function()
{
if(!db) return false;
db.close();
db = null;
return true;
};
/**
* convenience function to obliterate the queue
*/
var wipe = function()
{
close();
indexedDB.deleteDatabase(db_name);
return true;
};
/**
* generate a unique, auto-incrementing ID
*/
var new_id = function(options)
{
check_db();
options || (options = {});
var id = null;
var trx = db.transaction([tbl.ids], 'readwrite');
trx.oncomplete = function(e)
{
if(!id)
{
if(options.error) options.error('bad id');
return;
}
if(options.success) options.success(id, e);
};
trx.onerror = function(e)
{
if(options.error) options.error(e);
}
// upsert the ID record
var store = trx.objectStore(tbl.ids);
var req = store.get('id');
req.onsuccess = function(e)
{
var item = req.result;
if(!item)
{
id = 1;
store.put({id: 'id', value: 1});
}
else
{
item.value++;
id = item.value;
store.put(item);
}
};
};
/**
* generic function to move a queue item from one table to another.
*/
var move_item = function(id, from, to, options)
{
options || (options = {});
var trx = db.transaction([from, to], 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
var do_move_item = function(item, success)
{
if(options.transform)
{
item = options.transform(item);
}
var store = trx.objectStore(to);
var req = store.add(item);
req.onsuccess = success;
};
var store = trx.objectStore(from);
var req;
if(from == tbl.buried)
{
// if we're looking up the buried table, we use the "id" index
var index = store.index('id');
req = index.get(id);
}
else
{
req = store.get(id);
}
req.onsuccess = function(e)
{
var item = req.result;
if(!item)
{
if(options.error) options.error(new HustleNotFound('item '+ id +' wasn\'t found'));
return;
}
var item_id = item.id;
// account for the buried table's IDs
if(from == tbl.buried) item_id = item._id;
do_move_item(item, function(e) {
var req = store.delete(item_id);
req.onerror = options.error;
});
};
};
/**
* wrapper to create a new queue item for storage in the DB
*
* valid option values are 'priority'
*/
var create_queue_item = function(data, options)
{
var item = {data: data};
var fields = [
{name: 'priority', type: 'int', default: 1024},
{name: 'delay', type: 'int', default: 0},
{name: 'ttr', type: 'int', default: 0}
];
// loop over our fields, making sure they are the correct type and
// format.
for(var i = 0; i < fields.length; i++)
{
var field = fields[i];
if(options[field.name])
{
item[field.name] = options[field.name];
switch(field.type)
{
case 'int':
item[field.name] = parseInt(item[field.name]);
break;
case 'float':
item[field.name] = parseFloat(item[field.name]);
break;
}
}
if(field.default && typeof item[field.name] == 'undefined')
{
item[field.name] = field.default;
}
}
// some defaults
item.age = 0;
item.reserves = 0;
item.releases = 0;
item.timeouts = 0;
item.buries = 0;
item.kicks = 0;
item.created = new Date().getTime();
return item;
};
// ---------------------------------------------------------------------
// queue interface functions
// ---------------------------------------------------------------------
/**
* grab an item by id from the queue
*/
var peek = function(id, options)
{
check_db();
options || (options = {});
if(!id)
{
if(options.error) options.error(new HustleBadId('bad id given'));
return false;
}
var item = null;
var tables = [tbl.reserved, tbl.delayed, tbl.buried].concat(qoptions.tubes);
var trx = db.transaction(tables, 'readonly');
trx.oncomplete = function(e)
{
if(!item && options.not_found_error)
{
if(options.error) options.error(new HustleNotFound('item '+ id +' not found'));
return;
}
if(options.success) options.success(item, e);
};
trx.onerror = function(e) { if(options.error) options.error(e); }
// scan all tables for this id
tables.forEach(function(table) {
var req;
if(table == tbl.buried)
{
var index = trx.objectStore(table).index('id');
req = index.get(id);
}
else
{
req = trx.objectStore(table).get(id);
}
req.onsuccess = function(e)
{
var res = e && e.target && e.target.result;
if(item || !res) return false;
item = res;
item.age = Math.round((new Date().getTime() - item.expire) / 1000);
if(table == tbl.reserved)
{
item.state = 'reserved';
if(item.ttr > 0)
{
item.time_left = Math.round((item.expire - new Date().getTime()) / 1000);
}
}
else if(table == tbl.buried)
{
item.state = 'buried';
}
else
{
item.state = 'ready';
if(!item.tube) item.tube = table;
}
}
});
};
/**
* put a new item in the queue in the specified tube (or the "default"
* tube)
*/
var put = function(data, options)
{
check_db();
options || (options = {});
if(!data) return false;
var tube = options.tube ? options.tube : 'default';
if(qoptions.tubes.indexOf(tube) < 0) throw new HustleBadTube('tube '+ tube +' doesn\'t exist');
var item = create_queue_item(data, options);
item.tube = tube;
if(item.delay && item.delay > 0)
{
item.activate = new Date().getTime() + (1000 * item.delay);
tube = tbl.delayed;
delete item.delayed;
}
// grab a unique ID for this item
new_id({
success: function(id) {
item.id = id;
var trx = db.transaction([tube], 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(item, e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
var store = trx.objectStore(tube);
var req = store.add(item);
req.onsuccess = function(e)
{
item.id = e.target.result;
};
},
error: function(e) {
if(options.error) options.error(new HustleBadID('error generating id'));
}
});
};
/**
* grab one item off of the given tube (or "default" tube) and move it
* onto the reserved table.
*/
var reserve = function(options)
{
check_db();
options || (options = {});
var tube = options.tube ? options.tube : 'default';
if(qoptions.tubes.indexOf(tube) < 0) throw new HustleBadTube('tube '+ tube +' doesn\'t exist');
var item = null;
var trx = db.transaction([tbl.reserved, tube], 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(item, e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
// called once we have an item, puts the item in the reserved table
var put_in_reserved = function(citem, success)
{
item = citem;
item.reserves++;
if(item.ttr > 0)
{
item.expire = new Date().getTime() + (1000 * item.ttr);
}
var store = trx.objectStore(tbl.reserved);
var req = store.add(item);
req.onsuccess = success;
};
// grab one item from the tube, and put it in reserved
var store = trx.objectStore(tube);
var index = store.index('priority');
index.openCursor().onsuccess = function(e)
{
var cursor = e.target.result;
if(cursor)
{
put_in_reserved(cursor.value, function(e) {
// remove the item from the tube once we know it's reserved
var req = store.delete(cursor.value.id);
req.onerror = options.error;
});
}
};
};
/**
* delete a queue item by id. checks all tubes and the reserved/buried
* tables as well.
*/
var del = function(id, options)
{
check_db();
options || (options = {});
peek(id, {
success: function(item) {
if(!item)
{
if(options.success) options.success(null);
return;
}
var table = item.tube;
var item_id = item.id;
if(item.state == 'reserved')
{
table = tbl.reserved;
}
else if(item.state == 'buried')
{
table = tbl.buried;
// be mindful of the buried table's own IDs
item_id = item._id;
}
var trx = db.transaction(table, 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(item, e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
trx.objectStore(table).delete(item_id);
},
error: options.error
});
};
/**
* release a reserved item back into the queue (from the tube it came
* from).
*/
var release = function(id, options)
{
check_db();
options || (options = {});
peek(id, {
not_found_error: true,
success: function(item) {
if(item.state != 'reserved')
{
if(options.error) options.error(new HustleNotFound('item '+ id +' isn\'t reserved'));
return;
}
var tube = item.tube;
var delay = options.delay && parseInt(options.delay);
if(delay) tube = tbl.delayed;
move_item(id, tbl.reserved, tube, {
transform: function(item) {
item.releases++;
if(options.priority)
{
var pri = parseInt(options.priority);
if(pri) item.priority = pri;
}
if(delay) item.activate = new Date().getTime() + (1000 * delay);
return item;
},
success: options.success,
error: options.error
});
},
error: options.error
});
};
/**
* move an item to the buried table. this is a great way to track items
* that fail a lot and can't be ignored, allowing you to look over them
* later on and see what jobs are failing.
*/
var bury = function(id, options)
{
check_db();
options || (options = {});
peek(id, {
not_found_error: true,
success: function(item) {
if(item.state == 'buried')
{
if(options.success) options.success();
return;
}
var table = item.tube;
if(item.state == 'reserved')
{
table = tbl.reserved;
}
move_item(id, table, tbl.buried, {
transform: function(titem) {
titem.buries++;
titem.tube = item.tube;
if(options.priority)
{
var pri = parseInt(options.priority);
if(pri) titem.priority = pri;
}
return titem;
},
success: options.success,
error: options.error
});
},
error: options.error
});
};
/**
* kick N many buried items back into their tubes
*/
var kick = function(num, options)
{
check_db();
options || (options = {});
var records = 0;
// open all tables since we may get a range of tubes when kicking
var tables = [tbl.buried].concat(qoptions.tubes);
var trx = db.transaction(tables, 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(records, e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
var put_in_tube = function(item, success)
{
item.kicks++;
var tube = item.tube;
// remove the buried table's ID
delete item._id;
var store = trx.objectStore(tube)
var req = store.add(item);
req.onsuccess = success;
};
// grab one item from the tube, and put it in reserved
var store = trx.objectStore(tbl.buried);
store.openCursor().onsuccess = function(e)
{
var cursor = e.target.result;
if(cursor)
{
put_in_tube(cursor.value, function(e) {
// remove the item from the tube once we know it's reserved
var req = store.delete(cursor.key);
req.onerror = options.error;
});
records++;
if(records < num) cursor.continue();
}
};
};
/**
* kick a job from the buried table by its id
*/
var kick_job = function(id, options)
{
check_db();
options || (options = {});
peek(id, {
not_found_error: true,
success: function(item) {
if(item.state != 'buried')
{
if(options.error) options.error(new HustleNotFound('item '+ id +' isn\'t buried'));
return;
}
move_item(id, tbl.buried, item.tube, {
transform: function(item) {
item.kicks++;
delete item._id;
return item;
},
success: options.success,
error: options.error
});
},
error: options.error
});
};
/**
* reset a job's ttr
*/
var touch = function(id, options)
{
check_db();
options || (options = {});
peek(id, {
not_found_error: true,
success: function(item) {
if(item.state != 'reserved')
{
console.log('item.state: ', item.state);
if(options.error) options.error(new HustleNotFound('item '+ id +' isn\'t reserved'));
return;
}
if(item.ttr <= 0)
{
if(options.success) options.success();
return;
}
var trx = db.transaction(tbl.reserved, 'readwrite');
trx.oncomplete = function(e) { if(options.success) options.success(e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
var store = trx.objectStore(tbl.reserved);
var req = store.get(id);
req.onsuccess = function(e)
{
var item = req.result;
item.expire = new Date().getTime() + (item.ttr * 1000);
store.put(item);
};
},
error: options.error
});
};
var count_ready = function(tube, options)
{
check_db();
options || (options = {});
if(qoptions.tubes.indexOf(tube) < 0) throw new HustleBadTube('tube '+ tube +' doesn\'t exist');
var count = null;
var trx = db.transaction(tube, 'readonly');
trx.oncomplete = function(e) { if(options.success) options.success(count, e); };
trx.onerror = function(e) { if(options.error) options.error(e); }
var store = trx.objectStore(tube);
var req = store.count();
req.onsuccess = function(e)
{
count = req.result;
};
};
var list = function(tube, options)
{
check_db();
options || (options = {});
var results = [];
var trx = db.transaction([tube, tbl.reserved, tbl.delayed, tbl.buried], 'readonly');
trx.oncomplete = function(e) {
if(options.success) options.success(results);
};
trx.onerror = function(e) { if(options.error) options.error(e); }
var load_results = function(table, options)
{
var store = trx.objectStore(table);
var req = store.openCursor();
req.onsuccess = function(e)
{
var cursor = e.target.result;
if(cursor)
{
var rec = cursor.value;
if(rec.tube == tube)
{
if(table != tube) rec.state = table.replace(/^_/, '');
results.push(rec);
}
cursor.continue();
}
else
{
options && options.success && options.success();
}
};
};
load_results(tube, {
success: function() {
load_results(tbl.reserved, {
success: function() {
load_results(tbl.delayed, {
success: function() {
load_results(tbl.buried);
}
});
}
});
}
});
};
/**
* A class that makes consumption of a tube more manageable. For each
* reserved item, calls the given handler function.
*
* Has two public methods: start and stop. The consumer is started by
* default on instantiation.
*/
var Consumer = function(fn, coptions)
{
coptions || (coptions = {});
var tube = coptions.tube ? coptions.tube : 'default';
var delay = coptions.delay ? coptions.delay : 100;
var interval = null;
var poll = function(options)
{
options || (options = {});
if(!interval || !db) return;
if(coptions.enable_fn)
{
var res = coptions.enable_fn();
if(!res) return false;
}
// grab an item from the tube
reserve({
tube: tube,
success: function(item) {
if(!item) return;
fn(item);
// immediately poll again for new items
setTimeout(poll);
}.bind(this)
});
};
var interval = null;
var start = function()
{
if(interval) return false;
interval = setInterval(poll, delay);
return true;
};
var stop = function()
{
if(!interval) return false;
clearInterval(interval);
interval = null;
return true;
};
start();
this.start = start;
this.stop = stop;
return this;
};
// ---------------------------------------------------------------------
// maintenance/cleanup
// ---------------------------------------------------------------------
/**
* move jobs in the delayed state into their respective tubes
*/
var move_delayed_jobs_to_ready = function(options)
{
options || (options = {});
var move_items = [];
var trx = db.transaction(tbl.delayed, 'readonly');
trx.oncomplete = function(e)
{
move_items.forEach(function(item) {
move_item(item.id, tbl.delayed, item.tube, {
error: function(e) {
console.error('Hustle: delayed move: ', e);
}
});
});
};
trx.onerror = function(e)
{
console.error('Hustle: delayed move: ', e);
if(options.error) options.error(e);
}
var store = trx.objectStore(tbl.delayed);
var index = store.index('activate');
var bound = new Date().getTime();
var range = IDBKeyRange.upperBound(bound);
index.openCursor(range).onsuccess = function(e)
{
var cursor = e.target.result;
if(cursor)
{
move_items.push(cursor.value);
cursor.continue();
}
}
};
/**
* move expired jobs to their ready tube
*/
var move_expired_jobs_to_ready = function(options)
{
options || (options = {});
var move_items = [];
var trx = db.transaction(tbl.reserved, 'readonly');
trx.oncomplete = function(e)
{
move_items.forEach(function(item) {
move_item(item.id, tbl.reserved, item.tube, {
transform: function(item) {
delete item.expire;
item.timeouts++;
return item;
},
error: function(e) {
if(e instanceof HustleNotFound)
{
move_items.erase(item);
}
console.error('Hustle: ttr move: ', e);
}
});
});
};
trx.onerror = function(e)
{
console.error('Hustle: ttr move: ', e);
if(options.error) options.error(e);
}
var store = trx.objectStore(tbl.reserved);
var index = store.index('expire');
var bound = new Date().getTime();
var range = IDBKeyRange.upperBound(bound);
index.openCursor(range).onsuccess = function(e)
{
var cursor = e.target.result;
if(cursor)
{
move_items.push(cursor.value);
cursor.continue();
}
}
};
/**
* this function does database cleanup. only runs while db is open.
*/
do_maintenance = function()
{
var run_maintenance = function()
{
if(!db) return false;
move_delayed_jobs_to_ready();
move_expired_jobs_to_ready();
setTimeout(run_maintenance, maintenance_delay);
};
setTimeout(run_maintenance, maintenance_delay);
};
// ---------------------------------------------------------------------
// exports
// ---------------------------------------------------------------------
var Queue = {
peek: peek,
put: put,
reserve: reserve,
delete: del,
release: release,
bury: bury,
kick: kick,