|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -namespace Microsoft.DotNet.Cli |
| 4 | +namespace Microsoft.DotNet.Cli; |
| 5 | + |
| 6 | +internal interface ITransactionContext |
5 | 7 | { |
6 | | - internal interface ITransactionContext |
7 | | - { |
8 | | - public void AddRollbackAction(Action action); |
| 8 | + public void AddRollbackAction(Action action); |
| 9 | + |
| 10 | + // Add an action which will be run at the end of the transaction, whether it succeeds or not (after any rollback actions, if applicable) |
| 11 | + public void AddCleanupAction(Action action); |
| 12 | +} |
| 13 | + |
| 14 | +internal class CliTransaction |
| 15 | +{ |
| 16 | + // Delegate called when rollback starts, mainly in order to print a log message |
| 17 | + public Action RollbackStarted { get; set; } |
9 | 18 |
|
10 | | - // Add an action which will be run at the end of the transaction, whether it succeeds or not (after any rollback actions, if applicable) |
11 | | - public void AddCleanupAction(Action action); |
| 19 | + // Delegate called when rollback fails. If set, exception will be passed to the delegate, but not rethrown |
| 20 | + public Action<Exception> RollbackFailed { get; set; } |
| 21 | + |
| 22 | + public static void RunNew(Action<ITransactionContext> action) |
| 23 | + { |
| 24 | + new CliTransaction().Run(action); |
12 | 25 | } |
13 | 26 |
|
14 | | - internal class CliTransaction |
| 27 | + public void Run(Action<ITransactionContext> action, Action rollback) |
15 | 28 | { |
16 | | - // Delegate called when rollback starts, mainly in order to print a log message |
17 | | - public Action RollbackStarted { get; set; } |
| 29 | + Run(context => |
| 30 | + { |
| 31 | + context.AddRollbackAction(rollback); |
18 | 32 |
|
19 | | - // Delegate called when rollback fails. If set, exception will be passed to the delegate, but not rethrown |
20 | | - public Action<Exception> RollbackFailed { get; set; } |
| 33 | + action(context); |
| 34 | + }); |
| 35 | + } |
21 | 36 |
|
22 | | - public static void RunNew(Action<ITransactionContext> action) |
| 37 | + public void Run(Action<ITransactionContext> action) |
| 38 | + { |
| 39 | + TransactionContext transactionContext = new(); |
| 40 | + try |
23 | 41 | { |
24 | | - new CliTransaction().Run(action); |
| 42 | + action(transactionContext); |
25 | 43 | } |
26 | | - |
27 | | - public void Run(Action<ITransactionContext> action, Action rollback) |
| 44 | + catch (Exception) |
28 | 45 | { |
29 | | - Run(context => |
30 | | - { |
31 | | - context.AddRollbackAction(rollback); |
32 | | - |
33 | | - action(context); |
34 | | - }); |
35 | | - } |
| 46 | + // Roll back transaction |
| 47 | + RollbackStarted?.Invoke(); |
36 | 48 |
|
37 | | - public void Run(Action<ITransactionContext> action) |
38 | | - { |
39 | | - TransactionContext transactionContext = new(); |
40 | | - try |
41 | | - { |
42 | | - action(transactionContext); |
43 | | - } |
44 | | - catch (Exception) |
| 49 | + transactionContext.RollbackActions.Reverse(); |
| 50 | + foreach (var rollbackAction in transactionContext.RollbackActions) |
45 | 51 | { |
46 | | - // Roll back transaction |
47 | | - RollbackStarted?.Invoke(); |
48 | | - |
49 | | - transactionContext.RollbackActions.Reverse(); |
50 | | - foreach (var rollbackAction in transactionContext.RollbackActions) |
| 52 | + try |
| 53 | + { |
| 54 | + rollbackAction(); |
| 55 | + } |
| 56 | + catch (Exception ex) |
51 | 57 | { |
52 | | - try |
| 58 | + if (RollbackFailed != null) |
53 | 59 | { |
54 | | - rollbackAction(); |
| 60 | + RollbackFailed(ex); |
55 | 61 | } |
56 | | - catch (Exception ex) |
| 62 | + else |
57 | 63 | { |
58 | | - if (RollbackFailed != null) |
59 | | - { |
60 | | - RollbackFailed(ex); |
61 | | - } |
62 | | - else |
63 | | - { |
64 | | - throw; |
65 | | - } |
| 64 | + throw; |
66 | 65 | } |
67 | 66 | } |
68 | | - |
69 | | - throw; |
70 | 67 | } |
71 | | - finally |
| 68 | + |
| 69 | + throw; |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + foreach (var cleanupAction in transactionContext.CleanupActions) |
72 | 74 | { |
73 | | - foreach (var cleanupAction in transactionContext.CleanupActions) |
74 | | - { |
75 | | - cleanupAction(); |
76 | | - } |
| 75 | + cleanupAction(); |
77 | 76 | } |
78 | 77 | } |
| 78 | + } |
79 | 79 |
|
80 | | - class TransactionContext : ITransactionContext |
81 | | - { |
82 | | - public List<Action> RollbackActions { get; set; } = new List<Action>(); |
| 80 | + class TransactionContext : ITransactionContext |
| 81 | + { |
| 82 | + public List<Action> RollbackActions { get; set; } = new List<Action>(); |
83 | 83 |
|
84 | | - // Actions which will be run either when the transaction completes successfully, or after rollback actions have been run |
85 | | - public List<Action> CleanupActions { get; set; } = new List<Action>(); |
| 84 | + // Actions which will be run either when the transaction completes successfully, or after rollback actions have been run |
| 85 | + public List<Action> CleanupActions { get; set; } = new List<Action>(); |
86 | 86 |
|
87 | | - public void AddRollbackAction(Action action) |
88 | | - { |
89 | | - RollbackActions.Add(action); |
90 | | - } |
| 87 | + public void AddRollbackAction(Action action) |
| 88 | + { |
| 89 | + RollbackActions.Add(action); |
| 90 | + } |
91 | 91 |
|
92 | | - public void AddCleanupAction(Action action) |
93 | | - { |
94 | | - CleanupActions.Add(action); |
95 | | - } |
| 92 | + public void AddCleanupAction(Action action) |
| 93 | + { |
| 94 | + CleanupActions.Add(action); |
96 | 95 | } |
97 | 96 | } |
| 97 | +} |
98 | 98 |
|
99 | | - static class CliTransactionExtensions |
| 99 | +static class CliTransactionExtensions |
| 100 | +{ |
| 101 | + public static void Run(this ITransactionContext context, Action action, Action rollback = null, Action cleanup = null) |
100 | 102 | { |
101 | | - public static void Run(this ITransactionContext context, Action action, Action rollback = null, Action cleanup = null) |
| 103 | + if (rollback != null) |
102 | 104 | { |
103 | | - if (rollback != null) |
104 | | - { |
105 | | - context.AddRollbackAction(rollback); |
106 | | - } |
107 | | - if (cleanup != null) |
108 | | - { |
109 | | - context.AddCleanupAction(cleanup); |
110 | | - } |
111 | | - action(); |
| 105 | + context.AddRollbackAction(rollback); |
| 106 | + } |
| 107 | + if (cleanup != null) |
| 108 | + { |
| 109 | + context.AddCleanupAction(cleanup); |
112 | 110 | } |
| 111 | + action(); |
113 | 112 | } |
114 | 113 | } |
0 commit comments