-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZazClient.cs
106 lines (97 loc) · 3.63 KB
/
ZazClient.cs
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
using System;
using System.Threading.Tasks;
using Zaz.Client.Avanced;
using Zaz.Client.Avanced.Logging;
namespace Zaz.Client
{
public class ZazClient
{
readonly AdvancedZazClient _underlineClient;
public ZazClient(string url, ZazConfiguration configuration = null)
{
_underlineClient = new AdvancedZazClient(url, configuration);
}
/// <summary>
/// Post command in sync manner.
/// </summary>
/// <exception cref="Zaz.Client.ZazException">Wraps all errors occurred while sending command</exception>
[Obsolete("Use PostAsync method instead")]
public string PostLegacy(object cmd, params string[] tags)
{
try
{
var posting = _underlineClient.PostAsync(new CommandEnvelope
{
Key = cmd.GetType().FullName,
Command = cmd,
Tags = tags
});
return posting.Result;
}
catch (AggregateException aex)
{
var ex = aex.Flatten();
if (ex.InnerExceptions.Count > 1)
{
throw ZazException.CreateDefault(ex);
}
var ex2 = ex.GetBaseException();
throw ZazException.CreateDefault(ex2);
}
}
public Task<string> PostAsync(object cmd, params string[] tags)
{
return _underlineClient.PostAsync(new CommandEnvelope
{
Key = cmd.GetType().FullName,
Command = cmd,
Tags = tags
}).ContinueWith(task =>
{
if (task.Status == TaskStatus.Faulted && task.Exception != null)
{
var aex = task.Exception;
var ex = aex.Flatten();
if (ex.InnerExceptions.Count > 1)
{
throw ZazException.CreateDefault(ex);
}
var ex2 = ex.GetBaseException();
throw ZazException.CreateDefault(ex2);
}
return task.Result;
});
}
/// <summary>
/// Post command in sync manner.
/// This method causing some heavy traffic on checking the scheduled task status. And this method is not
/// covered with any tests as they been getting stuck on the massive tests run locally and on the build
/// server. This marked it as protected to make possible to write some hacks in case of compatibility issues.
/// </summary>
/// <exception cref="Zaz.Client.ZazException">Wraps all errors occurred while sending command</exception>
[Obsolete("Use PostAsync method instead.")]
protected void Post(object cmd, string[] tags = null, IObserver<LogEntry> log = null)
{
try
{
var posting = _underlineClient.PostScheduled(new CommandEnvelope
{
Key = cmd.GetType().FullName,
Command = cmd,
Tags = tags
}, log ?? new ZazLogToSystemTraceAdapter());
posting.Wait();
}
catch (AggregateException aex)
{
var ex = aex.Flatten();
if (ex.InnerExceptions.Count > 1)
{
throw ZazException.CreateDefault(ex);
}
var ex2 = ex.GetBaseException();
throw ZazException.CreateDefault(ex2);
}
}
}
}