-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLauncher.cpp
195 lines (155 loc) · 7.12 KB
/
Launcher.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "Launcher.h"
#include <inttypes.h>
namespace Thunder {
ENUM_CONVERSION_BEGIN(Plugin::Launcher::mode)
{ Plugin::Launcher::mode::RELATIVE, _TXT("relative") },
{ Plugin::Launcher::mode::ABSOLUTE, _TXT("absolute") },
{ Plugin::Launcher::mode::ABSOLUTE_WITH_INTERVAL, _TXT("interval") },
ENUM_CONVERSION_END(Plugin::Launcher::mode)
namespace Plugin {
namespace {
static Metadata<Launcher> metadata(
// Version
1, 0, 0,
// Preconditions
{},
// Terminations
{},
// Controls
{}
);
}
/* static */ Launcher::ProcessObserver Launcher::_observer;
/* virtual */ const string Launcher::Initialize(PluginHost::IShell* service)
{
Core::Time scheduleTime;
Time interval;
string message;
Config config;
ASSERT(_service == nullptr);
ASSERT(_memory == nullptr);
ASSERT(_activity.IsValid() == false);
// Setup skip URL for right offset.
config.FromString(service->ConfigLine());
if ((config.Command.IsSet() == false) || (config.Command.Value().empty() == true)) {
message = _T("Command is not set");
}
else if (ScheduleParameters(config, message, scheduleTime, interval) == true) {
_service = service;
_service->AddRef();
_deactivationInProgress = false;
_memory = Core::ServiceType<MemoryObserverImpl>::Create<Exchange::IMemory>(0);
ASSERT(_memory != nullptr);
_activity = Core::ProxyType<Job>::Create(&config, interval, _memory);
ASSERT (_activity.IsValid() == true);
// Well if we where able to parse the parameters (if needed) we are ready to start it..
_observer.Register(&_notification);
_activity->Schedule(scheduleTime);
}
return (message);
}
/* virtual */ void Launcher::Deinitialize(PluginHost::IShell* /* service */)
{
if (_service != nullptr) {
ASSERT(_memory != nullptr);
ASSERT(_activity.IsValid() == true);
_deactivationInProgress = true;
_activity->Shutdown();
_observer.Unregister(&_notification);
_activity.Release();
_memory->Release();
_memory = nullptr;
_service->Release();
_service = nullptr;
}
}
/* virtual */ string Launcher::Information() const
{
// No additional info to report.
return (string());
}
void Launcher::Update(const ProcessObserver::Info& info)
{
// There is always an Activity as the unregister takes place in a locked sequence. So no new notifications
// Will enter after the unregister of this handler.
ASSERT (_activity.IsValid() == true);
ASSERT(_service != nullptr);
// This can potentially be called on a socket thread, so the deactivation (wich in turn kills this object) must be done
// on a seperate thread. Also make sure this call-stack can be unwound before we are totally destructed.
if (_activity->IsActive() == true) {
_activity->Update(info);
if (_activity->IsActive() == false) {
uint32_t result = _activity->ExitCode();
if (result != Core::ERROR_NONE) {
if (_deactivationInProgress == false) {
_deactivationInProgress = true;
SYSLOG(Logging::Fatal, (_T("FORCED Shutdown: %s by error: %d."), _service->Callsign().c_str(), result));
Core::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::FAILURE));
}
}
else if (_activity->Continuous() == false) {
if (_deactivationInProgress == false) {
_deactivationInProgress = true;
TRACE(Trace::Information, (_T("Launcher [%s] has run succesfully, deactivation requested."), _service->Callsign().c_str()));
Core::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::AUTOMATIC));
}
}
else {
TRACE(Trace::Information, (_T("Launcher [%s] has run succesfully, scheduled for the next run."), _service->Callsign().c_str()));
}
}
}
}
bool Launcher::ScheduleParameters(const Config& config, string& message, Core::Time& scheduleTime, Time& interval) {
// initialize with defaults..
scheduleTime = Core::Time::Now();
interval = Time();
// Only if a schedule section is set, we need to do something..
if (config.ScheduleTime.IsSet() == true) {
mode timeMode = config.ScheduleTime.Mode.Value();
Time time(config.ScheduleTime.Time.Value());
interval = Time(config.ScheduleTime.Interval.Value());
if (time.IsValid() != true) {
message = _T("Incorrect time format for Scheduled time.");
}
else if ( (config.ScheduleTime.Interval.IsSet() == true) && (interval.IsValid() != true) ) {
message = _T("Incorrect time format for Interval time.");
}
else if ( (timeMode == ABSOLUTE_WITH_INTERVAL) && ((interval.IsValid() == false) || (interval.TimeInSeconds() == 0)) ) {
message = _T("Requested mode is ABSOLUTE WITH INTERVAL but no interval (or 0 second interval) is given.");
}
else {
// All signals green, we have valid input, calculate the ScheduleTime/Interval time
if (timeMode == RELATIVE) { //Schedule Job at relative timing
scheduleTime.Add(time.TimeInSeconds() * Time::MilliSecondsPerSecond);
}
else {
Core::Time now (scheduleTime);
// Go to a first viable start time (compared to the current time, in seconds)
scheduleTime = Core::Time(now.Year(), now.Month(), now.Day(),
(time.HasHours() ? time.Hours() : now.Hours()),
(time.HasMinutes() ? time.Minutes() : now.Minutes()),
time.Seconds(), 0, false);
if (timeMode == ABSOLUTE_WITH_INTERVAL) {
uint32_t intervalJump = interval.TimeInSeconds() * Time::MilliSecondsPerSecond;
if (scheduleTime >= now) {
Core::Time workTime (scheduleTime);
while (workTime.Sub(intervalJump) > now) { scheduleTime.Sub(intervalJump); }
}
else {
// Now increment with the interval till we reach a valid point
while (scheduleTime < now) { scheduleTime.Add(intervalJump); }
}
}
else if (scheduleTime < now) {
uint32_t jump = (time.HasHours() ? Time::HoursPerDay * Time::MinutesPerHour * Time::SecondsPerMinute :
(time.HasMinutes() ? Time::MinutesPerHour * Time::SecondsPerMinute : Time::SecondsPerMinute));
scheduleTime.Add(jump * Time::MilliSecondsPerSecond);
}
}
}
}
return (message.empty());
}
} //namespace Plugin
} // namespace Thunder