-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop Heartbeat monitor jobs on cancelation #20570
Conversation
Pinging @elastic/uptime (Team:Uptime) |
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.
75b3e62
to
cab5285
Compare
Really interesting and good find @jsoriano ! I tried to add a test here, but I couldn't, because there was no externally visible thing to test. We are however missing cancellation tests. Would you mind adding this one below to this patch? This should be added to I guess it's kind of weird to add a test that doesn't really test the patch, but given that there's no good test to write, I think this will have to do. I suppose we could add more insight into the scheduler internals, but that feels like a weird design choice. func TestCancellingJobs(t *testing.T) {
s := NewWithLocation(10, monitoring.NewRegistry(), tarawaTime())
require.NoError(t, s.Start())
// Mutex to guard removeFn
taskInitMtx := sync.Mutex{}
// Let the job run once, then cancel it immediately
taskInitMtx.Lock()
var removeFn func ()
timesRan := batomic.MakeInt(0)
removeFn, err := s.Add(testSchedule{delay: 0}, "testCancel", func(ctx context.Context) []TaskFunc {
timesRan.Inc()
taskInitMtx.Lock()
removeFn()
taskInitMtx.Unlock()
return nil
})
require.NoError(t, err)
taskInitMtx.Unlock()
// It's hard to tell if the job still exists since
// we just recursively requeue them, but we should know after a second
time.Sleep(time.Second)
require.Equal(t, 1, timesRan.Load())
require.NoError(t, s.Stop())
} |
@andrewvc yeah, I also couldn't find a way to test this. As you said we would need to expose scheduler or timer queue internals and can be weird. I thought that a way to do it could be to expose the length of the timer queue, but this is not reliable because the task is not in the queue while it is being executed. Regarding the test for cancelation, wdyt about discussing it in a separate PR? I think it is always complicated to automatically test for things that are not expected to happen. In this case I am concerned by the sleep, I would prefer not having to add it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. We can discuss the added test in a separate PR
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. (cherry picked from commit a6d98d6)
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. (cherry picked from commit a6d98d6)
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. (cherry picked from commit a6d98d6)
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. (cherry picked from commit a6d98d6)
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.
…0588) 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. (cherry picked from commit 5921705)
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.
Checklist
I have commented my code, particularly in hard-to-understand areasI have made corresponding changes to the documentationI have made corresponding change to the default configuration filesI have added tests that prove my fix is effective or that my feature worksCHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.How to test this PR locally
-d scheduler
.Related issues