Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EventQueue.cs
1using System;
3using System.Threading;
4using System.Threading.Tasks;
6
8{
13 public class EventQueue : EventSink
14 {
15 private Timer timer;
16 private int eventLifetimeDays;
17 private readonly string queueName;
18 private IPersistedQueue queue = null;
19 private bool errorState = false;
20
27 public EventQueue(string ObjectID, string QueueName)
28 : this(ObjectID, QueueName, 0, null)
29 {
30 }
31
40 public EventQueue(string ObjectID, string QueueName, int EventLifetimeDays,
41 TimeSpan? CleanupTime)
42 : base(ObjectID)
43 {
44 this.queueName = QueueName;
45
46 if (EventLifetimeDays <= 0 || !CleanupTime.HasValue)
47 {
48 this.timer = null;
49 this.eventLifetimeDays = 0;
50 }
51 else
52 {
53 if (CleanupTime.Value < TimeSpan.Zero || CleanupTime.Value.TotalDays >= 1.0)
54 throw new ArgumentOutOfRangeException("Invalid time.", nameof(CleanupTime));
55
56 int MillisecondsPerDay = 1000 * 60 * 60 * 24;
57 int MsUntilNext = (int)((DateTime.Today.AddDays(1).Add(CleanupTime.Value) - DateTime.Now).TotalMilliseconds + 0.5);
58
59 this.eventLifetimeDays = EventLifetimeDays;
60 this.timer = new Timer(this.PurgeOldItems, null, MsUntilNext, MillisecondsPerDay);
61 }
62 }
63
67 public override async Task DisposeAsync()
68 {
69 if (!(this.queue is null))
70 {
71 await this.queue.DisposeAsync();
72 this.queue = null;
73 }
74
75 this.timer?.Dispose();
76 this.timer = null;
77
78 await base.DisposeAsync();
79 }
80
85 public override async Task Queue(Event Event)
86 {
87 if (this.queue is null)
88 this.queue = await Database.GetQueue(this.queueName);
89
90 QueuedEvent Item = new QueuedEvent(Event);
91
92 if (await this.queue.Enqueue(Item, this.errorState ? 0 : 10000))
93 this.errorState = false;
94 else if (!this.errorState)
95 {
96 this.errorState = true;
97
98 Event Error = new Event(EventType.Emergency, "Event Queue " + this.queueName +
99 " is full and new events cannot be queued.", this.ObjectID, string.Empty,
100 string.Empty, EventLevel.Major, string.Empty,
101 typeof(EventQueue).Namespace, string.Empty);
102
103 Error.Avoid(this);
104
105 Log.Event(Error);
106 }
107 }
108
112 public async Task ClearQueue()
113 {
114 if (this.queue is null)
115 this.queue = await Database.GetQueue(this.queueName);
116
117 await this.queue.Clear();
118 }
119
120 private async void PurgeOldItems(object P)
121 {
122 try
123 {
124 await this.PurgeOldItems(DateTime.UtcNow.AddDays(-this.eventLifetimeDays));
125 }
126 catch (Exception ex)
127 {
128 Log.Exception(ex);
129 }
130 }
131
140 public async Task<int> PurgeOldItems(DateTime Limit)
141 {
142 int NrEvents = 0;
143 object Item;
144
145 Item = await this.queue.Peek();
146
147 while (!(Item is null))
148 {
149 if (Item is QueuedEvent QueuedEvent && QueuedEvent.Timestamp > Limit)
150 break;
151
152 await this.queue.Dequeue();
153 NrEvents++;
154
155 Item = await this.queue.Peek();
156 }
157
158 if (NrEvents > 0)
159 {
160 KeyValuePair<string, object>[] Tags = new KeyValuePair<string, object>[]
161 {
162 new KeyValuePair<string, object>("Limit", Limit),
163 new KeyValuePair<string, object>("NrEvents", NrEvents)
164 };
165
166 if (NrEvents == 1)
167 Log.Informational("Purging 1 event from the queue.", this.ObjectID, Tags);
168 else
169 Log.Informational("Purging " + NrEvents.ToString() + " events from the queue.", this.ObjectID, Tags);
170 }
171
172 return NrEvents;
173 }
174
175 }
176}
Class representing an event.
Definition: Event.cs:11
void Avoid(IEventSink EventSink)
If the event sink EventSink should be avoided when logging the event.
Definition: Event.cs:190
Base class for event sinks.
Definition: EventSink.cs:9
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:344
static async void Event(Event Event)
Logs an event. It will be distributed to registered event sinks.
Definition: Log.cs:138
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:26
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
Definition: EventQueue.cs:14
override async Task DisposeAsync()
IDisposableAsync.DisposeAsync()
Definition: EventQueue.cs:67
EventQueue(string ObjectID, string QueueName)
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
Definition: EventQueue.cs:27
EventQueue(string ObjectID, string QueueName, int EventLifetimeDays, TimeSpan? CleanupTime)
Creates an even sink that queues incoming events in a local persisted queue, for processing by in ord...
Definition: EventQueue.cs:40
async Task< int > PurgeOldItems(DateTime Limit)
Purges old events from the queue. This method is called once a day automatically. It can also be call...
Definition: EventQueue.cs:140
override async Task Queue(Event Event)
Queues an event to be output.
Definition: EventQueue.cs:85
async Task ClearQueue()
Clears the queue.
Definition: EventQueue.cs:112
Class representing a queued event.
Definition: QueuedEvent.cs:12
DateTime Timestamp
Timestamp of event.
Definition: QueuedEvent.cs:77
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IPersistedQueue > GetQueue(string QueueName)
Gets a persistent dictionary containing objects in a collection.
Definition: Database.cs:2326
Task DisposeAsync()
Disposes of the object, asynchronously.
Inteface for persisted queues.
Task Clear()
Clears the queue.
Task< object > Peek()
Returns the next item available to be dequeued, without dequeueing it. If an item is not available,...
Task< bool > Enqueue(object Item)
Enqueues an item into the queue.
Task< object > Dequeue()
Dequeue an item from the queue. The task will wait for an item to be dequeued, or,...
EventLevel
Event level.
Definition: EventLevel.cs:7
EventType
Type of event.
Definition: EventType.cs:7