@cocalc/project
Version:
CoCalc: project daemon
72 lines • 2.82 kB
JavaScript
"use strict";
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_project_status_table = exports.register_project_status_table = void 0;
const hof_1 = require("async-await-utils/hof");
const misc_1 = require("@cocalc/util/misc");
const project_status_1 = require("../project-status");
class ProjectStatusTable {
constructor(table, logger, project_id) {
this.state = "ready";
this.status_handler = this.status_handler.bind(this);
this.project_id = project_id;
this.logger = logger;
this.log("register");
this.publish = (0, hof_1.reuseInFlight)(this.publish_impl);
this.table = table;
this.table.on("closed", () => this.close());
// initializing project status server + reacting when it has something to say
this.status_server = (0, project_status_1.get_ProjectStatusServer)();
this.status_server.start();
this.status_server.on("status", this.status_handler);
}
status_handler(status) {
this.log?.("status_server event 'status'", status.timestamp);
this.publish?.(status);
}
async publish_impl(status) {
if (this.state == "ready" && this.table.get_state() != "closed") {
const next = { project_id: this.project_id, status };
this.table.set(next, "shallow");
try {
await this.table.save();
}
catch (err) {
this.log(`error saving ${err}`);
}
}
else if (this.log != null) {
this.log(`ProjectStatusTable '${this.state}' and table is ${this.table?.get_state()}`);
}
}
close() {
this.log("close");
this.status_server?.off("status", this.status_handler);
this.table?.close_no_async();
(0, misc_1.close)(this);
this.state = "closed";
}
log(...args) {
if (this.logger == null)
return;
this.logger.debug("project_status", ...args);
}
}
let project_status_table = undefined;
function register_project_status_table(table, logger, project_id) {
logger.debug("register_project_status_table");
if (project_status_table != null) {
logger.debug("register_project_status_table: cleaning up an already existing one");
project_status_table.close();
}
project_status_table = new ProjectStatusTable(table, logger, project_id);
}
exports.register_project_status_table = register_project_status_table;
function get_project_status_table() {
return project_status_table;
}
exports.get_project_status_table = get_project_status_table;
//# sourceMappingURL=project-status.js.map