Repository URL to install this package:
Version:
0.6.0 ▾
|
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentManager = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
const prom_client_1 = __importDefault(require("prom-client"));
const logger_1 = require("./logger");
const metrics_1 = require("./metrics");
const contentReuploadCounter = new prom_client_1.default.Counter({
name: 'content_reupload_counter',
help: 'How many pinned content items were uploaded',
});
metrics_1.register.registerMetric(contentReuploadCounter);
class ContentManager {
constructor() {
this.isReuploading = false;
}
attemptRefreshContentReupload(beeApi) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.refreshContentReupload(beeApi);
}
catch (error) {
logger_1.logger.error('content reupload job failed', error);
}
});
}
refreshContentReupload(beeApi) {
return __awaiter(this, void 0, void 0, function* () {
const pins = yield beeApi.getAllPins();
if (!pins.length) {
logger_1.logger.info(`no pins found`);
return;
}
logger_1.logger.info(`checking pinned content (${pins.length} pins)`);
for (const pin of pins) {
const isRetrievable = yield beeApi.isReferenceRetrievable(pin);
logger_1.logger.debug(`pin ${pin} is ${isRetrievable ? 'retrievable' : 'not retrievable'}`);
if (!isRetrievable && !this.isReuploading) {
this.isReuploading = true;
try {
logger_1.logger.debug(`reuploading pinned content: ${pin}`);
yield beeApi.reuploadPinnedData(pin);
contentReuploadCounter.inc();
logger_1.logger.info(`pinned content reuploaded: ${pin}`);
}
catch (error) {
logger_1.logger.error('failed to reupload pinned content', error);
}
this.isReuploading = false;
}
}
});
}
/**
* Start the manager that checks for pinned content availability and reuploads the data if needed.
*/
start(config) {
const refreshContent = () => __awaiter(this, void 0, void 0, function* () { return this.attemptRefreshContentReupload(new bee_js_1.Bee(config.beeApiUrl)); });
this.stop();
refreshContent();
this.interval = setInterval(refreshContent, config.refreshPeriod);
}
stop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = undefined;
}
}
}
exports.ContentManager = ContentManager;