快速版:为什么我的功能主机在 5 分钟后没有做任何事情就自动杀死它?
我有一个 Azure 函数,它使用 nClam 扫描 blob 文件中的病毒。它似乎工作得很好,但突然间它会在触发任何 blob 之前自行杀死它!它会在 5 分钟后以 OutOfMemoryException 关闭:
[18/9/2019 10:33:33] Host initialized (405ms)
[18/9/2019 10:33:33] Host started (812ms)
[18/9/2019 10:33:33] Job host started
Hosting environment: Production
Content root path: D:\git\TopoAPI\Antivirus\bin\Debug\netcoreapp2.1
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
[18/9/2019 10:33:38] Host lock lease acquired by instance ID '000000000000000000000000C913FBA0'.
[18/9/2019 10:38:46] An unhandled exception has occurred. Host is shutting down.
[18/9/2019 10:38:46] Microsoft.WindowsAzure.Storage: Exception of type 'System.OutOfMemoryException' was thrown. System.Private.CoreLib: Exception of type 'System.OutOfMemoryException' was thrown.
[18/9/2019 10:38:46] Stopping host...
[18/9/2019 10:38:46] Stopping JobHost
[18/9/2019 10:38:46] Job host stopped
[18/9/2019 10:38:46] Host shutdown completed.
Application is shutting down...
下面是我的扫描 blob 触发功能:
[FunctionName("scanImports")]
public static async Task Scan([BlobTrigger("imports/{newBlobName}", Connection = "BlobConnectionstring")]CloudBlockBlob newBlob, string newBlobName, ILogger log, ExecutionContext context)
{
var config = new ConfigurationBuilder().SetBasePath(context.FunctionAppDirectory).AddJsonFile("local.settings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();
var clamClient = new ClamClient(config["ContainerAddress"], int.Parse(config["ContainerPort"]));
var blobFileStream = await newBlob.OpenReadAsync();
using (var memoryStream = new MemoryStream())
{
await blobFileStream.CopyToAsync(memoryStream);
var result = await clamClient.SendAndScanFileAsync(memoryStream.ToArray());
bool isClean = result.InfectedFiles == null || result.InfectedFiles.Count == 0;
// Check if newBlob is infected. If infected, move to quarantineBlob
if (!isClean)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(config["blobConnectionstring"]);
CloudBlobClient client;
CloudBlobContainer container;
CloudBlockBlob quarantineBlob;
client = storageAccount.CreateCloudBlobClient();
container = client.GetContainerReference(config["QuarantineBlobName"]);
await container.CreateIfNotExistsAsync();
quarantineBlob = container.GetBlockBlobReference(newBlobName);
quarantineBlob.Properties.ContentType = newBlob.Properties.ContentType;
await quarantineBlob.UploadFromStreamAsync(memoryStream);
await newBlob.DeleteAsync();
}
}
}
更新 1:主机在 5 分钟后因 OutOfMemoryException 而死亡。我试图延长功能超时,这没有什么区别。在此期间,该进程将持续使用 5-8% 的 cpu,并且在它死亡之前,该进程将使用超过 1500 MB 的内存。
更新 2:如果我从函数中删除所有代码,只留下一个 log.info() 语句,主机仍然会在 5 分钟后以 OutOfMemoryException 自行杀死它