121 lines
4.3 KiB
TypeScript
Raw Normal View History

import os from "node:os";
2023-11-09 22:27:20 +00:00
import { inject, injectAll, injectable } from "tsyringe";
import { OnLoad } from "@spt-aki/di/OnLoad";
import { OnUpdate } from "@spt-aki/di/OnUpdate";
2023-11-09 22:27:20 +00:00
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { ICoreConfig } from "@spt-aki/models/spt/config/ICoreConfig";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
2023-11-09 22:27:20 +00:00
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { HttpServer } from "@spt-aki/servers/HttpServer";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
import { EncodingUtil } from "@spt-aki/utils/EncodingUtil";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
2023-03-03 15:23:46 +00:00
@injectable()
export class App
{
protected onUpdateLastRun = {};
2023-11-09 22:27:20 +00:00
protected coreConfig: ICoreConfig;
2023-03-03 15:23:46 +00:00
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("LocalisationService") protected localisationService: LocalisationService,
2023-11-09 22:27:20 +00:00
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("EncodingUtil") protected encodingUtil: EncodingUtil,
@inject("HttpServer") protected httpServer: HttpServer,
2023-03-03 15:23:46 +00:00
@injectAll("OnLoad") protected onLoadComponents: OnLoad[],
2023-11-15 20:35:05 -05:00
@injectAll("OnUpdate") protected onUpdateComponents: OnUpdate[],
2023-03-03 15:23:46 +00:00
)
2023-11-09 22:27:20 +00:00
{
this.coreConfig = this.configServer.getConfig(ConfigTypes.CORE);
}
2023-03-03 15:23:46 +00:00
public async load(): Promise<void>
{
// execute onLoad callbacks
this.logger.info(this.localisationService.getText("executing_startup_callbacks"));
this.logger.debug(`OS: ${os.arch()} | ${os.version()} | ${process.platform}`);
this.logger.debug(`CPU: ${os.cpus()[0]?.model} cores: ${os.cpus().length}`);
this.logger.debug(`RAM: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)}GB`);
this.logger.debug(`PATH: ${this.encodingUtil.toBase64(process.argv[0])}`);
this.logger.debug(`PATH: ${this.encodingUtil.toBase64(process.execPath)}`);
this.logger.debug(`Server: ${globalThis.G_AKIVERSION || this.coreConfig.akiVersion}`);
if (globalThis.G_BUILDTIME)
2023-11-09 22:27:20 +00:00
{
this.logger.debug(`Date: ${globalThis.G_BUILDTIME}`);
2023-11-09 22:27:20 +00:00
}
if (globalThis.G_COMMIT)
2023-11-09 22:27:20 +00:00
{
this.logger.debug(`Commit: ${globalThis.G_COMMIT}`);
2023-11-09 22:27:20 +00:00
}
2023-03-03 15:23:46 +00:00
for (const onLoad of this.onLoadComponents)
{
await onLoad.onLoad();
}
setInterval(() =>
{
this.update(this.onUpdateComponents);
}, 5000);
}
protected async update(onUpdateComponents: OnUpdate[]): Promise<void>
{
// If the server has failed to start, skip any update calls
if (!this.httpServer.isStarted())
{
return;
}
2023-03-03 15:23:46 +00:00
for (const updateable of onUpdateComponents)
{
let success = false;
const lastRunTimeTimestamp = this.onUpdateLastRun[updateable.getRoute()] || 0; // 0 on first load so all update() calls occur on first load
const secondsSinceLastRun = this.timeUtil.getTimestamp() - lastRunTimeTimestamp;
try
{
success = await updateable.onUpdate(secondsSinceLastRun);
}
catch (err)
{
this.logUpdateException(err, updateable);
}
if (success)
{
this.onUpdateLastRun[updateable.getRoute()] = this.timeUtil.getTimestamp();
}
else
{
/* temporary for debug */
const warnTime = 20 * 60;
if (success === void 0 && !(secondsSinceLastRun % warnTime))
{
2023-11-15 20:35:05 -05:00
this.logger.debug(
this.localisationService.getText("route_onupdate_no_response", updateable.getRoute()),
);
2023-03-03 15:23:46 +00:00
}
}
}
}
protected logUpdateException(err: any, updateable: OnUpdate): void
{
this.logger.error(this.localisationService.getText("scheduled_event_failed_to_run", updateable.getRoute()));
2023-11-15 20:35:05 -05:00
if (err.message)
2023-03-03 15:23:46 +00:00
{
this.logger.error(err.message);
}
2023-11-15 20:35:05 -05:00
if (err.stack)
2023-03-03 15:23:46 +00:00
{
this.logger.error(err.stack);
}
}
2023-11-15 20:35:05 -05:00
}