Skip to content

Commit 6de1ebe

Browse files
committed
[OBJECT] Moving the "launch" into a stan-a-lone object with just enough locgic.
1 parent ff3762b commit 6de1ebe

2 files changed

Lines changed: 105 additions & 98 deletions

File tree

Launcher.cpp

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
6868

6969
/* virtual */ const string Launcher::Initialize(PluginHost::IShell* service)
7070
{
71+
Time time;
72+
Time interval;
7173
string message;
7274
Config config;
7375

@@ -79,52 +81,42 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
7981

8082
config.FromString(_service->ConfigLine());
8183

82-
_closeTime = (config.CloseTime.Value());
83-
84-
static Core::Process::Options options(config.Command.Value().c_str());
85-
auto iter = config.Parameters.Elements();
84+
if (config.ScheduleTime.IsSet() == true) {
8685

87-
while (iter.Next() == true) {
88-
const Config::Parameter& element(iter.Current());
86+
time = Time(config.ScheduleTime.Time.Value());
87+
if (time.IsValid() != true) {
88+
SYSLOG(Trace::Warning, "Interval format is wrong");
89+
}
8990

90-
if ((element.Option.IsSet() == true) && (element.Option.Value().empty() == false)) {
91-
if ((element.Value.IsSet() == true) && (element.Value.Value().empty() == false)) {
92-
options.Set(element.Option.Value(), element.Value.Value());
93-
}
94-
else {
95-
options.Set(element.Option.Value());
96-
}
91+
interval = Time(config.ScheduleTime.Interval.Value());
92+
if (interval.IsValid() != true) {
93+
SYSLOG(Trace::Warning, "Interval format is wrong");
9794
}
95+
printf("%s:%s:%d %s %s\n", __FILE__, __func__, __LINE__, time.c_str(), interval.c_str());
9896
}
9997

100-
_options = &options;
98+
_activity = Core::ProxyType<Job>::Create(&config, interval)
10199

102-
if (config.ScheduleTime.IsSet() == true) {
100+
if (_activity.IsValid() == true) {
101+
if (_activity->IsOperational() == true) {
102+
// Well if we where able to parse the parameters (if needed) we are ready to start it..
103+
_observer.Register(&_notification);
103104

104-
string time(config.ScheduleTime.Time.Value());
105-
if (time.empty() == false) {
106-
if (_time.Parse(time) != true) {
107-
TRACE_L1("Time format is wrong");
105+
if (_time.Valid() == true) {
106+
Workerpool::Instance().Schedule(scheduledTime, _activity);
108107
}
109-
}
110-
111-
string interval(config.ScheduleTime.Interval.Value());
112-
if (interval.empty() == false) {
113-
if (_interval.Parse(interval) != true) {
114-
TRACE_L1("Interval format is wrong");
108+
else {
109+
Workerpool::Instance().Submit(_activity);
115110
}
116111
}
117-
printf("%s:%s:%d %s %s\n", __FILE__, __func__, __LINE__, time.c_str(), interval.c_str());
112+
else {
113+
message = _T("Could not parse the configuration for the job.");
114+
}
118115
}
119-
120-
_observer.Register(&_notification);
121-
122-
// Well if we where able to parse the parameters (if needed) we are ready to start it..
123-
bool status = LaunchJob(_time);
124-
if (status == false) {
125-
_observer.Unregister(&_notification);
126-
message = _T("Could not spawn the requested app/script [") + config.Command.Value() + ']';
116+
else {
117+
message = _T("Could not create the job.");
127118
}
119+
128120
return (message);
129121
}
130122

@@ -159,48 +151,24 @@ SERVICE_REGISTRATION(Launcher, 1, 0);
159151
return (string());
160152
}
161153

162-
bool Launcher::ScheduleJob(Time time)
163-
{
164-
Core::Time scheduledTime(Core::Time::Now());
165-
uint64_t timeValueToTrigger = ((time.Hour() * 60 + time.Minute()) * 60 + time.Second()) * 1000;
166-
167-
scheduledTime.Add(timeValueToTrigger);
168-
PluginHost::WorkerPool::Instance().Schedule(scheduledTime, _activity);
169-
}
170-
171-
bool Launcher::LaunchJob(Time time)
172-
{
173-
bool status = true;
174-
if (time.Hour() == 0 && time.Minute() == 0 && time.Second() == 0) {
175-
_process.Launch(*_options, &_pid);
176-
177-
if (_pid == 0) {
178-
_observer.Unregister(&_notification);
179-
status = false;
180-
}
181-
else {
182-
_memory = Core::Service<MemoryObserverImpl>::Create<Exchange::IMemory>(_pid);
183-
ScheduleJob(_interval);
184-
}
185-
}
186-
else {
187-
ScheduleJob(_time);
188-
}
189-
return status;
190-
}
191-
192154
void Launcher::Update(const ProcessObserver::Info& info)
193155
{
194156
// This can potentially be called on a socket thread, so the deactivation (wich in turn kills this object) must be done
195157
// on a seperate thread. Also make sure this call-stack can be unwound before we are totally destructed.
196-
if (_pid == info.Id()) {
158+
if ((_activity.IsValid() == true) && (_activity->Process.Pid() == info.Id()) {
197159

198160
ASSERT(_service != nullptr);
199161

200162
if (info.Event() == ProcessObserver::Info::EVENT_EXIT) {
201163

202164
if ((info.ExitCode() & 0xFFFF) == 0) {
203-
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::AUTOMATIC));
165+
// Only do this if we do not need a retrigger on an intervall.
166+
if (_activity->IsOperational() == false) {
167+
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::AUTOMATIC));
168+
}
169+
else {
170+
TRACE(Trace::Information, (_T("The process has run, and completed succefully.")));
171+
}
204172
}
205173
else {
206174
PluginHost::WorkerPool::Instance().Submit(PluginHost::IShell::Job::Create(_service, PluginHost::IShell::DEACTIVATED, PluginHost::IShell::FAILURE));

Launcher.h

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -344,23 +344,41 @@ class Launcher : public PluginHost::IPlugin {
344344
};
345345

346346
class Time {
347-
public:
347+
public:
348348
Time()
349-
: _hour(0)
350-
, _minute(0)
351-
, _second(0)
349+
: _hour(~0)
350+
, _minute(~0)
351+
, _second(~0)
352+
{
353+
}
354+
Time(const string& time)
355+
: _hour(~0)
356+
, _minute(~0)
357+
, _second(~0)
358+
{
359+
Parse(time);
360+
}
361+
Time(const Time& copy)
362+
: _hour(copy._hour)
363+
, _minute(copy._minute)
364+
, _second(copy._second)
352365
{
353366
}
354-
355367
~Time ()
356368
{
357369
}
358370

359-
uint8_t Hour() { return _hour; }
360-
uint8_t Minute() { return _minute; }
361-
uint8_t Second() { return _second; }
371+
public:
372+
bool IsValid () const { return (HasSeconds() || HasMinutes() || HasHours()); }
373+
bool HasHours() const { return (_hour != static_cast<uint8_t>(~0)); }
374+
bool HasMinutes() const { return (_hour != static_cast<uint8_t>(~0)); }
375+
bool HasSeconds() const { return (_hour != static_cast<uint8_t>(~0)); }
376+
uint8_t Hour() const { return _hour; }
377+
uint8_t Minute() const { return _minute; }
378+
uint8_t Second() const { return _second; }
362379

363-
bool Parse(string time) {
380+
private:
381+
bool Parse(const string& time) {
364382
bool status = true;
365383
printf("%s:%s:%d: time = %s\n", __FILE__, __func__, __LINE__, time.c_str());
366384

@@ -441,32 +459,63 @@ class Launcher : public PluginHost::IPlugin {
441459
};
442460

443461
private:
444-
class PeriodicSync : public Core::IDispatchType<void> {
462+
class Job: public Core::IDispatchType<void> {
445463
private:
446-
PeriodicSync() = delete;
447-
PeriodicSync(const PeriodicSync&) = delete;
448-
PeriodicSync& operator=(const PeriodicSync&) = delete;
464+
Job() = delete;
465+
Job(const Job&) = delete;
466+
Job& operator=(const Job&) = delete;
449467

450468
public:
451-
PeriodicSync(Launcher* launcher)
452-
: _launcher(launcher)
469+
Job(Config* config const Time& interval)
470+
: _hasRun(false)
471+
, _options(config->Command.Value().c_str())
453472
{
454-
ASSERT(launcher != nullptr);
473+
auto iter = config->Parameters.Elements();
474+
475+
while (iter.Next() == true) {
476+
const Config::Parameter& element(iter.Current());
477+
478+
if ((element.Option.IsSet() == true) && (element.Option.Value().empty() == false)) {
479+
if ((element.Value.IsSet() == true) && (element.Value.Value().empty() == false)) {
480+
options.Set(element.Option.Value(), element.Value.Value());
481+
}
482+
else {
483+
options.Set(element.Option.Value());
484+
}
485+
}
486+
}
455487
}
456-
~PeriodicSync()
488+
~Job()
457489
{
458490
}
459491

460492
public:
493+
bool IsOperational() const {
494+
return ((_hasRun == false) && (_interval.IsValid() == false));
495+
}
496+
Core::Process& Process() {
497+
_return (_process);
498+
}
461499
virtual void Dispatch() override
462500
{
501+
_hasRun = true;
463502
Time time;
464-
printf("%s:%s:%d: \n", __FILE__, __func__, __LINE__);
465-
_launcher->LaunchJob(time);
503+
// Check if the process is not active, no need to reschedule the same job againb.
504+
if (_process.IsActive() == false) {
505+
printf("%s:%s:%d: \n", __FILE__, __func__, __LINE__);
506+
_process.Launch(_options);
507+
}
508+
if (_interval.IsValid() == true) {
509+
// Reschedule our next launch point...
510+
Workerpool::Instance().Schedule(this, CurrentTime + Interval);
511+
}
466512
}
467513

468514
private:
469-
Launcher* _launcher;
515+
bool _hasRun;
516+
Core::Process::Options _options(config.Command.Value().c_str());
517+
Core::Process _process;
518+
Time _interval;
470519
};
471520

472521
public:
@@ -484,7 +533,7 @@ class Launcher : public PluginHost::IPlugin {
484533
, _interval()
485534
, _options(nullptr)
486535
, _client(this)
487-
, _activity(Core::ProxyType<PeriodicSync>::Create(_client))
536+
, _activity()
488537
{
489538
}
490539
#ifdef __WIN32__
@@ -521,28 +570,18 @@ class Launcher : public PluginHost::IPlugin {
521570
// to this plugin. This Metadata can be used by the MetData plugin to publish this information to the ouside world.
522571
string Information() const override;
523572

524-
bool LaunchJob(Time time);
525-
526573
private:
527574
void Update(const ProcessObserver::Info& info);
528-
bool ScheduleJob(Time time);
575+
bool Execute();
529576

530577
private:
531578
PluginHost::IShell* _service;
532-
Core::Process _process;
533579
uint32_t _pid;
534580
uint8_t _closeTime;
535581
Core::Sink<Notification> _notification;
536582
Exchange::IMemory* _memory;
537583

538-
Time _time;
539-
Time _interval;
540-
541584
static ProcessObserver _observer;
542-
543-
Launcher* _client;
544-
Core::Process::Options* _options;
545-
Core::ProxyType<Core::IDispatchType<void> > _activity;
546585
};
547586

548587
} //namespace Plugin

0 commit comments

Comments
 (0)