Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConsoleWorker.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
5
7{
11 public static class ConsoleWorker
12 {
13 private static AsyncQueue<WorkItem> queue = new AsyncQueue<WorkItem>();
14 private static bool terminating = false;
15 private static bool terminated = false;
16
17 static ConsoleWorker()
18 {
19 Log.Terminating += Log_Terminating;
20 Task.Run(() => PerformWork());
21 }
22
23 private static void Log_Terminating(object Sender, System.EventArgs e)
24 {
25 Terminate();
26 Log.Terminating -= Log_Terminating;
27 }
28
29 internal static void Terminate()
30 {
31 terminating = true;
32 queue?.Dispose();
33 queue = null;
34 }
35
41 public static Task<bool> Queue(WorkItem Work)
42 {
43 if (!terminating)
44 return queue?.Add(Work) ?? Task.FromResult(false);
45 else
46 return Task.FromResult(false);
47 }
48
52 public static bool Terminating => terminating;
53
57 public static bool Terminated => terminated;
58
62 private static async void PerformWork()
63 {
64 try
65 {
66 WorkItem Item;
67
68 while (!((Item = await (queue?.Wait() ?? Task.FromResult<WorkItem>(null))) is null))
69 {
70 try
71 {
72 await Item.Execute();
73 Item.Processed(true);
74 }
75 catch (Exception ex)
76 {
77 Item.Processed(false);
78 ex = Log.UnnestException(ex);
79
80 Event Event = new Event(Log.GetEventType(ex), ex, string.Empty, string.Empty, string.Empty, EventLevel.Minor,
81 string.Empty, string.Empty);
82
83 foreach (IEventSink Sink in Log.Sinks)
84 {
85 if (Sink.GetType().FullName.Contains(".Console"))
86 Event.Avoid(Sink);
87 }
88
90 }
91 }
92 }
93 catch (Exception ex)
94 {
95 Log.Exception(ex);
96 }
97 finally
98 {
99 terminated = true;
100 }
101 }
102 }
103}
Class representing an event.
Definition: Event.cs:10
void Avoid(IEventSink EventSink)
If the event sink EventSink should be avoided when logging the event.
Definition: Event.cs:189
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static IEventSink[] Sinks
Registered sinks.
Definition: Log.cs:122
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:1647
static EventType GetEventType(Exception Exception)
Gets the event type corresponding to a given exception object.
Definition: Log.cs:1736
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
static async void Event(Event Event)
Logs an event. It will be distributed to registered event sinks.
Definition: Log.cs:128
Processes console tasks, such as input and output, in a serialized asynchronous manner.
static bool Terminated
If the console worker has been terminated.
static Task< bool > Queue(WorkItem Work)
Queues a work item.
static bool Terminating
If the console worker is being terminated.
Manages a Console operation.
Definition: WorkItem.cs:9
void Processed(bool Result)
Flags the item as processed.
Definition: WorkItem.cs:21
abstract Task Execute()
Executes the console operation.
Asynchronous First-in-First-out (FIFO) Queue, for use when transporting items of type T between task...
Definition: AsyncQueue.cs:15
void Dispose()
IDisposable.Dispose
Definition: AsyncQueue.cs:241
Task< bool > Add(T Item)
Adds an item last to the queue.
Definition: AsyncQueue.cs:77
Interface for all event sinks.
Definition: IEventSink.cs:9
EventLevel
Event level.
Definition: EventLevel.cs:7