-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAbstractCommand.cs
More file actions
164 lines (143 loc) · 4.54 KB
/
AbstractCommand.cs
File metadata and controls
164 lines (143 loc) · 4.54 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
using System;
using System.Linq;
using System.Threading.Tasks;
using Common.Logging;
using InEngine.Core.Exceptions;
using InEngine.Core.IO;
using InEngine.Core.LifeCycle;
using Konsole;
using Quartz;
namespace InEngine.Core
{
abstract public class AbstractCommand : IJob, IWrite, IHasCommandLifeCycle, IHasMailSettings
{
protected ILog Log { get; set; }
public CommandLifeCycle CommandLifeCycle { get; set; }
public Write Write { get; set; }
public ProgressBar ProgressBar { get; internal set; }
public string Name { get; set; }
public string SchedulerGroup { get; set; }
public string ScheduleId { get; set; }
public int SecondsBeforeTimeout { get; set; }
public MailSettings MailSettings { get; set; }
protected AbstractCommand()
{
Log = LogManager.GetLogger(GetType());
ScheduleId = Guid.NewGuid().ToString();
Name = GetType().FullName;
SchedulerGroup = GetType().AssemblyQualifiedName;
Write = new Write();
CommandLifeCycle = new CommandLifeCycle() {
MailSettings = MailSettings
};
SecondsBeforeTimeout = 300;
}
public virtual void Run()
{
throw new NotImplementedException();
}
public virtual void RunWithLifeCycle()
{
try
{
CommandLifeCycle.FirePreActions(this);
if (SecondsBeforeTimeout <= 0)
Run();
else
{
var task = Task.Run(() => Run());
if (!task.Wait(TimeSpan.FromSeconds(SecondsBeforeTimeout)))
throw new Exception($"Scheduled command timed out after {SecondsBeforeTimeout} second(s).");
}
CommandLifeCycle.FirePostActions(this);
}
catch (Exception exception)
{
Log.Error(this, exception);
Failed(exception);
throw new CommandFailedException("Command failed. See inner exception for details.", exception);
}
}
public virtual void Failed(Exception exception)
{}
#region ProgressBar
public void SetProgressBarMaxTicks(int maxTicks)
{
ProgressBar = new ProgressBar(maxTicks);
}
public void UpdateProgress(int tick)
{
ProgressBar.Refresh(tick, Name);
}
#endregion
#region Scheduling
public virtual void Execute(IJobExecutionContext context)
{
if (context != null)
{
var properties = GetType().GetProperties();
context.MergedJobDataMap.ToList().ForEach(x => {
var property = properties.FirstOrDefault(y => y.Name == x.Key);
if (property != null)
property.SetValue(this, x.Value);
});
}
RunWithLifeCycle();
}
#endregion
#region Console output
public IWrite Info(object val)
{
return Write.Info(val);
}
public IWrite Warning(object val)
{
return Write.Warning(val);
}
public IWrite Error(object val)
{
return Write.Error(val);
}
public IWrite Line(object val)
{
return Write.Line(val);
}
public IWrite ColoredLine(object val, ConsoleColor consoleColor)
{
return Write.ColoredLine(val, consoleColor);
}
public IWrite InfoText(object val)
{
return Write.InfoText(val);
}
public IWrite WarningText(object val)
{
return Write.WarningText(val);
}
public IWrite ErrorText(object val)
{
return Write.ErrorText(val);
}
public IWrite Text(object val)
{
return Write.Text(val);
}
public IWrite ColoredText(object val, ConsoleColor consoleColor)
{
return Write.ColoredText(val, consoleColor);
}
public IWrite Newline(int count = 1)
{
return Write.Newline(count);
}
public string FlushBuffer()
{
return Write.FlushBuffer();
}
public void ToFile(string path, string text, bool shouldAppend = false)
{
Write.ToFile(path, text, shouldAppend);
}
#endregion
}
}