UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

128 lines 4.55 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.start = exports.database_is_working = exports.number_of_clients = void 0; // Hub Registration (recording number of clients) var winston = require("./logger").getLogger("hub"); var misc = __importStar(require("smc-util/misc")); var defaults = misc.defaults, required = misc.required; // Global variables var started = false; var database_is_working = false; var the_database = undefined; var the_host = undefined; var the_port = undefined; var the_interval = undefined; // seconds var the_clients = {}; var number_of_clients = function () { return misc.len(the_clients); }; function _number_of_clients() { if (the_database == null) { throw new Error("database not yet set"); } return number_of_clients(); } exports.number_of_clients = _number_of_clients; function register_hub(cb) { winston.debug("register_hub"); if (the_database == null) { database_is_working = false; winston.debug("register_hub -- no database, so FAILED"); cb === null || cb === void 0 ? void 0 : cb("database not yet set"); return; } if (the_database._clients == null) { database_is_working = false; winston.debug("register_hub -- not connected, so FAILED"); cb === null || cb === void 0 ? void 0 : cb("database not connected"); return; } if (the_database.is_standby) { winston.debug("register_hub -- doing read query of site settings"); the_database.get_site_settings({ cb: function (err, _) { if (err) { winston.debug("register_hub -- FAILED read query"); database_is_working = false; } else { winston.debug("register_hub -- read query worked"); database_is_working = true; } }, }); return; } winston.debug("register_hub -- doing db query"); if (the_host == null || the_port == null || the_interval == null) { throw new Error("the_host, the_port, and the_interval must be set before registering this hub"); } the_database.register_hub({ host: the_host, port: the_port, clients: number_of_clients(), ttl: 3 * the_interval, cb: function (err) { if (err) { database_is_working = false; winston.debug("register_hub -- fail - " + err); } else { database_is_working = true; winston.debug("register_hub -- success"); } cb === null || cb === void 0 ? void 0 : cb(err); }, }); } function _database_is_working() { return database_is_working; } exports.database_is_working = _database_is_working; function start(opts) { opts = defaults(opts, { database: required, clients: required, host: required, port: required, interval_s: required, cb: undefined, }); winston.debug("hub_register.start..."); if (started) { throw new Error("Can't start hub_register twice"); } else { started = true; } the_database = opts.database; the_clients = opts.clients; the_host = opts.host; the_port = opts.port; the_interval = opts.interval_s; register_hub(opts.cb); setInterval(register_hub, the_interval * 1000); } exports.start = start; //# sourceMappingURL=hub_register.js.map