File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed
test/TestFunctionApps/TestCustomHandlerProject Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE in the project root for license information.
3
+
4
+ namespace Azure . Functions . Cli . Helpers
5
+ {
6
+ public static class FileLockHelper
7
+ {
8
+ private const string DefaultLockFileName = "func.lock" ;
9
+ private static readonly string _templatesLockFilePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".azurefunctions" ) ;
10
+
11
+ public static async Task < T > WithFileLockAsync < T > ( string lockFileName , Func < Task < T > > action )
12
+ {
13
+ lockFileName ??= DefaultLockFileName ;
14
+ var lockFile = Path . Combine ( _templatesLockFilePath , lockFileName ) ;
15
+
16
+ for ( int attempt = 0 ; attempt < 20 ; attempt ++ )
17
+ {
18
+ FileStream stream = null ;
19
+ try
20
+ {
21
+ stream = new FileStream (
22
+ lockFile ,
23
+ FileMode . OpenOrCreate ,
24
+ FileAccess . ReadWrite ,
25
+ FileShare . Delete ) ;
26
+
27
+ return await action ( ) ;
28
+ }
29
+ catch ( IOException )
30
+ {
31
+ stream ? . Dispose ( ) ;
32
+ await Task . Delay ( 1000 ) ;
33
+ }
34
+ finally
35
+ {
36
+ stream ? . Dispose ( ) ;
37
+ }
38
+ }
39
+
40
+ throw new IOException ( $ "Could not acquire file lock on { lockFile } after multiple attempts.") ;
41
+ }
42
+
43
+ public static Task WithFileLockAsync ( string lockFileName , Func < Task > action )
44
+ {
45
+ return WithFileLockAsync ( lockFileName , async ( ) =>
46
+ {
47
+ await action ( ) ;
48
+ return true ;
49
+ } ) ;
50
+ }
51
+ }
52
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments