-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTransactions.cs
More file actions
233 lines (206 loc) · 6.07 KB
/
Transactions.cs
File metadata and controls
233 lines (206 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Runtime.Cache;
using Waher.Runtime.Inventory;
namespace Waher.Runtime.Transactions
{
/// <summary>
/// Maintains a collection of active transactions.
/// </summary>
/// <typeparam name="T">Type of transaction managed by the class</typeparam>
public class Transactions<T> : ITransactions
where T : ITransaction
{
private readonly Cache<Guid, T> transactions;
/// <summary>
/// Maintains a collection of active transactions.
/// </summary>
/// <param name="TransactionTimeout">Maximum time before a transaction needs to complete or fail.</param>
public Transactions(TimeSpan TransactionTimeout)
{
this.transactions = new Cache<Guid, T>(int.MaxValue, TransactionTimeout, TransactionTimeout, true);
this.transactions.Removed += this.Transactions_Removed;
TransactionModule.Register(this);
}
private async Task Transactions_Removed(object Sender, CacheItemEventArgs<Guid, T> e)
{
if (e.Reason != RemovedReason.Manual)
{
try
{
T Transaction = e.Value;
if (Transaction.State != TransactionState.Committed &&
Transaction.State != TransactionState.RolledBack)
{
await Transaction.Abort();
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
}
/// <summary>
/// Rolls back any pending transactions and disposes of the object.
/// </summary>
public void Dispose()
{
TransactionModule.Unregister(this);
this.transactions.Removed -= this.Transactions_Removed;
this.transactions.Clear();
this.transactions.Dispose();
}
/// <summary>
/// Creates a new transaction
/// </summary>
/// <typeparam name="T2">Type of transaction class to create. Must be
/// equal to <typeparamref name="T"/> or a descendant.</typeparam>
/// <param name="Arguments">Constructor arguments.</param>
/// <returns>New transaction</returns>
public T2 CreateNew<T2>(params object[] Arguments)
where T2 : T
{
T2 Transaction = Types.Instantiate<T2>(false, Arguments);
this.transactions.Add(Transaction.Id, Transaction);
return Transaction;
}
/// <summary>
/// Register a transaction created elsewhere with the collection.
/// </summary>
/// <param name="Transaction">Transaction already created.</param>
public void Register(T Transaction)
{
this.transactions.Add(Transaction.Id, Transaction);
}
/// <summary>
/// Unregisters a transaction.
/// </summary>
/// <param name="Id">Transaction ID.</param>
/// <returns>If the transaction was found and removed.</returns>
public bool Unregister(Guid Id)
{
return this.transactions.Remove(Id);
}
/// <summary>
/// Unregisters a transaction.
/// </summary>
/// <param name="Transaction">Transaction reference.</param>
/// <returns>If the transaction was found and removed.</returns>
public bool Unregister(T Transaction)
{
if (this.transactions.TryGetValue(Transaction.Id, out T Transaction2) &&
Transaction2.Equals(Transaction))
{
return this.transactions.Remove(Transaction.Id);
}
else
return false;
}
/// <summary>
/// Tries to get a transaction, given its ID.
/// </summary>
/// <param name="Id">Transaction ID.</param>
/// <param name="Transaction">Transaction, if found.</param>
/// <returns>If a transaction with the corresponding ID was found.</returns>
public bool TryGetTransaction(Guid Id, out T Transaction)
{
return this.transactions.TryGetValue(Id, out Transaction);
}
/// <summary>
/// Prepares a transaction in the collection.
/// </summary>
/// <param name="TransactionId">Transaction ID</param>
/// <returns>If a transaction with the corresponding ID was found, and successfully prepared.</returns>
public async Task<bool> Prepare(Guid TransactionId)
{
try
{
if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
return false;
return await Transaction.Prepare();
}
catch (Exception ex)
{
Log.Exception(ex);
return false;
}
}
/// <summary>
/// Executes a transaction in the collection.
/// </summary>
/// <param name="TransactionId">Transaction ID</param>
/// <returns>If a transaction with the corresponding ID was found, and successfully executed.</returns>
public async Task<bool> Execute(Guid TransactionId)
{
try
{
if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
return false;
return await Transaction.Execute();
}
catch (Exception ex)
{
Log.Exception(ex);
return false;
}
}
/// <summary>
/// Cimmits a transaction in the collection.
/// </summary>
/// <param name="TransactionId">Transaction ID</param>
/// <returns>If a transaction with the corresponding ID was found, and successfully committed.</returns>
public async Task<bool> Commit(Guid TransactionId)
{
try
{
if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
return false;
if (!await Transaction.Commit())
return false;
this.transactions.Remove(TransactionId);
return true;
}
catch (Exception ex)
{
Log.Exception(ex);
return false;
}
}
/// <summary>
/// Prepares a transaction in the collection.
/// </summary>
/// <param name="TransactionId">Transaction ID</param>
/// <returns>If a transaction with the corresponding ID was found, and successfully prepared.</returns>
public async Task<bool> Rollback(Guid TransactionId)
{
try
{
if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
return false;
if (!await Transaction.Rollback())
return false;
this.transactions.Remove(TransactionId);
return true;
}
catch (Exception ex)
{
Log.Exception(ex);
return false;
}
}
/// <summary>
/// Gets pending transactions.
/// </summary>
public Task<ITransaction[]> GetTransactions()
{
T[] Transactions = this.transactions.GetValues();
int i, c = Transactions.Length;
ITransaction[] Result = new ITransaction[c];
for (i = 0; i < c; i++)
Result[i] = (ITransaction)Transactions[i];
return Task.FromResult<ITransaction[]>(Result);
}
}
}