-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTransactionModule.cs
More file actions
108 lines (95 loc) · 2.22 KB
/
TransactionModule.cs
File metadata and controls
108 lines (95 loc) · 2.22 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Runtime.Inventory;
namespace Waher.Runtime.Transactions
{
/// <summary>
/// Module making sure no unfinished transactions are left when system ends.
/// </summary>
[Singleton]
public class TransactionModule : IModule
{
private readonly static List<ITransactions> transactions = new List<ITransactions>();
private static bool running = false;
/// <summary>
/// Module making sure no unfinished transactions are left when system ends.
/// </summary>
public TransactionModule()
{
}
/// <summary>
/// Registers a collection of transactions with the module.
/// </summary>
/// <param name="Transactions">Collection of transactions.</param>
public static void Register(ITransactions Transactions)
{
lock (transactions)
{
if (!transactions.Contains(Transactions))
transactions.Add(Transactions);
}
}
/// <summary>
/// Unregisters a collection of transactions with the module.
/// </summary>
/// <param name="Transactions">Collection of transactions.</param>
/// <returns>If the collection was found and removed.</returns>
public static bool Unregister(ITransactions Transactions)
{
lock (transactions)
{
return transactions.Remove(Transactions);
}
}
/// <summary>
/// If the transaction module is running.
/// </summary>
public static bool Running => running;
/// <summary>
/// Starts the module.
/// </summary>
public Task Start()
{
running = true;
return Task.CompletedTask;
}
/// <summary>
/// Stops the module.
/// </summary>
public async Task Stop()
{
ITransactions[] Collections;
running = false;
lock (transactions)
{
Collections = transactions.ToArray();
transactions.Clear();
}
foreach (ITransactions Transactions in Collections)
{
try
{
ITransaction[] Pending = await Transactions.GetTransactions();
foreach (ITransaction T in Pending)
{
try
{
await T.Abort();
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
Transactions.Dispose();
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
}
}
}