| | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | 2 | | // |
| | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | 4 | | // you may not use this file except in compliance with the License. |
| | 5 | | // You may obtain a copy of the License at |
| | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | 7 | | // |
| | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 11 | | // |
| | 12 | | // See the License for the specific language governing permissions and |
| | 13 | | // limitations under the License. |
| | 14 | |
|
| | 15 | | using System; |
| | 16 | | using System.Collections.Generic; |
| | 17 | | using System.Linq; |
| | 18 | | using System.Text; |
| | 19 | | using System.Threading; |
| | 20 | | using System.Threading.Tasks; |
| | 21 | | using Microsoft.WindowsAzure.Storage; |
| | 22 | | using Microsoft.WindowsAzure.Storage.Blob; |
| | 23 | | using System.IO; |
| | 24 | |
|
| | 25 | | namespace Microsoft.Azure.Batch.Conventions.Files |
| | 26 | | { |
| | 27 | | internal sealed class TrackedFile : ITrackedSaveOperation |
| | 28 | | { |
| 1 | 29 | | public static readonly TimeSpan DefaultFlushInterval = TimeSpan.FromMinutes(1); |
| | 30 | |
|
| | 31 | | private readonly Timer _timer; |
| | 32 | | private readonly CloudAppendBlob _blob; |
| | 33 | | private readonly string _filePath; |
| | 34 | | private long _flushPointer = 0; |
| 0 | 35 | | private readonly object _lock = new object(); |
| | 36 | |
|
| 0 | 37 | | public TrackedFile(string filePath, CloudAppendBlob blob, TimeSpan interval) |
| | 38 | | { |
| 0 | 39 | | _filePath = filePath; |
| 0 | 40 | | _blob = blob; |
| 0 | 41 | | _timer = new Timer(OnTimer, null, TimeSpan.FromMilliseconds(1), interval); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void OnTimer(object state) |
| | 45 | | { |
| 0 | 46 | | Flush(FlushMode.IfIdle); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | private void Flush(FlushMode flushMode) |
| | 50 | | { |
| | 51 | | // If this is the forced flush on Dispose, wait until we acquire the lock. Otherwise, |
| | 52 | | // just check to see if the lock is available, and if not, we are still processing the |
| | 53 | | // last tranche of appends, so bail out and wait for the next flush interval. |
| 0 | 54 | | var lockTimeout = (flushMode == FlushMode.IfIdle ? TimeSpan.Zero : Timeout.InfiniteTimeSpan); |
| 0 | 55 | | bool acquiredLock = false; |
| 0 | 56 | | Monitor.TryEnter(_lock, lockTimeout, ref acquiredLock); |
| | 57 | |
|
| 0 | 58 | | if (!acquiredLock) |
| | 59 | | { |
| 0 | 60 | | return; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | try |
| | 64 | | { |
| 0 | 65 | | var file = new FileInfo(_filePath); |
| | 66 | |
|
| 0 | 67 | | if (!file.Exists) |
| | 68 | | { |
| 0 | 69 | | return; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | var uploadPointer = file.Length; |
| | 73 | |
|
| 0 | 74 | | if (uploadPointer <= _flushPointer) |
| | 75 | | { |
| 0 | 76 | | return; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | using (var stm = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| | 80 | | { |
| 0 | 81 | | stm.Seek(_flushPointer, SeekOrigin.Begin); |
| 0 | 82 | | _blob.AppendFromStreamAsync(stm, uploadPointer - _flushPointer).GetAwaiter().GetResult(); |
| 0 | 83 | | _flushPointer = uploadPointer; |
| 0 | 84 | | } |
| 0 | 85 | | } |
| 0 | 86 | | catch (Exception ex) |
| | 87 | | { |
| 0 | 88 | | if (flushMode == FlushMode.IfIdle) |
| | 89 | | { |
| 0 | 90 | | OnFlushError(ex); |
| | 91 | | } |
| | 92 | | else |
| | 93 | | { |
| 0 | 94 | | throw; |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | | finally |
| | 98 | | { |
| 0 | 99 | | Monitor.Exit(_lock); |
| 0 | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private enum FlushMode |
| | 104 | | { |
| | 105 | | IfIdle, |
| | 106 | | Force, |
| | 107 | | } |
| | 108 | |
|
| | 109 | | public event EventHandler<Exception> FlushError; |
| | 110 | |
|
| | 111 | | private void OnFlushError(Exception exception) |
| | 112 | | { |
| 0 | 113 | | var handler = FlushError; |
| 0 | 114 | | if (handler != null) |
| | 115 | | { |
| 0 | 116 | | handler(this, exception); |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | public void Dispose() |
| | 121 | | { |
| 0 | 122 | | _timer.Change(Timeout.Infinite, Timeout.Infinite); |
| 0 | 123 | | _timer.Dispose(); |
| | 124 | |
|
| 0 | 125 | | Flush(FlushMode.Force); |
| 0 | 126 | | } |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|