@@ -41,10 +41,10 @@ internal class CachingTransport : ITransport, IAsyncDisposable, IDisposable
41
41
// Inner transport exposed internally primarily for testing
42
42
internal ITransport InnerTransport => _innerTransport ;
43
43
44
- public static CachingTransport Create ( ITransport innerTransport , SentryOptions options )
44
+ public static CachingTransport Create ( ITransport innerTransport , SentryOptions options , bool startWorker = true )
45
45
{
46
46
var transport = new CachingTransport ( innerTransport , options ) ;
47
- transport . Initialize ( ) ;
47
+ transport . Initialize ( startWorker ) ;
48
48
return transport ;
49
49
}
50
50
@@ -64,38 +64,21 @@ private CachingTransport(ITransport innerTransport, SentryOptions options)
64
64
_processingDirectoryPath = Path . Combine ( _isolatedCacheDirectoryPath , "__processing" ) ;
65
65
}
66
66
67
- private void Initialize ( )
67
+ private void Initialize ( bool startWorker )
68
68
{
69
+ // Restore any abandoned files from a previous session
70
+ MoveUnprocessedFilesBackToCache ( ) ;
71
+
72
+ // Ensure directories exist
69
73
Directory . CreateDirectory ( _isolatedCacheDirectoryPath ) ;
70
74
Directory . CreateDirectory ( _processingDirectoryPath ) ;
71
75
72
- _worker = Task . Run ( CachedTransportBackgroundTaskAsync ) ;
76
+ // Start a worker, if one is needed
77
+ _worker = startWorker ? Task . Run ( CachedTransportBackgroundTaskAsync ) : Task . CompletedTask ;
73
78
}
74
79
75
80
private async Task CachedTransportBackgroundTaskAsync ( )
76
81
{
77
- try
78
- {
79
- // Processing directory may already contain some files left from previous session
80
- // if the worker has been terminated unexpectedly.
81
- // Move everything from that directory back to cache directory.
82
- if ( Directory . Exists ( _processingDirectoryPath ) )
83
- {
84
- foreach ( var filePath in Directory . EnumerateFiles ( _processingDirectoryPath ) )
85
- {
86
- var destinationPath = Path . Combine ( _isolatedCacheDirectoryPath , Path . GetFileName ( filePath ) ) ;
87
- _options . LogDebug ( "Moving unprocessed file back to cache: {0} to {1}." ,
88
- filePath , destinationPath ) ;
89
-
90
- File . Move ( filePath , destinationPath ) ;
91
- }
92
- }
93
- }
94
- catch ( Exception e )
95
- {
96
- _options . LogError ( "Failed to move unprocessed files back to cache." , e ) ;
97
- }
98
-
99
82
while ( ! _workerCts . IsCancellationRequested )
100
83
{
101
84
try
@@ -130,6 +113,34 @@ private async Task CachedTransportBackgroundTaskAsync()
130
113
_options . LogDebug ( "Background worker of CachingTransport has shutdown." ) ;
131
114
}
132
115
116
+ private void MoveUnprocessedFilesBackToCache ( )
117
+ {
118
+ // Processing directory may already contain some files left from previous session
119
+ // if the cache was working when the process terminated unexpectedly.
120
+ // Move everything from that directory back to cache directory.
121
+
122
+ if ( ! Directory . Exists ( _processingDirectoryPath ) )
123
+ {
124
+ // nothing to do
125
+ return ;
126
+ }
127
+
128
+ foreach ( var filePath in Directory . EnumerateFiles ( _processingDirectoryPath ) )
129
+ {
130
+ try
131
+ {
132
+ var destinationPath = Path . Combine ( _isolatedCacheDirectoryPath , Path . GetFileName ( filePath ) ) ;
133
+ _options . LogDebug ( "Moving unprocessed file back to cache: {0} to {1}." ,
134
+ filePath , destinationPath ) ;
135
+ File . Move ( filePath , destinationPath ) ;
136
+ }
137
+ catch ( Exception e )
138
+ {
139
+ _options . LogError ( "Failed to move unprocessed file back to cache: {0}" , e , filePath ) ;
140
+ }
141
+ }
142
+ }
143
+
133
144
private void EnsureFreeSpaceInCache ( )
134
145
{
135
146
// Trim files, leaving only (X - 1) of the newest ones.
@@ -206,6 +217,13 @@ private async Task InnerProcessCacheAsync(string file, CancellationToken cancell
206
217
// Let the worker catch, log, wait a bit and retry.
207
218
throw ;
208
219
}
220
+
221
+ if ( ex . Source == "FakeFailingTransport" )
222
+ {
223
+ // HACK: Deliberately sent from unit tests to avoid deleting the file from processing
224
+ return ;
225
+ }
226
+
209
227
LogFailureWithDiscard ( file , ex ) ;
210
228
}
211
229
}
0 commit comments