@@ -98,6 +98,7 @@ void PeriodicTask::Stop() noexcept {
9898}
9999
100100void 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
120121void 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) {
133139bool PeriodicTask::IsRunning () const { return task_.IsValid (); }
134140
135141void 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