Skip to content

Commit

Permalink
Fixing tests after adding extension manager #753
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Nov 16, 2023
1 parent 75d2770 commit a323634
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 120 deletions.
6 changes: 3 additions & 3 deletions benchmark/BenchmarkRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class BenchmarkRunner {
const bm = new Benchmark('List directory', req,
async (): Promise<void> => {
await ObjectManagers.reset();
await ObjectManagers.InitManagers();
await ObjectManagers.getInstance().init();
}, null,
async (): Promise<void> => {
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
Expand All @@ -135,7 +135,7 @@ export class BenchmarkRunner {
async bmListPersons(): Promise<BenchmarkResult[]> {
const bm = new Benchmark('Listing Faces', Utils.clone(this.requestTemplate), async (): Promise<void> => {
await ObjectManagers.reset();
await ObjectManagers.InitManagers();
await ObjectManagers.getInstance().init();
}, null,
async (): Promise<void> => {
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
Expand Down Expand Up @@ -289,7 +289,7 @@ export class BenchmarkRunner {
await fs.promises.rm(ProjectPath.DBFolder, {recursive: true, force: true});
Config.Database.type = DatabaseType.sqlite;
Config.Jobs.scheduled = [];
await ObjectManagers.InitManagers();
await ObjectManagers.getInstance().init();
};

private async setupDB(): Promise<void> {
Expand Down
162 changes: 87 additions & 75 deletions src/backend/model/ObjectManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,98 @@ export class ObjectManagers {
private locationManager: LocationManager;
private albumManager: AlbumManager;
private extensionManager: ExtensionManager;
private initDone = false;

constructor() {
this.managers = [];
}

public static getInstance(): ObjectManagers {
if (!this.instance) {
this.instance = new ObjectManagers();
}
return this.instance;
}

public static async reset(): Promise<void> {
Logger.silly(LOG_TAG, 'Object manager reset begin');
if (ObjectManagers.isReady()) {
if (
ObjectManagers.getInstance().IndexingManager &&
ObjectManagers.getInstance().IndexingManager.IsSavingInProgress
) {
await ObjectManagers.getInstance().IndexingManager.SavingReady;
}
for (const manager of ObjectManagers.getInstance().managers) {
if (manager === ObjectManagers.getInstance().versionManager) {
continue;
}
if (manager.cleanUp) {
await manager.cleanUp();
}
}
}

await SQLConnection.close();
this.instance = null;
Logger.debug(LOG_TAG, 'Object manager reset done');
}

public static isReady(): boolean {
return this.instance && this.instance.initDone;
}


public async init(): Promise<void> {
if (this.initDone) {
return;
}
await SQLConnection.init();
await this.initManagers();
Logger.debug(LOG_TAG, 'SQL DB inited');
this.initDone = true;
}

private async initManagers(): Promise<void> {
this.AlbumManager = new AlbumManager();
this.GalleryManager = new GalleryManager();
this.IndexingManager = new IndexingManager();
this.PersonManager = new PersonManager();
this.CoverManager = new CoverManager();
this.SearchManager = new SearchManager();
this.SharingManager = new SharingManager();
this.UserManager = new UserManager();
this.VersionManager = new VersionManager();
this.JobManager = new JobManager();
this.LocationManager = new LocationManager();
this.ExtensionManager = new ExtensionManager();

for (const manager of ObjectManagers.getInstance().managers) {
if (manager === ObjectManagers.getInstance().versionManager) {
continue;
}
if (manager.init) {
await manager.init();
}
}
}

public async onDataChange(
changedDir: ParentDirectoryDTO = null
): Promise<void> {
await this.VersionManager.onNewDataVersion();

for (const manager of this.managers) {
if (manager === this.versionManager) {
continue;
}
if (manager.onNewDataVersion) {
await manager.onNewDataVersion(changedDir);
}
}
}


get VersionManager(): VersionManager {
return this.versionManager;
}
Expand Down Expand Up @@ -182,79 +269,4 @@ export class ObjectManagers {
this.extensionManager = value;
this.managers.push(this.extensionManager as IObjectManager);
}

public static getInstance(): ObjectManagers {
if (this.instance === null) {
this.instance = new ObjectManagers();
}
return this.instance;
}

public static async reset(): Promise<void> {
Logger.silly(LOG_TAG, 'Object manager reset begin');
if (
ObjectManagers.getInstance().IndexingManager &&
ObjectManagers.getInstance().IndexingManager.IsSavingInProgress
) {
await ObjectManagers.getInstance().IndexingManager.SavingReady;
}
for (const manager of ObjectManagers.getInstance().managers) {
if (manager === ObjectManagers.getInstance().versionManager) {
continue;
}
if (manager.cleanUp) {
await manager.cleanUp();
}
}

await SQLConnection.close();
this.instance = null;
Logger.debug(LOG_TAG, 'Object manager reset done');
}

public static async InitManagers(): Promise<void> {
await ObjectManagers.reset();
await SQLConnection.init();
await this.initManagers();
Logger.debug(LOG_TAG, 'SQL DB inited');
}

private static async initManagers(): Promise<void> {
ObjectManagers.getInstance().AlbumManager = new AlbumManager();
ObjectManagers.getInstance().GalleryManager = new GalleryManager();
ObjectManagers.getInstance().IndexingManager = new IndexingManager();
ObjectManagers.getInstance().PersonManager = new PersonManager();
ObjectManagers.getInstance().CoverManager = new CoverManager();
ObjectManagers.getInstance().SearchManager = new SearchManager();
ObjectManagers.getInstance().SharingManager = new SharingManager();
ObjectManagers.getInstance().UserManager = new UserManager();
ObjectManagers.getInstance().VersionManager = new VersionManager();
ObjectManagers.getInstance().JobManager = new JobManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
ObjectManagers.getInstance().ExtensionManager = new ExtensionManager();

for (const manager of ObjectManagers.getInstance().managers) {
if (manager === ObjectManagers.getInstance().versionManager) {
continue;
}
if (manager.init) {
await manager.init();
}
}
}

public async onDataChange(
changedDir: ParentDirectoryDTO = null
): Promise<void> {
await this.VersionManager.onNewDataVersion();

for (const manager of this.managers) {
if (manager === this.versionManager) {
continue;
}
if (manager.onNewDataVersion) {
await manager.onNewDataVersion(changedDir);
}
}
}
}
6 changes: 4 additions & 2 deletions src/backend/model/extension/ExtensionConfigWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export class ExtensionConfigWrapper {
const pc = ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass());
try {
await pc.load();
for (const ext of Object.values(ObjectManagers.getInstance().ExtensionManager.extObjects)) {
ext.config.loadToConfig(ConfigClassBuilder.attachPrivateInterface(pc));
if (ObjectManagers.isReady()) {
for (const ext of Object.values(ObjectManagers.getInstance().ExtensionManager.extObjects)) {
ext.config.loadToConfig(ConfigClassBuilder.attachPrivateInterface(pc));
}
}
} catch (e) {
console.error('Error during loading original config. Reverting to defaults.');
Expand Down
5 changes: 5 additions & 0 deletions src/backend/model/extension/ExtensionDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ export const ExtensionDecorator = <I extends [], O>(fn: (ee: IExtensionEvents) =
propertyName: string,
descriptor: PropertyDescriptor
) => {

const targetMethod = descriptor.value;
descriptor.value = async function(...args: I) {
if (!ObjectManagers.isReady()) {
return await targetMethod.apply(this, args);
}

const event = fn(ObjectManagers.getInstance().ExtensionManager.events) as ExtensionEvent<I, O>;
const eventObj = {stopPropagation: false};
const input = await event.triggerBefore({inputs: args}, eventObj);
Expand Down
6 changes: 6 additions & 0 deletions src/backend/model/extension/ExtensionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export class ExtensionManager implements IObjectManager {
public async init() {
this.extObjects = {};
this.initEvents();
if (!Config.Extensions.enabled) {
return;
}
this.router = express.Router();
Server.getInstance().app.use(ExtensionManager.EXTENSION_API_PATH, this.router);
this.loadExtensionsList();
Expand Down Expand Up @@ -117,6 +120,9 @@ export class ExtensionManager implements IObjectManager {


public async cleanUp() {
if (!Config.Extensions.enabled) {
return;
}
this.initEvents(); // reset events
await this.cleanUpExtensions();
Server.getInstance().app.use(ExtensionManager.EXTENSION_API_PATH, express.Router());
Expand Down
16 changes: 8 additions & 8 deletions src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Server {
static instance: Server = null;

public static getInstance(): Server {
if (this.instance === null) {
if (!this.instance) {
this.instance = new Server();
}
return this.instance;
Expand All @@ -54,11 +54,16 @@ export class Server {
this.init().catch(console.error);
}

get App(): any {
get Server(): HttpServer {
return this.server;
}

async init(): Promise<void> {

this.app = express();
LoggerRouter.route(this.app);
this.app.set('view engine', 'ejs');

Logger.info(LOG_TAG, 'running diagnostics...');
await ConfigDiagnostics.runDiagnostics();
Logger.verbose(
Expand All @@ -78,11 +83,6 @@ export class Server {
return v;
}, 2));

this.app = express();

LoggerRouter.route(this.app);

this.app.set('view engine', 'ejs');

/**
* Session above all
Expand Down Expand Up @@ -130,7 +130,7 @@ export class Server {
Localizations.init();

this.app.use(locale(Config.Server.languages, 'en'));
await ObjectManagers.InitManagers();
await ObjectManagers.getInstance().init();

Router.route(this.app);

Expand Down
3 changes: 2 additions & 1 deletion src/common/config/private/PrivateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../entities/job/JobScheduleDTO';
import {
ClientConfig,
ClientExtensionsConfig,
ClientGPXCompressingConfig,
ClientMediaConfig,
ClientMetaFileConfig,
Expand Down Expand Up @@ -1015,7 +1016,7 @@ export class ServerServiceConfig extends ClientServiceConfig {


@SubConfigClass<TAGS>({softReadonly: true})
export class ServerExtensionsConfig {
export class ServerExtensionsConfig extends ClientExtensionsConfig {
@ConfigProperty({volatile: true})
list: string[] = [];

Expand Down
19 changes: 19 additions & 0 deletions src/common/config/public/ClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,16 @@ export class ClientUserConfig {
unAuthenticatedUserRole: UserRoles = UserRoles.Admin;
}

@SubConfigClass({tags: {client: true}, softReadonly: true})
export class ClientExtensionsConfig {
@ConfigProperty({
tags: {
name: $localize`Enabled`,
priority: ConfigPriority.advanced,
}
})
enabled: boolean = true;
}

@SubConfigClass<TAGS>({tags: {client: true}, softReadonly: true})
export class ClientConfig {
Expand Down Expand Up @@ -1496,4 +1506,13 @@ export class ClientConfig {
description: $localize`This feature enables you to generate 'random photo' urls. That URL returns a photo random selected from your gallery. You can use the url with 3rd party application like random changing desktop background. Note: With the current implementation, random link also requires login.`
})
RandomPhoto: ClientRandomPhotoConfig = new ClientRandomPhotoConfig();


@ConfigProperty({
tags: {
name: $localize`Extensions`,
uiIcon: 'ionCloudOutline'
} as TAGS,
})
Extensions: ClientExtensionsConfig = new ClientExtensionsConfig();
}
Loading

0 comments on commit a323634

Please sign in to comment.