Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: memoize app getStatus calls #35426

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/apps-engine/src/server/AppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { IAppStorageItem } from './storage';
import { AppLogStorage, AppMetadataStorage } from './storage';
import { AppSourceStorage } from './storage/AppSourceStorage';
import { AppInstallationSource } from './storage/IAppStorageItem';
import { AppStatusCache } from './AppStatusCache';

export interface IAppInstallParameters {
enable: boolean;
Expand Down Expand Up @@ -101,6 +102,8 @@ export class AppManager {

private readonly runtime: AppRuntimeManager;

private readonly appStatusCache: AppStatusCache;

private isLoaded: boolean;

constructor({ metadataStorage, logStorage, bridges, sourceStorage }: IAppManagerDeps) {
Expand Down Expand Up @@ -137,6 +140,8 @@ export class AppManager {

this.parser = new AppPackageParser();
this.compiler = new AppCompiler();
this.appStatusCache = new AppStatusCache();

this.accessorManager = new AppAccessorManager(this);
this.listenerManager = new AppListenerManager(this);
this.commandManager = new AppSlashCommandManager(this);
Expand Down Expand Up @@ -233,6 +238,10 @@ export class AppManager {
return this.runtime;
}

public getAppStatusCache(): AppStatusCache {
return this.appStatusCache;
}

/** Gets whether the Apps have been loaded or not. */
public areAppsLoaded(): boolean {
return this.isLoaded;
Expand Down
40 changes: 40 additions & 0 deletions packages/apps-engine/src/server/AppStatusCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { AppStatus } from '../definition/AppStatus';

export class AppStatusCache {
private readonly cache: Record<string, { status: AppStatus, expiresAt: Date }> = {};
private readonly CACHE_TTL = 1000 * 60;

// We clean up the cache every 5 minutes
private readonly CLEANUP_INTERVAL = 1000 * 60 * 5;
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give them environment variables so we can configure from outside?


public constructor() {
setInterval(() => this.clearExpired(), this.CLEANUP_INTERVAL);
}

public set(appId: string, status: AppStatus): void {
this.cache[appId] = {
status,
expiresAt: new Date(Date.now() + this.CACHE_TTL),
};
}

public get(appId: string): AppStatus | undefined {
const cache = this.cache[appId];

if (cache && cache.expiresAt > new Date()) {
return cache.status;
}

return undefined;
}

private clearExpired(): void {
const now = new Date();

for (const appId in this.cache) {
if (this.cache[appId].expiresAt < now) {
delete this.cache[appId];
}
}
}
}
Comment on lines +31 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this and the timer is unnecessary.

if cached item expired, you are setting a fresh one anyway and returning undefined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I forgot to take into account that a workspace cannot have that many apps such that this would be required

2 changes: 1 addition & 1 deletion packages/apps-engine/src/server/ProxiedApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ProxiedApp {

public async setStatus(status: AppStatus, silent?: boolean): Promise<void> {
await this.call(AppMethod.SETSTATUS, status);

this.manager.getAppStatusCache().set(this.getID(), status);
if (!silent) {
await this.manager.getBridges().getAppActivationBridge().doAppStatusChanged(this, status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { IParseAppPackageResult } from '../../compiler';
import { AppConsole, type ILoggerStorageEntry } from '../../logging';
import type { AppAccessorManager, AppApiManager } from '../../managers';
import type { AppLogStorage, IAppStorageItem } from '../../storage';
import { AppStatusCache } from '../../AppStatusCache';

const baseDebug = debugFactory('appsEngine:runtime:deno');

Expand Down Expand Up @@ -110,6 +111,8 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
private readonly messenger: ProcessMessenger;

private readonly livenessManager: LivenessManager;

private readonly appStatusCache: AppStatusCache;

// We need to keep the appSource around in case the Deno process needs to be restarted
constructor(
Expand All @@ -133,6 +136,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
this.api = manager.getApiManager();
this.logStorage = manager.getLogStorage();
this.bridges = manager.getBridges();
this.appStatusCache = manager.getAppStatusCache();
}

public spawnProcess(): void {
Expand Down Expand Up @@ -241,7 +245,16 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
return AppStatus.UNKNOWN;
}

return this.sendRequest({ method: 'app:getStatus', params: [] }) as Promise<AppStatus>;
const statusCache = this.appStatusCache.get(this.getAppId());
if (!statusCache) {
this.debug(`${this.getAppId()} status not found in cache, fetching from subprocess`);
const status = await this.sendRequest({ method: 'app:getStatus', params: [] }) as AppStatus;
this.appStatusCache.set(this.getAppId(), status);
return status;
}

this.debug(`${this.getAppId()} status found in cache: ${statusCache}`);
return statusCache;
}

public async setupApp() {
Expand Down
4 changes: 4 additions & 0 deletions packages/apps-engine/tests/test-data/TestAppStatusCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AppStatusCache } from '../../src/server/AppStatusCache';

export class TestAppStatusCache extends AppStatusCache {
}
4 changes: 4 additions & 0 deletions packages/apps-engine/tests/test-data/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { AppRuntimeManager } from '../../src/server/managers/AppRuntimeMana
import type { UIActionButtonManager } from '../../src/server/managers/UIActionButtonManager';
import type { DenoRuntimeSubprocessController } from '../../src/server/runtime/deno/AppsEngineDenoRuntime';
import type { AppLogStorage, AppMetadataStorage, AppSourceStorage, IAppStorageItem } from '../../src/server/storage';
import { TestAppStatusCache } from './TestAppStatusCache';

export class TestInfastructureSetup {
private appStorage: TestsAppStorage;
Expand Down Expand Up @@ -103,6 +104,9 @@ export class TestInfastructureSetup {
getRuntime: () => {
return this.runtimeManager;
},
getAppStatusCache() {
return new TestAppStatusCache();
},
} as unknown as AppManager;
}

Expand Down
Loading