public bool StartAssignment(HostAssignmentContext context) { if (!_webHostEnvironment.InStandbyMode) { // This is only true when specializing pinned containers. if (!context.Environment.TryGetValue(EnvironmentSettingNames.ContainerStartContext, out string startContext)) { _logger.LogError("Assign called while host is not in placeholder mode and start context is not present."); return(false); } } if (_environment.IsContainerReady()) { _logger.LogError("Assign called while container is marked as specialized."); return(false); } if (context.IsWarmupRequest) { // Based on profiling download code jit-ing holds up cold start. // Pre-jit to avoid paying the cost later. Task.Run(async() => await _packageDownloadHandler.Download(context.GetRunFromPkgContext())); return(true); } else if (_assignmentContext == null) { lock (_assignmentLock) { if (_assignmentContext != null) { return(_assignmentContext.Equals(context)); } _assignmentContext = context; } _logger.LogInformation($"Starting Assignment. Cloud Name: {_environment.GetCloudName()}"); // set a flag which will cause any incoming http requests to buffer // until specialization is complete // the host is guaranteed not to receive any requests until AFTER assign // has been initiated, so setting this flag here is sufficient to ensure // that any subsequent incoming requests while the assign is in progress // will be delayed until complete _webHostEnvironment.DelayRequests(); // start the specialization process in the background Task.Run(async() => await Assign(context)); return(true); } else { // No lock needed here since _assignmentContext is not null when we are here return(_assignmentContext.Equals(context)); } }
public async Task <bool> ApplyBlobPackageContext(RunFromPackageContext pkgContext, string targetPath, bool azureFilesMounted, bool throwOnFailure = true) { try { // If Azure Files are mounted, /home will point to a shared remote dir // So extracting to /home/site/wwwroot can interfere with other running instances // Instead extract to localSitePackagesPath and bind to /home/site/wwwroot // home will continue to point to azure file share var localSitePackagesPath = azureFilesMounted ? _environment.GetEnvironmentVariableOrDefault(EnvironmentSettingNames.LocalSitePackages, EnvironmentSettingNames.DefaultLocalSitePackagesPath) : string.Empty; // download zip and extract var filePath = await _packageDownloadHandler.Download(pkgContext); await UnpackPackage(filePath, targetPath, pkgContext, localSitePackagesPath); string bundlePath = Path.Combine(targetPath, "worker-bundle"); if (Directory.Exists(bundlePath)) { _logger.LogInformation("Python worker bundle detected"); } return(true); } catch (Exception e) { _logger.LogDebug(e, nameof(ApplyBlobPackageContext)); if (throwOnFailure) { throw; } return(false); } }