如何避免Azure Function因下载ffmpeg至node_modules而重启
问题描述
Azure Function运行时会下载ffmpeg可执行文件到node_modules/.ffmpeg目录,该操作触发目录变更检测,导致Function主机强制重启。相关日志片段如下:
2022-11-29T16:01:13.397 [Information] Executing 'Functions.Render' (Reason='(null)', Id=fb038d89-051c-4adb-bc32-009e4620a6df) 2022-11-29T16:01:16.013 [Information] JavaScript activity function started. 2022-11-29T16:01:40.510 [Information] Host Status: {"id": "render-durable","state": "Running","version": "3.17.0.0","versionDetails": "3.17.0 Commit hash: a2c47e3bd045f6b4eec138e144fa830368ed9426","platformVersion": "99.0.7.639","instanceId": "5e5795336a674e1791e4c4a118daee66ab744eb64085f877d49cee0c12f9c15f","computerName": "10-30-0-31","processUptime": 59974,"functionAppContentEditingState": "Unknown","extensionBundle": {"id": "Microsoft.Azure.Functions.ExtensionBundle","version": "3.17.0"}} 2022-11-29T16:01:41.161 [Information] Watched directory change of type 'Created' detected for 'C:\home\site\wwwroot\node_modules\.ffmpeg\ffmpeg-25161681083400045.exe' 2022-11-29T16:01:41.162 [Information] Host configuration has changed. Signaling shutdown 2022-11-29T16:01:42.080 [Information] Downloading ffmpeg 0.8 Mb/121.6 Mb 2022-11-29T16:01:43.085 [Information] Downloading ffmpeg 1.9 Mb/121.6 Mb 2022-11-29T16:01:44.103 [Information] Downloading ffmpeg 2.8 Mb/121.6 Mb 2022-11-29T16:01:45.125 [Information] Downloading ffmpeg 3.8 Mb/121.6 Mb 2022-11-29T16:01:46.139 [Information] Downloading ffmpeg 4.9 Mb/121.6 Mb 2022-11-29T16:01:47.151 [Information] Downloading ffmpeg 5.8 Mb/121.6 Mb 2022-11-29T16:01:48.167 [Information] Downloading ffmpeg 6.8 Mb/121.6 Mb 2022-11-29T16:01:49.197 [Information] Downloading ffmpeg 7.9 Mb/121.6 Mb 2022-11-29T16:01:50.219 [Information] Downloading ffmpeg 8.9 Mb/121.6 Mb 2022-11-29T16:01:51.227 [Information] Downloading ffmpeg 9.9 Mb/121.6 Mb 2022-11-29T16:01:51.693 [Error] at Object.module.exports.exports.createStatusError (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:980:15) 2022-11-29T16:01:51.762 [Information] at ClientDuplexStream._emitStatusIfDone (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:7690:26) 2022-11-29T16:01:51.762 [Information] at ClientDuplexStream._receiveStatus (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:7668:8) 2022-11-29T16:01:51.763 [Information] at Object.onReceiveStatus (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:22262:15) 2022-11-29T16:01:51.763 [Information] at InterceptingListener.module.exports.InterceptingListener._callNext (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:21519:42) 2022-11-29T16:01:51.763 [Information] at InterceptingListener.module.exports.InterceptingListener.onReceiveStatus (C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:21569:8) 2022-11-29T16:01:51.763 [Information] at C:\Program Files (x86)\SiteExtensions\Functions\3.17.0\workers\node\dist\src\worker-bundle.js:22078:18 { 2022-11-29T16:01:51.763 [Information] code: 14, 2022-11-29T16:01:51.764 [Information] metadata: Metadata { _internal_repr: {}, flags: 0 }, 2022-11-29T16:01:51.764 [Information] details: 'GOAWAY received' 2022-11-29T16:01:51.764 [Information] }
解决方案
1. 更改ffmpeg下载路径到非监控目录
Azure Functions默认监控wwwroot目录下的文件变化,检测到变更就会重启主机。可将ffmpeg下载到不被监控的目录:
- 临时目录:代码中指定路径为系统临时目录,示例:
const ffmpegPath = path.join(process.env.TEMP, '.ffmpeg', 'ffmpeg.exe'); - 专用工具目录:使用
D:\home\site\tools(Windows)或/home/site/tools(Linux),该目录不会被函数主机监控。
2. 配置函数应用忽略指定目录
在Azure门户的函数应用配置 > 应用程序设置中添加环境变量FUNCTIONS_WATCHER_EXCLUDE,值设为node_modules/.ffmpeg,主机将忽略该目录的变化,不会触发重启。
3. 提前打包ffmpeg到部署包
避免运行时下载,直接将ffmpeg包含在部署包中:
- 使用自带ffmpeg的npm包(如
ffmpeg-static),安装时将ffmpeg下载到本地,部署时一同上传至Azure; - 手动将ffmpeg文件放到项目目录(如
./tools/ffmpeg),代码中直接引用该路径,部署时包含此文件。
内容的提问来源于stack exchange,提问作者ulu
相关产品推荐
相关产品推荐

