From cab52859d592829ad037351bd14cc4c877506681 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 12 Aug 2020 13:15:39 +0200 Subject: [PATCH] Stop Heartbeat monitor jobs on cancelation If a monitor is stopped, for example when using autodiscover, the scheduled tasks should be stopped too. Scheduler was rescheduling tasks forever once started, though these tasks were not being executed because they are also aware of the context. This change avoids the execution and rescheduling of tasks once its job context is done. --- CHANGELOG.next.asciidoc | 1 + heartbeat/scheduler/scheduler.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index fde6fe7abd04..e01bc94a2904 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -247,6 +247,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fixed excessive memory usage introduced in 7.5 due to over-allocating memory for HTTP checks. {pull}15639[15639] - Fixed TCP TLS checks to properly validate hostnames, this broke in 7.x and only worked for IP SANs. {pull}17549[17549] - Add support for new `service_name` option to all monitors. {pull}19932[19932]. +- Stop rescheduling tasks of stopped monitors. {pull}20570[20570] *Journalbeat* diff --git a/heartbeat/scheduler/scheduler.go b/heartbeat/scheduler/scheduler.go index 18f927a2d806..87db7075c448 100644 --- a/heartbeat/scheduler/scheduler.go +++ b/heartbeat/scheduler/scheduler.go @@ -188,6 +188,12 @@ func (s *Scheduler) Add(sched Schedule, id string, entrypoint TaskFunc) (removeF var taskFn timerqueue.TimerTaskFn taskFn = func(_ time.Time) { + select { + case <-jobCtx.Done(): + debugf("Job '%v' canceled", id) + return + default: + } s.stats.activeJobs.Inc() lastRanAt = s.runRecursiveJob(jobCtx, entrypoint) s.stats.activeJobs.Dec()