Skip to content

Commit

Permalink
Merge pull request #2 from dl1998/wait-implementation
Browse files Browse the repository at this point in the history
Extend implementation with Wait method
  • Loading branch information
dl1998 authored Feb 22, 2024
2 parents 9ae4233 + c23c74e commit 2f8e6b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

task := ScheduleDemoTask(*newScheduler)

time.Sleep(10 * time.Second)
task.Wait()

fmt.Println()
fmt.Println(task)
Expand Down
13 changes: 11 additions & 2 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (scheduler *Scheduler) ScheduleTask(name string, startTime *time.Time, dura

ticker := time.NewTicker(interval)
defer ticker.Stop()
defer scheduler.removeTask(scheduledTask)
defer scheduler.StopTask(scheduledTask)

for {
select {
Expand All @@ -200,7 +200,10 @@ func (scheduler *Scheduler) ScheduleTask(name string, startTime *time.Time, dura

// StopTask encapsulates task stopping sequence.
func (scheduler *Scheduler) StopTask(task *Task) {
close(task.stopSignal)
if task.stopSignal != nil {
close(task.stopSignal)
task.stopSignal = nil
}
scheduler.removeTask(task)
}

Expand Down Expand Up @@ -246,3 +249,9 @@ func (task *Task) SetToContext(name string, value interface{}) {
func (task *Task) RemoveFromContext(name string) {
delete(task.context, name)
}

func (task *Task) Wait() {
if task.stopSignal != nil {
<-task.stopSignal
}
}

0 comments on commit 2f8e6b7

Please sign in to comment.