Exemplo n.º 1
0
        public async Task <TaskOutputPage> GetLastPageAsync(int jobId, string taskResultKey, [FromQuery] int pageSize, CancellationToken token)
        {
            var result = new TaskOutputPage()
            {
                Offset = 0, Size = 0
            };
            var blob = this.utilities.GetTaskOutputBlob(jobId, taskResultKey);

            if (pageSize > 1024 || !await blob.ExistsAsync(null, null, token))
            {
                return(result);
            }

            if (pageSize == 0)
            {
                pageSize = 1000;
            }

            await blob.FetchAttributesAsync(null, null, null, token);

            if (blob.Properties.Length == 0)
            {
                return(result);
            }

            result.Offset = blob.Properties.Length - pageSize;
            if (result.Offset < 0)
            {
                result.Offset = 0;
            }

            using (MemoryStream stream = new MemoryStream(pageSize))
            {
                await blob.DownloadRangeToStreamAsync(stream, result.Offset, pageSize, null, null, null, token);

                stream.Seek(0, SeekOrigin.Begin);
                StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                result.Content = await sr.ReadToEndAsync();

                result.Size = stream.Position;
            }

            return(result);
        }
Exemplo n.º 2
0
        public async T.Task <TaskOutputPage> GetOutputPageAsync(JobType type, string taskResultKey, int pageSize, long offset, CancellationToken token)
        {
            if (pageSize <= 0)
            {
                pageSize = MaxPageSize;
            }
            if (pageSize > MaxPageSize)
            {
                pageSize = MaxPageSize;
            }

            var result = new TaskOutputPage()
            {
                Offset = offset, Size = 0
            };

            var blob = this.Utilities.GetJobOutputBlob(type, taskResultKey);

            if (!await blob.ExistsAsync(null, null, token))
            {
                return(result);
            }

            await blob.FetchAttributesAsync();

            result.Eof = blob.Metadata.TryGetValue(TaskOutputPage.EofMark, out string value) && Boolean.TryParse(value, out bool eof) && eof;

            var blobLength = blob.Properties.Length;

            if (blobLength == 0)
            {
                return(result);
            }

            if (offset < 0)
            {
                offset += blobLength;
                if (offset < 0)
                {
                    offset = 0;
                }
            }

            result.Offset = offset;

            if (blobLength <= offset)
            {
                return(result);
            }

            using (MemoryStream stream = new MemoryStream(pageSize))
            {
                await blob.DownloadRangeToStreamAsync(stream, offset, pageSize, null, null, null, token);

                stream.Seek(0, SeekOrigin.Begin);
                StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                result.Content = await sr.ReadToEndAsync();

                result.Size = stream.Position;
            }

            result.Eof = result.Eof && (result.Size + result.Offset >= blobLength);

            return(result);
        }