package runbook

import (
	"context"
	"sync"
)

var jobCancelMap sync.Map

func registerJobCancel(jobID uint, cancel context.CancelFunc) {
	jobCancelMap.Store(jobID, cancel)
}

func clearJobCancel(jobID uint) {
	if v, ok := jobCancelMap.Load(jobID); ok {
		if cancel, ok2 := v.(context.CancelFunc); ok2 {
			cancel()
		}
		jobCancelMap.Delete(jobID)
	}
}

func CancelJob(jobID uint) bool {
	if v, ok := jobCancelMap.Load(jobID); ok {
		if cancel, ok2 := v.(context.CancelFunc); ok2 {
			cancel()
		}
		jobCancelMap.Delete(jobID)
		return true
	}
	return false
}
