42 lines
719 B
Go
42 lines
719 B
Go
package raft
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Worker struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
cancelled bool // Variable pour suivre si cancel a été appelé
|
|
}
|
|
|
|
func (w *Worker) Start() {
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-w.ctx.Done():
|
|
fmt.Println("🛑 Worker arrêté !")
|
|
return
|
|
default:
|
|
fmt.Println("🔄 Worker en cours...")
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (w *Worker) Stop() {
|
|
if !w.cancelled {
|
|
w.cancel() // Annuler le contexte
|
|
w.cancelled = true // Marquer comme annulé
|
|
} else {
|
|
fmt.Println("❗ Cancel déjà appelé, Worker déjà arrêté.")
|
|
}
|
|
}
|
|
|
|
func (w *Worker) IsCancelled() bool {
|
|
return w.cancelled
|
|
}
|