Skip to content

Commit

Permalink
feat: implement cancelJob and runAll methods
Browse files Browse the repository at this point in the history
  • Loading branch information
firminochangani committed Jan 21, 2024
1 parent fb96604 commit b278f35
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
10 changes: 9 additions & 1 deletion schedule/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (j *Job) Seconds() *Job {
return j
}

func (j *Job) Do(handler JobHandler) {
func (j *Job) Do(handler JobHandler) *Job {
j.handler = handler
j.scheduler.appendJob(j)

return j
}

func (j *Job) run(ctx context.Context) {
Expand All @@ -66,6 +68,12 @@ func (j *Job) run(ctx context.Context) {
}
}

func (j *Job) runHandler(ctx context.Context) {
j.isRunning.Store(true)
j.handler(ctx)
j.isRunning.Store(false)
}

func (j *Job) stop() {
j.scheduler.logger.Infof("Stopping the job %s", j.id)

Expand Down
10 changes: 10 additions & 0 deletions schedule/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
type Logger interface {
Info(msg string, arg ...any)
Infof(msg string, arg ...any)
Debug(msg string, arg ...any)
Debugf(msg string, arg ...any)
}

type DefaultLogger struct {
Expand All @@ -28,3 +30,11 @@ func (d *DefaultLogger) Info(msg string, arg ...any) {
func (d *DefaultLogger) Infof(msg string, arg ...any) {
d.logger.Info(fmt.Sprintf(msg, arg...))
}

func (d *DefaultLogger) Debug(msg string, arg ...any) {
d.logger.Debug(msg, arg...)
}

func (d *DefaultLogger) Debugf(msg string, arg ...any) {
d.logger.Debug(fmt.Sprintf(msg, arg...))
}
25 changes: 22 additions & 3 deletions schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schedule
import (
"context"
"sync"
"time"

"github.com/oklog/ulid/v2"
)
Expand Down Expand Up @@ -52,9 +53,20 @@ func (s *Schedule) appendJob(job *Job) {
s.logger.Infof("Job with id %s has been appended to the scheduler", job.id)
}

func (s *Schedule) RunAll(delayInSeconds int) {}
// RunAll Runs all jobs regardless if they are scheduled to run or not.
func (s *Schedule) RunAll(ctx context.Context, delay time.Duration) {
s.lock.RLock()
defer s.lock.RUnlock()

s.logger.Debugf("Running all %d jobs with %d of delay in between", len(s.jobs), delay)

// GetJobs Return all jobs
for _, job := range s.jobs {
job.runHandler(ctx)
time.Sleep(delay)
}
}

// GetJobs Returns all jobs
func (s *Schedule) GetJobs() []*Job {
s.lock.RLock()
defer s.lock.RUnlock()
Expand All @@ -69,7 +81,7 @@ func (s *Schedule) GetJobs() []*Job {
return result
}

// Clear Stop all jobs and then delete them
// Clear Stops all jobs and then delete them from the schedule
func (s *Schedule) Clear() {
s.lock.Lock()
defer s.lock.Unlock()
Expand All @@ -80,7 +92,14 @@ func (s *Schedule) Clear() {
}
}

// CancelJob Stops a job a removes it from the schedule
func (s *Schedule) CancelJob(job *Job) {
job.stop()

s.lock.Lock()
defer s.lock.Unlock()

delete(s.jobs, job.id)
}

func (s *Schedule) cancelAllJobs() {
Expand Down
39 changes: 39 additions & 0 deletions schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,42 @@ func TestSchedule_Clear(t *testing.T) {
// Then expect
assert.Empty(t, s.GetJobs())
}

func TestSchedule_CancelJob(t *testing.T) {
s := schedule.New()

// Given
job := s.Every(1).Seconds().Do(func(ctx context.Context) {
t.Log("Handler ran successfully")
})
require.Contains(t, s.GetJobs(), job)

// Do
s.CancelJob(job)

// Expect
assert.NotContains(t, s.GetJobs(), job)
}

func TestSchedule_RunAll(t *testing.T) {
s := schedule.New()
result := make([]int, 3)

handler := func(idx int) func(ctx context.Context) {
return func(ctx context.Context) {
result[idx] = idx
t.Logf("Job %d ran successfully", idx)
}
}

// Given
s.Every(1).Seconds().Do(handler(0))
s.Every(1).Seconds().Do(handler(1))
s.Every(1).Seconds().Do(handler(2))

// Do
s.RunAll(context.Background(), time.Millisecond*10)

// Expect
assert.Equal(t, []int{0, 1, 2}, result, "all jobs to have been run and mutated the 'result' slice")
}

0 comments on commit b278f35

Please sign in to comment.