Skip to content

Stopped and disabled task is not stopped #4736

@Michal-Leszczynski

Description

@Michal-Leszczynski

When using sctool stop, task is stopped.
When using sctool stop --disable task is disabled, but if it was running, it's execution is not interrupted.
It's not intuitive, undocumented and probably wrong.
We can see that:

func (h *taskHandler) stopTask(w http.ResponseWriter, r *http.Request) {
	t := mustTaskFromCtx(r)

	disable := false
	if d := r.FormValue("disable"); d != "" {
		var err error
		disable, err = strconv.ParseBool(d)
		if err != nil {
			respondBadRequest(w, r, err)
			return
		}
	}

	if t.Enabled && disable {
		t.Enabled = false
		// current task is canceled on save no need to stop it again
		if err := h.Scheduler.PutTask(r.Context(), t); err != nil {
			respondError(w, r, errors.Wrapf(err, "update task %q", t.ID))
			return
		}
	}

But it takes us to:

// Unschedule cancels schedule of a key. It does not stop an active run.
func (s *Scheduler[K]) Unschedule(ctx context.Context, key K) {
	s.listener.OnUnschedule(ctx, key)
	s.mu.Lock()
	s.unscheduleLocked(key)
	s.mu.Unlock()
}

Which contradicts comment from above.

What's funny is that our test for stop and disable actually stops the task manually first, and only later on disables it, so it behaves differently from /stop endpoint handler:

	t.Run("stop and disable task", func(t *testing.T) {
		h := newSchedTestHelper(t, session)
		defer h.close()
		ctx := context.Background()

		Print("When: task is scheduled")
		task := h.makeTask(scheduler.Schedule{
			StartDate: now(),
			Interval:  interval,
		})
		if err := h.service.PutTask(ctx, task); err != nil {
			t.Fatal(err)
		}

		Print("Then: task runs")
		h.assertStatus(task, scheduler.StatusRunning)

		Print("When: task is stopped")
		h.service.StopTask(ctx, task)

		Print("Then: task stops")
		h.assertStatus(task, scheduler.StatusStopped)

		Print("When: task is disabled")
		task.Enabled = false
		if err := h.service.PutTask(ctx, task); err != nil {
			t.Fatal(err)
		}

		Print("Then: task is not executed")
		h.assertNotStatus(task, scheduler.StatusRunning)
	})

I validated that if task is not sopped manually in the test, it fails.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions