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

Adding package.json warning when using AzureFiles #501

Merged
merged 2 commits into from
Dec 17, 2021
Merged
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
31 changes: 31 additions & 0 deletions src/WorkerChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

import { Context } from '@azure/functions';
import { access, constants } from 'fs';
import * as path from 'path';
import { format, isFunction } from 'util';
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
import { CreateContextAndInputs, LogCallback, ResultCallback } from './Context';
Expand Down Expand Up @@ -129,6 +131,8 @@ export class WorkerChannel implements IWorkerChannel {
throw new InternalException(msg);
}

this.logColdStartWarning();

const workerCapabilities = {
RpcHttpTriggerMetadataRemoved: 'true',
RpcHttpBodyOnly: 'true',
Expand Down Expand Up @@ -407,4 +411,31 @@ export class WorkerChannel implements IWorkerChannel {
after(context);
}
}

private logColdStartWarning(delayInMs?: number): void {
// On reading a js file with function code('require') NodeJs tries to find 'package.json' all the way up to the file system root.
// In Azure files it causes a delay during cold start as connection to Azure Files is an expensive operation.
if (
process.env.WEBSITE_CONTENTAZUREFILECONNECTIONSTRING &&
process.env.WEBSITE_CONTENTSHARE &&
process.env.AzureWebJobsScriptRoot
) {
// Add delay to avoid affecting coldstart
if (!delayInMs) {
delayInMs = 5000;
}
setTimeout(() => {
access(path.join(process.env.AzureWebJobsScriptRoot!, 'package.json'), constants.F_OK, (e) => {
if (e) {
this.log({
message:
'package.json is not found at the root of the Function App in Azure Files - cold start for NodeJs can be affected.',
level: LogLevel.Debug,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be LogLevel.Warning?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's leave it as LogLevel. Debug as we do not want to scary customers with sudden unknown warning.

logCategory: LogCategory.System,
});
}
});
}, delayInMs);
}
}
}
22 changes: 22 additions & 0 deletions test/WorkerChannelTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { FunctionInfo } from '../src/FunctionInfo';
import { FunctionLoader } from '../src/FunctionLoader';
import { WorkerChannel } from '../src/WorkerChannel';
import { TestEventStream } from './TestEventStream';
import LogCategory = rpc.RpcLog.RpcLogCategory;
import LogLevel = rpc.RpcLog.Level;

describe('WorkerChannel', () => {
let channel: WorkerChannel;
Expand Down Expand Up @@ -662,5 +664,25 @@ describe('WorkerChannel', () => {
};
assertInvocationSuccess(expectedOutput, expectedReturnValue);
});

it('logs AzureFiles cold start warning', async () => {
process.env.WEBSITE_CONTENTAZUREFILECONNECTIONSTRING = 'test';
process.env.WEBSITE_CONTENTSHARE = 'test';
process.env.AzureWebJobsScriptRoot = 'test';

// Accesing private method
(channel as any).logColdStartWarning(100);

// Set slight delay
await new Promise((resolve) => setTimeout(resolve, 200));
sinon.assert.calledWith(stream.written, <rpc.IStreamingMessage>{
rpcLog: {
message:
'package.json is not found at the root of the Function App in Azure Files - cold start for NodeJs can be affected.',
level: LogLevel.Debug,
logCategory: LogCategory.System,
},
});
});
});
});