feat: complete Ancestor quote bot with production-ready Docker support

This commit is contained in:
unfunny
2026-09-13 14:14:38 -04:00
parent f2016da05a
commit fe7351a7dc
1145 changed files with 30890 additions and 97684 deletions

View File

@@ -1,4 +1,15 @@
import _defineProperty from "@babel/runtime/helpers/defineProperty";
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MatrixScheduler = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _logger = require("./logger");
var _event = require("./@types/event");
var _utils = require("./utils");
var _httpApi = require("./http-api");
/*
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
@@ -19,10 +30,7 @@ limitations under the License.
* This is an internal module which manages queuing, scheduling and retrying
* of requests.
*/
import { logger } from "./logger.js";
import { EventType } from "./@types/event.js";
import { removeElement } from "./utils.js";
import { calculateRetryBackoff } from "./http-api/index.js";
const DEBUG = false; // set true to enable console logging.
/**
@@ -31,14 +39,41 @@ const DEBUG = false; // set true to enable console logging.
* @returns Resolved/rejected depending on the outcome of the request.
*/
export class MatrixScheduler {
// eslint-disable-next-line camelcase
class MatrixScheduler {
/**
* Default retry algorithm for the matrix scheduler. Retries events up to 4 times with exponential backoff.
* Retries events up to 4 times using exponential backoff. This produces wait
* times of 2, 4, 8, and 16 seconds (30s total) after which we give up. If the
* failure was due to a rate limited request, the time specified in the error is
* waited before being retried.
* @param attempts - Number of attempts that have been made, including the one that just failed (ie. starting at 1)
* @see retryAlgorithm
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
static RETRY_BACKOFF_RATELIMIT(event, attempts, err) {
return calculateRetryBackoff(err, attempts, false);
if (err.httpStatus === 400 || err.httpStatus === 403 || err.httpStatus === 401) {
// client error; no amount of retrying with save you now.
return -1;
}
if (err instanceof _httpApi.ConnectionError) {
return -1;
}
// if event that we are trying to send is too large in any way then retrying won't help
if (err.name === "M_TOO_LARGE") {
return -1;
}
if (err.name === "M_LIMIT_EXCEEDED") {
const waitTime = err.data.retry_after_ms;
if (waitTime > 0) {
return waitTime;
}
}
if (attempts > 4) {
return -1; // give up
}
return 1000 * Math.pow(2, attempts);
}
/**
@@ -46,9 +81,10 @@ export class MatrixScheduler {
* concurrently.
* @see queueAlgorithm
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
static QUEUE_MESSAGES(event) {
// enqueue messages or events that associate with another event (redactions and relations)
if (event.getType() === EventType.RoomMessage || event.hasAssociation()) {
if (event.getType() === _event.EventType.RoomMessage || event.hasAssociation()) {
// put these events in the 'message' queue.
return "message";
}
@@ -58,7 +94,7 @@ export class MatrixScheduler {
// queueName: [{
// event: MatrixEvent, // event to send
// defer: PromiseWithResolvers, // defer to resolve/reject at the END of the retries
// defer: Deferred, // defer to resolve/reject at the END of the retries
// attempts: Number // number of times we've called processFn
// }, ...]
@@ -101,10 +137,12 @@ export class MatrixScheduler {
* the event is not put into a queue and will be sent concurrently.
*/
queueAlgorithm = MatrixScheduler.QUEUE_MESSAGES) {
_defineProperty(this, "queues", {});
_defineProperty(this, "activeQueues", []);
_defineProperty(this, "procFn", null);
_defineProperty(this, "processQueue", queueName => {
this.retryAlgorithm = retryAlgorithm;
this.queueAlgorithm = queueAlgorithm;
(0, _defineProperty2.default)(this, "queues", {});
(0, _defineProperty2.default)(this, "activeQueues", []);
(0, _defineProperty2.default)(this, "procFn", null);
(0, _defineProperty2.default)(this, "processQueue", queueName => {
// get head of queue
const obj = this.peekNextEvent(queueName);
if (!obj) {
@@ -125,7 +163,7 @@ export class MatrixScheduler {
// remove this from the queue
this.removeNextEvent(queueName);
debuglog("Queue '%s' sent event %s", queueName, obj.event.getId());
obj.resolvers.resolve(res);
obj.defer.resolve(res);
// keep processing
this.processQueue(queueName);
}, err => {
@@ -135,7 +173,7 @@ export class MatrixScheduler {
debuglog("retry(%s) err=%s event_id=%s waitTime=%s", obj.attempts, err, obj.event.getId(), waitTimeMs);
if (waitTimeMs === -1) {
// give up (you quitter!)
logger.info("Queue '%s' giving up on event %s", queueName, obj.event.getId());
_logger.logger.info("Queue '%s' giving up on event %s", queueName, obj.event.getId());
// remove this from the queue
this.clearQueue(queueName, err);
} else {
@@ -143,8 +181,6 @@ export class MatrixScheduler {
}
});
});
this.retryAlgorithm = retryAlgorithm;
this.queueAlgorithm = queueAlgorithm;
}
/**
@@ -178,7 +214,7 @@ export class MatrixScheduler {
return false;
}
let removed = false;
removeElement(this.queues[name], element => {
(0, _utils.removeElement)(this.queues[name], element => {
if (element.event.getId() === event.getId()) {
// XXX we should probably reject the promise?
// https://github.com/matrix-org/matrix-js-sdk/issues/496
@@ -217,15 +253,15 @@ export class MatrixScheduler {
if (!this.queues[queueName]) {
this.queues[queueName] = [];
}
const eventResolvers = Promise.withResolvers();
const deferred = (0, _utils.defer)();
this.queues[queueName].push({
event: event,
resolvers: eventResolvers,
defer: deferred,
attempts: 0
});
debuglog("Queue algorithm dumped event %s into queue '%s'", event.getId(), queueName);
this.startProcessingQueues();
return eventResolvers.promise;
return deferred.promise;
}
startProcessingQueues() {
if (!this.procFn) return;
@@ -246,13 +282,13 @@ export class MatrixScheduler {
if (index >= 0) {
this.activeQueues.splice(index, 1);
}
logger.info("Stopping queue '%s' as it is now empty", queueName);
_logger.logger.info("Stopping queue '%s' as it is now empty", queueName);
}
clearQueue(queueName, err) {
logger.info("clearing queue '%s'", queueName);
_logger.logger.info("clearing queue '%s'", queueName);
let obj;
while (obj = this.removeNextEvent(queueName)) {
obj.resolvers.reject(err);
obj.defer.reject(err);
}
this.disableQueue(queueName);
}
@@ -273,9 +309,10 @@ export class MatrixScheduler {
}
/* istanbul ignore next */
exports.MatrixScheduler = MatrixScheduler;
function debuglog(...args) {
if (DEBUG) {
logger.log(...args);
_logger.logger.log(...args);
}
}
//# sourceMappingURL=scheduler.js.map