@tduniec/backstage-plugin-time-saver-backend
Version:
This plugin provides an implementation of charts and statistics related to your time savings that are coming from usage of your templates. Plugins is built from frontend and backend part. Backend plugin is responsible for scheduled stats parsing process a
143 lines (139 loc) • 3.92 kB
JavaScript
'use strict';
var backendPluginApi = require('@backstage/backend-plugin-api');
var handler = require('../timeSaver/handler.cjs.js');
var timeSaverApi = require('../api/timeSaverApi.cjs.js');
var ScaffolderDatabase = require('../database/ScaffolderDatabase.cjs.js');
var TimeSaverDatabase = require('../database/TimeSaverDatabase.cjs.js');
var scheduler = require('../timeSaver/scheduler.cjs.js');
var commonRouter = require('./commonRouter.cjs.js');
const TS_PLUGIN_DEFAULT_SCHEDULE = {
frequency: {
minutes: 5
},
timeout: {
minutes: 30
},
initialDelay: {
seconds: 30
}
};
class PluginInitializer {
logger;
config;
auth;
scheduler;
database;
lifecycle;
tsHandler;
apiHandler;
tsScheduler;
router;
constructor(router, logger, config, auth, database, scheduler, lifecycle) {
this.router = router;
this.logger = logger;
this.config = config;
this.auth = auth;
this.database = database;
this.scheduler = scheduler;
this.lifecycle = lifecycle;
}
static async builder(router, logger, config, auth, database, scheduler, lifecycle) {
const instance = new PluginInitializer(
router,
logger,
config,
auth,
database,
scheduler,
lifecycle
);
await instance.initialize();
return instance;
}
async initialize() {
this.logger = this.dependencies.logger;
this.config = this.dependencies.config;
this.auth = this.dependencies.auth;
this.database = this.dependencies.database;
this.scheduler = this.dependencies.scheduler;
this.lifecycle = this.dependencies.lifecycle;
const timeSaverDbInstance = await TimeSaverDatabase.TimeSaverDatabase.create(
this.database,
this.logger
);
const scaffolderDbInstance = await ScaffolderDatabase.ScaffolderDatabase.create(
this.config,
this.logger,
this.lifecycle
);
this.tsHandler = new handler.TimeSaverHandler(
this.logger,
this.config,
this.auth,
timeSaverDbInstance
);
this.apiHandler = new timeSaverApi.TimeSaverApi(
this.logger,
this.config,
this.auth,
timeSaverDbInstance,
scaffolderDbInstance
);
this.tsScheduler = new scheduler.TsScheduler(
this.logger,
this.config,
this.auth,
timeSaverDbInstance
);
let taskDefinition = TS_PLUGIN_DEFAULT_SCHEDULE;
const configurableSchedule = this.config.getOptionalConfig(
"ts.scheduler.handler"
);
if (configurableSchedule) {
taskDefinition = backendPluginApi.readSchedulerServiceTaskScheduleDefinitionFromConfig(
configurableSchedule
);
}
taskDefinition = {
...taskDefinition,
scope: taskDefinition.scope || "global"
// setting global flag on scope - scheduler will attempt to ensure that only one worker machine runs the task at a time,
};
const taskRunner = this.scheduler.createScheduledTaskRunner(taskDefinition);
this.tsScheduler.schedule(taskRunner);
this.router = commonRouter.setupCommonRoutes(
this.router,
this.logger,
this.tsHandler,
this.tsApi
);
}
get dependencies() {
if (!this.router || !this.logger || !this.config || !this.auth || !this.database || !this.scheduler || !this.lifecycle) {
throw new Error("PluginInitializer not properly initialized");
}
return {
router: this.router,
logger: this.logger,
config: this.config,
auth: this.auth,
database: this.database,
scheduler: this.scheduler,
lifecycle: this.lifecycle
};
}
get timeSaverHandler() {
return this.tsHandler;
}
get tsApi() {
return this.apiHandler;
}
get timeSaverScheduler() {
return this.tsScheduler;
}
get timeSaverRouter() {
return this.router;
}
}
exports.PluginInitializer = PluginInitializer;
//# sourceMappingURL=pluginInitializer.cjs.js.map