public override async Task <Stream> LoadAsync(CancellationToken cancellationToken)
        {
            var result = await _storageBlob.GetAsync(Options.ModelName, Options.ModelFileName, cancellationToken);

            if (result == null)
            {
                throw new ApplicationException("No Model was retrieved");
            }

            return(result);
        }
Exemple #2
0
        internal async Task RunAsync()
        {
            var sw = ValueStopwatch.StartNew();
            CancellationTokenSource?cancellation = null;

            try
            {
                cancellation = CancellationTokenSource.CreateLinkedTokenSource(_stopping.Token);
                cancellation.CancelAfter(TimeoutMilliseconds);

                var blob = await _storageBlob.GetBlobAsync(_modelName, _fileName, cancellation.Token);

                var etag = string.Empty;

                if (blob != null)
                {
                    await blob.FetchAttributesAsync();

                    etag = blob.Properties.ETag;
                }

                if (_eTag != etag)
                {
                    var stream = await _storageBlob.GetAsync(_modelName, _fileName, cancellation.Token);

                    if (stream != null)
                    {
                        var previousToken = Interlocked.Exchange(ref _reloadToken, new ModelReloadToken());

                        _model = _mlContext.Model.Load(stream, out _);

                        _logger.LogInformation("[{loader}][Reloaded] {modelName} Elapsed {time}ms", nameof(AzureStorageMSModelLoader), _modelName, sw.GetElapsedTime().TotalMilliseconds);

                        previousToken.OnReload();
                    }
                }
            }
            catch (OperationCanceledException) when(!_stopping.IsCancellationRequested)
            {
                // This is a cancellation - if the app is shutting down we want to ignore it.
            }
            catch (Exception ex)
            {
                _logger.LogError("Azure Storage Model Loader failed", ex);
            }
            finally
            {
                cancellation?.Dispose();
            }

            // schedule a polling task only if none exists and a valid delay is specified
            if (_pollingTask == null)
            {
                _pollingTask = PollForChangesAsync();
            }
        }