Skip to content

Commit a4d60b2

Browse files
committed
added logic
1 parent 117fe92 commit a4d60b2

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

core/src/utils/periodic_task.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void PeriodicTask::Stop() noexcept {
9898
}
9999

100100
void PeriodicTask::SetSettings(Settings settings) {
101+
101102
bool should_notify_task{};
102103
{
103104
auto writer = settings_.StartWrite();
@@ -106,7 +107,7 @@ void PeriodicTask::SetSettings(Settings settings) {
106107
return;
107108
}
108109
settings.flags = writer->flags;
109-
should_notify_task = settings.period != writer->period || settings.exception_period != writer->exception_period;
110+
should_notify_task = settings.period != writer->period || settings.exception_period != writer->exception_period || || settings.enabled != writer->enabled;
110111
*writer = std::move(settings);
111112
writer.Commit();
112113
}
@@ -118,7 +119,12 @@ void PeriodicTask::SetSettings(Settings settings) {
118119
}
119120

120121
void PeriodicTask::ForceStepAsync() {
122+
121123
should_force_step_ = true;
124+
if (!writer->enabled) {
125+
writer->enabled = true;
126+
writer.Commit();
127+
}
122128
changed_event_.Send();
123129
}
124130

@@ -133,6 +139,7 @@ bool PeriodicTask::SynchronizeDebug(bool preserve_span) {
133139
bool PeriodicTask::IsRunning() const { return task_.IsValid(); }
134140

135141
void PeriodicTask::Run() {
142+
136143
bool skip_step = false;
137144
{
138145
auto settings = settings_.Read();
@@ -145,11 +152,12 @@ void PeriodicTask::Run() {
145152
const auto before = std::chrono::steady_clock::now();
146153
bool no_exception = true;
147154

148-
if (!std::exchange(skip_step, false)) {
155+
const auto settings = settings_.Read();
156+
bool taskEnabled = settings->enabled;
157+
if (!std::exchange(skip_step, false) && taskEnabled) {
149158
no_exception = Step();
150159
}
151160

152-
const auto settings = settings_.Read();
153161
auto period = settings->period;
154162
const auto exception_period = settings->exception_period.value_or(period);
155163

@@ -162,12 +170,17 @@ void PeriodicTask::Run() {
162170
start = std::chrono::steady_clock::now();
163171
}
164172

165-
while (changed_event_.WaitForEventUntil(start + MutatePeriod(period))) {
173+
while ((!taskEnabled && changed_event_.WaitForEvent()) || (taskEnabled && changed_event_.WaitForEventUntil(start + MutatePeriod(period)))) {
166174
if (should_force_step_.exchange(false)) {
167-
break;
175+
break;
168176
}
169177
// The config variable value has been changed, reload
170178
const auto settings = settings_.Read();
179+
taskEnabled = settings->enabled;
180+
if(!taskEnabled) {
181+
break;
182+
}
183+
171184
period = settings->period;
172185
const auto exception_period = settings->exception_period.value_or(period);
173186
if (!no_exception) period = exception_period;

0 commit comments

Comments
 (0)