Skip to content

Commit

Permalink
Add logging to detect if function app dir doesn't change (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Sep 10, 2024
1 parent a5ed77c commit 48d9914
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface LegacyRegisteredFunction extends RegisteredFunction {
}

export class AppContext {
functionAppDirectory: string | null | undefined;
constructor(functionAppDirectory: string | null | undefined) {
this.functionAppDirectory = functionAppDirectory;
}
packageJson: PackageJson = {};
/**
* this hook data will be passed to (and set by) all hooks in all scopes
Expand Down
6 changes: 3 additions & 3 deletions src/WorkerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IEventStream } from './GrpcClient';
import { InvocationLogContext, LogHookContext } from './hooks/LogHookContext';

class WorkerContext {
app = new AppContext();
app = new AppContext(undefined);
defaultProgrammingModel?: ProgrammingModel;

/**
Expand Down Expand Up @@ -54,8 +54,8 @@ class WorkerContext {
}
}

resetApp(): void {
this.app = new AppContext();
resetApp(functionAppDirectory: string | null | undefined): void {
this.app = new AppContext(functionAppDirectory);
this.app.programmingModel = this.defaultProgrammingModel;
}

Expand Down
27 changes: 26 additions & 1 deletion src/eventHandlers/FunctionEnvironmentReloadHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getWorkerMetadata } from './getWorkerMetadata';
import LogCategory = rpc.RpcLog.RpcLogCategory;
import LogLevel = rpc.RpcLog.Level;
import CapabilitiesUpdateStrategy = rpc.FunctionEnvironmentReloadResponse.CapabilitiesUpdateStrategy;
import * as path from 'path';

/**
* Environment variables from the current process
Expand All @@ -27,7 +28,27 @@ export class FunctionEnvironmentReloadHandler extends EventHandler<
}

async handleEvent(msg: rpc.IFunctionEnvironmentReloadRequest): Promise<rpc.IFunctionEnvironmentReloadResponse> {
worker.resetApp();
if (!msg.functionAppDirectory) {
worker.log({
message: `FunctionEnvironmentReload functionAppDirectory is not defined`,
level: LogLevel.Debug,
logCategory: LogCategory.System,
});
}

if (
worker.app.functionAppDirectory &&
msg.functionAppDirectory &&
isPathEqual(worker.app.functionAppDirectory, msg.functionAppDirectory)
) {
worker.log({
message: `FunctionEnvironmentReload functionAppDirectory has not changed`,
level: LogLevel.Debug,
logCategory: LogCategory.System,
});
}

worker.resetApp(msg.functionAppDirectory);

const response = this.getDefaultResponse(msg);

Expand Down Expand Up @@ -63,3 +84,7 @@ export class FunctionEnvironmentReloadHandler extends EventHandler<
return response;
}
}

function isPathEqual(path1: string, path2: string): boolean {
return path.relative(path1, path2) === '';
}
9 changes: 9 additions & 0 deletions src/eventHandlers/WorkerInitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export class WorkerInitHandler extends EventHandler<'workerInitRequest', 'worker
}

async handleEvent(msg: rpc.IWorkerInitRequest): Promise<rpc.IWorkerInitResponse> {
if (!msg.functionAppDirectory) {
worker.log({
message: `WorkerInit functionAppDirectory is not defined`,
level: LogLevel.Debug,
logCategory: LogCategory.System,
});
}
worker.app.functionAppDirectory = msg.functionAppDirectory;

const response = this.getDefaultResponse(msg);

worker.log({
Expand Down
37 changes: 31 additions & 6 deletions test/eventHandlers/FunctionEnvironmentReloadHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(2),
msg.envReload.response
);
expect(process.env.hello).to.equal('world');
expect(process.env.SystemDrive).to.equal('Q:');
expect(process.env.PlaceholderVariable).to.be.undefined;
Expand All @@ -56,7 +60,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(2),
msg.envReload.response
);
expect(process.env.hello).to.equal('world');
expect(process.env.SystemDrive).to.equal('Q:');
expect(process.env.PlaceholderVariable).to.be.undefined;
Expand All @@ -80,7 +88,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(0),
msg.envReload.response
);
expect(process.env).to.be.empty;
});

Expand All @@ -92,7 +104,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(0),
msg.envReload.response
);

stream.addTestMessage({
requestId: 'testReqId',
Expand All @@ -108,7 +124,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(0), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(0),
msg.envReload.response
);
});

it('reloads environment variable and keeps cwd without functionAppDirectory', async () => {
Expand All @@ -123,7 +143,11 @@ describe('FunctionEnvironmentReloadHandler', () => {
functionAppDirectory: null,
},
});
await stream.assertCalledWith(msg.envReload.reloadEnvVarsLog(2), msg.envReload.response);
await stream.assertCalledWith(
msg.envReload.funcAppDirNotDefined,
msg.envReload.reloadEnvVarsLog(2),
msg.envReload.response
);
expect(process.env.hello).to.equal('world');
expect(process.env.SystemDrive).to.equal('Q:');
expect(process.cwd() == cwd);
Expand Down Expand Up @@ -181,6 +205,7 @@ describe('FunctionEnvironmentReloadHandler', () => {
},
});
await stream.assertCalledWith(
msg.envReload.funcAppDirNotChanged,
msg.envReload.reloadEnvVarsLog(0),
msg.envReload.changingCwdLog(testAppPath),
msg.envReload.response
Expand Down
2 changes: 1 addition & 1 deletion test/eventHandlers/TestEventStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class TestEventStream extends EventEmitter implements IEventStream {
await fs.writeFile(testPackageJsonPath, '{}');

worker._hostVersion = undefined;
worker.resetApp();
worker.resetApp(this.originalCwd);

// minor delay so that it's more likely extraneous messages are associated with this test as opposed to leaking into the next test
await new Promise((resolve) => setTimeout(resolve, 20));
Expand Down
8 changes: 8 additions & 0 deletions test/eventHandlers/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export namespace msg {
return msg.infoLog(`Changing current working directory to ${dir}`);
}

export const funcAppDirNotDefined = msg.debugLog(
'FunctionEnvironmentReload functionAppDirectory is not defined'
);

export const funcAppDirNotChanged = msg.debugLog(
'FunctionEnvironmentReload functionAppDirectory has not changed'
);

export const response = new RegExpStreamingMessage(
{
requestId: 'testReqId',
Expand Down

0 comments on commit 48d9914

Please sign in to comment.