anything-llm/server/utils/BackgroundWorkers/index.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

[BETA] Live document sync (#1719) * wip bg workers for live document sync * Add ability to re-embed specific documents across many workspaces via background queue bgworkser is gated behind expieremental system setting flag that needs to be explictly enabled UI for watching/unwatching docments that are embedded. TODO: UI to easily manage all bg tasks and see run results TODO: UI to enable this feature and background endpoints to manage it * create frontend views and paths Move elements to correct experimental scope * update migration to delete runs on removal of watched document * Add watch support to YouTube transcripts (#1716) * Add watch support to YouTube transcripts refactor how sync is done for supported types * Watch specific files in Confluence space (#1718) Add failure-prune check for runs * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * dual build update copy of alert modals * update job interval * Add support for live-sync of Github files * update copy for document sync feature * hide Experimental features from UI * update docs links * [FEAT] Implement new settings menu for experimental features (#1735) * implement new settings menu for experimental features * remove unused context save bar --------- Co-authored-by: timothycarambat <rambat1010@gmail.com> * dont run job on boot * unset workflow changes * Add persistent encryption service Relay key to collector so persistent encryption can be used Encrypt any private data in chunkSources used for replay during resync jobs * update jsDOC * Linting and organization * update modal copy for feature --------- Co-authored-by: Sean Hatfield <seanhatfield5@gmail.com>
2024-06-21 22:38:50 +02:00
const path = require("path");
const Graceful = require("@ladjs/graceful");
const Bree = require("bree");
class BackgroundService {
name = "BackgroundWorkerService";
static _instance = null;
#root = path.resolve(__dirname, "../../jobs");
constructor() {
if (BackgroundService._instance) {
this.#log("SINGLETON LOCK: Using existing BackgroundService.");
return BackgroundService._instance;
}
this.logger = this.getLogger();
BackgroundService._instance = this;
}
#log(text, ...args) {
console.log(`\x1b[36m[${this.name}]\x1b[0m ${text}`, ...args);
}
async boot() {
const { DocumentSyncQueue } = require("../../models/documentSyncQueue");
if (!(await DocumentSyncQueue.enabled())) {
this.#log("Feature is not enabled and will not be started.");
return;
}
this.#log("Starting...");
this.bree = new Bree({
logger: this.logger,
root: this.#root,
jobs: this.jobs(),
errorHandler: this.onError,
workerMessageHandler: this.onWorkerMessageHandler,
});
this.graceful = new Graceful({ brees: [this.bree], logger: this.logger });
this.graceful.listen();
this.bree.start();
this.#log("Service started");
}
async stop() {
this.#log("Stopping...");
if (!!this.graceful && !!this.bree) this.graceful.stopBree(this.bree, 0);
this.bree = null;
this.graceful = null;
this.#log("Service stopped");
}
jobs() {
return [
// Job for auto-sync of documents
// https://github.com/breejs/bree
{
name: "sync-watched-documents",
interval: "1hr",
},
];
}
getLogger() {
const { format, createLogger, transports } = require("winston");
return new createLogger({
level: "info",
format: format.combine(
format.colorize(),
format.printf(({ level, message, service }) => {
return `\x1b[36m[${service}]\x1b[0m ${level}: ${message}`;
})
),
defaultMeta: { service: this.name },
transports: [new transports.Console()],
});
}
onError(error, _workerMetadata) {
this.logger.error(`[${error.name}]: ${error.message}`);
}
onWorkerMessageHandler(message, _workerMetadata) {
this.logger.info(`[${message.name}]: ${message.message}`);
}
}
module.exports.BackgroundService = BackgroundService;