@keetanetwork/anchor 0.0.108 → 0.0.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/queue/drivers/queue_firestore.d.ts.map +1 -1
- package/lib/queue/drivers/queue_firestore.js +4 -0
- package/lib/queue/drivers/queue_firestore.js.map +1 -1
- package/lib/queue/drivers/queue_postgres.d.ts +81 -1
- package/lib/queue/drivers/queue_postgres.d.ts.map +1 -1
- package/lib/queue/drivers/queue_postgres.js +248 -92
- package/lib/queue/drivers/queue_postgres.js.map +1 -1
- package/lib/queue/drivers/queue_redis.d.ts +9 -0
- package/lib/queue/drivers/queue_redis.d.ts.map +1 -1
- package/lib/queue/drivers/queue_redis.js +54 -61
- package/lib/queue/drivers/queue_redis.js.map +1 -1
- package/lib/queue/drivers/queue_sqlite3.d.ts.map +1 -1
- package/lib/queue/drivers/queue_sqlite3.js +15 -1
- package/lib/queue/drivers/queue_sqlite3.js.map +1 -1
- package/lib/queue/index.d.ts +63 -1
- package/lib/queue/index.d.ts.map +1 -1
- package/lib/queue/index.js +160 -39
- package/lib/queue/index.js.map +1 -1
- package/lib/queue/internal.d.ts +7 -0
- package/lib/queue/internal.d.ts.map +1 -1
- package/lib/queue/internal.js +9 -0
- package/lib/queue/internal.js.map +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/lib/queue/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { __addDisposableResource, __disposeResources } from "tslib";
|
|
|
2
2
|
import { assertNever } from '../utils/never.js';
|
|
3
3
|
import { asleep } from '../utils/asleep.js';
|
|
4
4
|
import { Errors } from './common.js';
|
|
5
|
-
import { MethodLogger, ManageStatusUpdates, ConvertStringToRequestID } from './internal.js';
|
|
5
|
+
import { CompareQueueEntryOrder, MethodLogger, ManageStatusUpdates, ConvertStringToRequestID } from './internal.js';
|
|
6
6
|
import { AsyncDisposableStack } from '../utils/defer.js';
|
|
7
7
|
const keetaAnchorPipeableQueueStatuses = ['completed', 'failed_permanently'];
|
|
8
8
|
/**
|
|
@@ -154,6 +154,12 @@ export class KeetaAnchorQueueStorageDriverMemory {
|
|
|
154
154
|
return (queueDuplicate);
|
|
155
155
|
}
|
|
156
156
|
})();
|
|
157
|
+
/*
|
|
158
|
+
* Oldest updated first, as the storage driver contract requires;
|
|
159
|
+
* the sort is stable, so ties keep their insertion order. Both
|
|
160
|
+
* branches above return a copy, so sorting in place is safe.
|
|
161
|
+
*/
|
|
162
|
+
allEntriesInStatus.sort(CompareQueueEntryOrder);
|
|
157
163
|
let retval = allEntriesInStatus;
|
|
158
164
|
if (filter?.limit !== undefined) {
|
|
159
165
|
retval = allEntriesInStatus.slice(0, filter.limit);
|
|
@@ -322,6 +328,13 @@ export class KeetaAnchorQueueRunner {
|
|
|
322
328
|
* prepared to handle that situation as well.
|
|
323
329
|
*/
|
|
324
330
|
processTimeout = 300_000; /* 5 minutes */
|
|
331
|
+
/**
|
|
332
|
+
* The number of completed entries fetched per maintenance pass
|
|
333
|
+
* when moving entries to the next stage. A full page means more
|
|
334
|
+
* entries may be waiting behind it, so unmoved entries are sent
|
|
335
|
+
* to the back of the line.
|
|
336
|
+
*/
|
|
337
|
+
pipeMoveFetchLimit = 100;
|
|
325
338
|
/**
|
|
326
339
|
* When piping a batch of jobs to another runner, this is the number
|
|
327
340
|
* of jobs to include in each batch (max).
|
|
@@ -511,7 +524,8 @@ export class KeetaAnchorQueueRunner {
|
|
|
511
524
|
oldStatus: '@internal',
|
|
512
525
|
by: this.workerID
|
|
513
526
|
});
|
|
514
|
-
}
|
|
527
|
+
},
|
|
528
|
+
pipeMoveFetchLimit: this.pipeMoveFetchLimit
|
|
515
529
|
});
|
|
516
530
|
}
|
|
517
531
|
decodeEntry(entry) {
|
|
@@ -693,23 +707,30 @@ export class KeetaAnchorQueueRunner {
|
|
|
693
707
|
return (true);
|
|
694
708
|
}
|
|
695
709
|
const processJobOk = Symbol('processJobOk');
|
|
696
|
-
const
|
|
697
|
-
const
|
|
698
|
-
if (timeout
|
|
699
|
-
|
|
700
|
-
if (elapsed >= timeout) {
|
|
701
|
-
logger?.debug(`Timeout of ${timeout}ms reached after processing ${index} entries (${startingStatus} phase; elapsed ${elapsed}ms)`);
|
|
702
|
-
return (processJobTimeout);
|
|
703
|
-
}
|
|
710
|
+
const processJobContended = Symbol('processJobContended');
|
|
711
|
+
const timedOut = function (index, startingStatus) {
|
|
712
|
+
if (timeout === undefined) {
|
|
713
|
+
return (false);
|
|
704
714
|
}
|
|
715
|
+
const elapsed = Date.now() - startTime;
|
|
716
|
+
if (elapsed < timeout) {
|
|
717
|
+
return (false);
|
|
718
|
+
}
|
|
719
|
+
logger?.debug(`Timeout of ${timeout}ms reached after processing ${index} entries (${startingStatus} phase; elapsed ${elapsed}ms)`);
|
|
720
|
+
return (true);
|
|
721
|
+
};
|
|
722
|
+
const processJob = async (entry, startingStatus, claimed, processor) => {
|
|
705
723
|
await this.updateRunnerLockTimestamp();
|
|
706
724
|
let setEntryStatus = { status: 'failed_temporarily', output: null };
|
|
707
725
|
logger?.debug(`Trying to acquire process lock for entry request with id ${String(entry.id)}`);
|
|
708
726
|
try {
|
|
709
727
|
/*
|
|
710
|
-
* Get a lock by setting it to 'processing'
|
|
728
|
+
* Get a lock by setting it to 'processing', unless the
|
|
729
|
+
* driver already claimed it
|
|
711
730
|
*/
|
|
712
|
-
|
|
731
|
+
if (!claimed) {
|
|
732
|
+
await this.queue.setStatus(entry.id, 'processing', { oldStatus: startingStatus, by: this.workerID });
|
|
733
|
+
}
|
|
713
734
|
/*
|
|
714
735
|
* Log an info-level log when we start and finish processing an entry
|
|
715
736
|
*/
|
|
@@ -753,7 +774,7 @@ export class KeetaAnchorQueueRunner {
|
|
|
753
774
|
catch (error) {
|
|
754
775
|
if (Errors.IncorrectStateAssertedError.isInstance(error)) {
|
|
755
776
|
logger?.debug(`Skipping request with id ${String(entry.id)} because it is no longer in the expected state "${startingStatus}"`, error);
|
|
756
|
-
return (
|
|
777
|
+
return (processJobContended);
|
|
757
778
|
}
|
|
758
779
|
logger?.error(`Failed to process request with id ${String(entry.id)}, setting state to "${setEntryStatus.status}":`, error);
|
|
759
780
|
setEntryStatus.status = 'failed_temporarily';
|
|
@@ -772,20 +793,56 @@ export class KeetaAnchorQueueRunner {
|
|
|
772
793
|
await this.queue.setStatus(entry.id, setEntryStatus.status, { oldStatus: 'processing', by: by, output: this.encodeResponse(setEntryStatus.output), error: setEntryStatus.error });
|
|
773
794
|
return (processJobOk);
|
|
774
795
|
};
|
|
775
|
-
|
|
776
|
-
*
|
|
796
|
+
/**
|
|
797
|
+
* After a timeout, report 'empty' when nothing is left in the
|
|
798
|
+
* status, so the caller knows there is no more work
|
|
777
799
|
*/
|
|
778
|
-
|
|
779
|
-
const
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
retval = false;
|
|
783
|
-
break;
|
|
800
|
+
const statusAfterTimeout = async (status) => {
|
|
801
|
+
const remaining = await this.queue.query({ status: status, limit: 1 });
|
|
802
|
+
if (remaining.length === 0) {
|
|
803
|
+
return ('empty');
|
|
784
804
|
}
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
805
|
+
return ('timeout');
|
|
806
|
+
};
|
|
807
|
+
/**
|
|
808
|
+
* Process up to `batchSize` entries in the given status, one
|
|
809
|
+
* entry per call, recursing for the next one.
|
|
810
|
+
*
|
|
811
|
+
* Returns 'empty' when no entries are left in that status,
|
|
812
|
+
* 'timeout' when the run timeout was reached, and 'more' when
|
|
813
|
+
* the batch is used up and entries may remain.
|
|
814
|
+
*
|
|
815
|
+
* Losing an entry to another worker does not use up a batch
|
|
816
|
+
* slot, but `contended` bounds the retries so a run always ends.
|
|
817
|
+
*/
|
|
818
|
+
const processStatus = async (status, processor, processed = 0, contended = 0) => {
|
|
819
|
+
if (processed >= batchSize) {
|
|
820
|
+
return ('more');
|
|
788
821
|
}
|
|
822
|
+
/*
|
|
823
|
+
* Check the timeout before picking, because a driver that
|
|
824
|
+
* claims atomically has already taken the entry by the time
|
|
825
|
+
* the pick returns
|
|
826
|
+
*/
|
|
827
|
+
if (timedOut(processed, status)) {
|
|
828
|
+
return (await statusAfterTimeout(status));
|
|
829
|
+
}
|
|
830
|
+
const next = await this.nextEntryToProcess(status, processed + contended);
|
|
831
|
+
if (next === undefined) {
|
|
832
|
+
return ('empty');
|
|
833
|
+
}
|
|
834
|
+
const result = await processJob(next.entry, status, next.claimed, processor);
|
|
835
|
+
if (result === processJobContended && contended < batchSize) {
|
|
836
|
+
return (await processStatus(status, processor, processed, contended + 1));
|
|
837
|
+
}
|
|
838
|
+
return (await processStatus(status, processor, processed + 1, contended));
|
|
839
|
+
};
|
|
840
|
+
/*
|
|
841
|
+
* Process pending jobs first
|
|
842
|
+
*/
|
|
843
|
+
const pendingResult = await processStatus('pending', this.processor.bind(this));
|
|
844
|
+
if (pendingResult === 'empty') {
|
|
845
|
+
retval = false;
|
|
789
846
|
}
|
|
790
847
|
/*
|
|
791
848
|
* Next process any pipes to other runners
|
|
@@ -821,24 +878,12 @@ export class KeetaAnchorQueueRunner {
|
|
|
821
878
|
status: 'stuck',
|
|
822
879
|
processor: this.processorStuck?.bind(this)
|
|
823
880
|
}];
|
|
824
|
-
let timeoutReached = false;
|
|
825
881
|
for (const condition of conditions) {
|
|
826
882
|
if (condition.processor === undefined) {
|
|
827
883
|
continue;
|
|
828
884
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
const entry = entries[0];
|
|
832
|
-
if (entry === undefined) {
|
|
833
|
-
break;
|
|
834
|
-
}
|
|
835
|
-
const result = await processJob(index, entry, condition.status, condition.processor);
|
|
836
|
-
if (result === processJobTimeout) {
|
|
837
|
-
timeoutReached = true;
|
|
838
|
-
break;
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
if (timeoutReached) {
|
|
885
|
+
const conditionResult = await processStatus(condition.status, condition.processor);
|
|
886
|
+
if (conditionResult === 'timeout') {
|
|
842
887
|
break;
|
|
843
888
|
}
|
|
844
889
|
}
|
|
@@ -854,6 +899,42 @@ export class KeetaAnchorQueueRunner {
|
|
|
854
899
|
await result_2;
|
|
855
900
|
}
|
|
856
901
|
}
|
|
902
|
+
/**
|
|
903
|
+
* Pick the next entry in the given status for this worker to try.
|
|
904
|
+
*
|
|
905
|
+
* When the driver can claim an entry atomically, it does, and the
|
|
906
|
+
* entry comes back already in "processing" for this worker.
|
|
907
|
+
*
|
|
908
|
+
* Otherwise query() returns the oldest entries first, so if every
|
|
909
|
+
* worker took the first one they would all race for the same entry
|
|
910
|
+
* and all but one would lose it. Instead each worker looks at the
|
|
911
|
+
* oldest `workers.count` entries and takes the one at its worker
|
|
912
|
+
* ID plus `attempt`, so concurrent workers start on different
|
|
913
|
+
* entries. Adding the attempt number rotates the slot within a
|
|
914
|
+
* run, so every worker reaches the oldest entry within
|
|
915
|
+
* `workers.count` attempts even when some worker IDs are not
|
|
916
|
+
* running. With a single worker this is simply the oldest entry.
|
|
917
|
+
* The caller still has to claim that entry.
|
|
918
|
+
*
|
|
919
|
+
* @param status The status to pick an entry from
|
|
920
|
+
* @param attempt How many entries this run has already tried in
|
|
921
|
+
* that status
|
|
922
|
+
*/
|
|
923
|
+
async nextEntryToProcess(status, attempt) {
|
|
924
|
+
if (this.queue.claimNext !== undefined) {
|
|
925
|
+
const entry = await this.queue.claimNext(status, this.workerID);
|
|
926
|
+
if (entry === null) {
|
|
927
|
+
return (undefined);
|
|
928
|
+
}
|
|
929
|
+
return ({ entry: entry, claimed: true });
|
|
930
|
+
}
|
|
931
|
+
const candidates = await this.queue.query({ status: status, limit: Math.max(1, this.workers.count) });
|
|
932
|
+
const entry = candidates[(this.workers.id + attempt) % Math.max(1, candidates.length)];
|
|
933
|
+
if (entry === undefined) {
|
|
934
|
+
return (undefined);
|
|
935
|
+
}
|
|
936
|
+
return ({ entry: entry, claimed: false });
|
|
937
|
+
}
|
|
857
938
|
async markStuckRequestsAsStuck() {
|
|
858
939
|
const stuckThreshold = this.processTimeout * this.stuckMultiplier;
|
|
859
940
|
const logger = this.methodLogger('markStuckRequestsAsStuck');
|
|
@@ -904,7 +985,8 @@ export class KeetaAnchorQueueRunner {
|
|
|
904
985
|
if (pipes.length === 0) {
|
|
905
986
|
return;
|
|
906
987
|
}
|
|
907
|
-
const
|
|
988
|
+
const fetchLimit = this.pipeMoveFetchLimit;
|
|
989
|
+
const allRequests = await this.queue.query({ status: statusTarget, limit: fetchLimit });
|
|
908
990
|
let requests = allRequests;
|
|
909
991
|
const RequestSentToPipes = new Map();
|
|
910
992
|
function IncrRequestSentToPipes(requestID) {
|
|
@@ -1071,16 +1153,55 @@ export class KeetaAnchorQueueRunner {
|
|
|
1071
1153
|
}
|
|
1072
1154
|
}
|
|
1073
1155
|
}
|
|
1156
|
+
const unmovedRequests = [];
|
|
1074
1157
|
const TotalPipes = pipes.length;
|
|
1075
1158
|
for (const request of allRequests) {
|
|
1076
1159
|
const sentCount = RequestSentToPipes.get(request.id) ?? 0;
|
|
1077
1160
|
if (sentCount !== TotalPipes) {
|
|
1078
1161
|
logger?.debug(`${statusTarget} request with id ${String(request.id)} was only moved to ${sentCount} out of ${TotalPipes} pipes -- not marking as moved`);
|
|
1162
|
+
unmovedRequests.push(request);
|
|
1079
1163
|
continue;
|
|
1080
1164
|
}
|
|
1081
1165
|
logger?.debug(`Marking ${statusTarget} request with id ${String(request.id)} as moved`);
|
|
1082
1166
|
await this.queue.setStatus(request.id, 'moved', { oldStatus: statusTarget, by: this.workerID });
|
|
1083
1167
|
}
|
|
1168
|
+
/*
|
|
1169
|
+
* If we fetched a full page, there may be more entries behind
|
|
1170
|
+
* the ones we could not move. Since query() returns the oldest
|
|
1171
|
+
* updated entries first, the same entries would be fetched again
|
|
1172
|
+
* on the next pass and block everything behind them, so touch
|
|
1173
|
+
* each one we could not move to send it to the back of the line.
|
|
1174
|
+
*
|
|
1175
|
+
* Touching resets "updated", which completed-entry retention
|
|
1176
|
+
* also reads; that is safe only because retention is skipped
|
|
1177
|
+
* for any queue with pipes (see deleteExpiredCompleted). The
|
|
1178
|
+
* original worker is kept so the entry still records who
|
|
1179
|
+
* processed it.
|
|
1180
|
+
*/
|
|
1181
|
+
if (allRequests.length >= fetchLimit) {
|
|
1182
|
+
await this.sendToBackOfLine(unmovedRequests, statusTarget);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Send entries to the back of the line by setting each to the
|
|
1187
|
+
* status it already has, which bumps "updated" and keeps the
|
|
1188
|
+
* worker that processed it. Another worker may have changed an
|
|
1189
|
+
* entry since it was fetched, so a failure is logged and skipped.
|
|
1190
|
+
*/
|
|
1191
|
+
async sendToBackOfLine(requests, status) {
|
|
1192
|
+
const logger = this.methodLogger('sendToBackOfLine');
|
|
1193
|
+
for (const request of requests) {
|
|
1194
|
+
let worker = undefined;
|
|
1195
|
+
if (request.worker !== null) {
|
|
1196
|
+
worker = request.worker;
|
|
1197
|
+
}
|
|
1198
|
+
try {
|
|
1199
|
+
await this.queue.setStatus(request.id, status, { oldStatus: status, by: worker });
|
|
1200
|
+
}
|
|
1201
|
+
catch (error) {
|
|
1202
|
+
logger?.debug(`Failed to move ${status} request with id ${String(request.id)} to the back of the line:`, error);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1084
1205
|
}
|
|
1085
1206
|
async maintain() {
|
|
1086
1207
|
const logger = this.methodLogger('maintain');
|