Skip to content

Commit

Permalink
Add Feature Delete Job By Tag (#33)
Browse files Browse the repository at this point in the history
* Update README.md

* Add Feature Delete Job By Tag

Update Readme How to Use Add Tag and Remove Job By Tag
Add Test Create and Delete Job By Tag

* Add Feature Delete Job By Tag

Update Readme How to Use Add Tag and Remove Job By Tag
Add Test Create and Delete Job By Tag

* Add Exported Error Message

Change logic when deleting jobs with tag function

* Update Logic RemoveTag Return Rerr

Update logic to golang best practice error checking

Co-authored-by: John Roesler <johnrroesler@gmail.com>
Co-authored-by: Streppel <streppels@gmail.com>
  • Loading branch information
3 people committed Apr 29, 2020
1 parent 19d750d commit 8c7e3da
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func main() {
// Do jobs with params
s2.Every(1).Second().Do(taskWithParams, 1, "hello")

// Do Jobs with tags
// initialize tag
tag1 := []string{"tag1"}
tag2 := []string{"tag2"}


s2.Every(1).Week().SetTag(tag1).Do(task)
s2.Every(1).Week().SetTag(tag2).Do(task)

// Removing Job Based on Tag
scheduler.RemoveJobByTag("tag1")

// Do jobs on specific weekday
s2.Every(1).Monday().Do(task)
s2.Every(1).Thursday().Do(task)
Expand Down
1 change: 1 addition & 0 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
ErrNotAFunction = errors.New("only functions can be schedule into the job queue")
ErrPeriodNotSpecified = errors.New("unspecified job period")
ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday")
ErrJobNotFoundWithTag = errors.New("no jobs found with given tag")
)

type timeUnit int
Expand Down
29 changes: 29 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"
"time"
)

Expand Down Expand Up @@ -209,6 +210,27 @@ func (s *Scheduler) removeByCondition(shouldRemove func(*Job) bool) {
}
}

// RemoveJobByTag will Remove Jobs by Tag
func (s *Scheduler) RemoveJobByTag(tag string) error {
jobindex, err := s.findJobsIndexByTag(tag)
if err != nil {
return err
}
// Remove job if jobindex is valid
s.jobs = removeAtIndex(s.jobs, jobindex)
return nil
}

// Find first job index by given string
func (s *Scheduler) findJobsIndexByTag(tag string) (int, error) {
for i, job := range s.jobs {
if strings.Contains(strings.Join(job.Tags(), " "), tag) {
return i, nil
}
}
return -1, ErrJobNotFoundWithTag
}

func removeAtIndex(jobs []*Job, i int) []*Job {
if i == len(jobs)-1 {
return jobs[:i]
Expand Down Expand Up @@ -292,6 +314,13 @@ func (s *Scheduler) At(t string) *Scheduler {
return s
}

// SetTag will add tag when creating a job
func (s *Scheduler) SetTag(t []string) *Scheduler {
job := s.getCurrentJob()
job.tags = t
return s
}

// StartAt schedules the next run of the Job
func (s *Scheduler) StartAt(t time.Time) *Scheduler {
s.getCurrentJob().nextRun = t
Expand Down
34 changes: 34 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func TestScheduled(t *testing.T) {
}
}

func TestScheduledWithTag(t *testing.T) {
sched := NewScheduler(time.UTC)
customtag := []string{"mycustomtag"}
sched.Every(1).Hour().SetTag(customtag).Do(task)
if !sched.Scheduled(task) {
t.Fatal("Task was scheduled but function couldn't find it")
}
}

func TestStartImmediately(t *testing.T) {
sched := NewScheduler(time.UTC)
now := time.Now().UTC()
Expand Down Expand Up @@ -381,6 +390,31 @@ func TestRemoveByRef(t *testing.T) {
assert.ElementsMatch(t, []*Job{job2}, scheduler.Jobs())
}

func TestRemoveByTag(t *testing.T) {
scheduler := NewScheduler(time.UTC)

// Creating 2 Jobs with Unique tags
customtag1 := []string{"tag one"}
customtag2 := []string{"tag two"}
scheduler.Every(1).Minute().SetTag(customtag1).Do(taskWithParams, 1, "hello") // index 0
scheduler.Every(1).Minute().SetTag(customtag2).Do(taskWithParams, 2, "world") // index 1

assert.Equal(t, 2, scheduler.Len(), "Incorrect number of jobs")

// check Jobs()[0] tags is equal with tag "tag one" (customtag1)
assert.Equal(t, scheduler.Jobs()[0].Tags(), customtag1, "Job With Tag 'tag one' is removed from index 0")

scheduler.RemoveJobByTag("tag one")
assert.Equal(t, 1, scheduler.Len(), "Incorrect number of jobs after removing 1 job")

// check Jobs()[0] tags is equal with tag "tag two" (customtag2) after removing "tag one"
assert.Equal(t, scheduler.Jobs()[0].Tags(), customtag2, "Job With Tag 'tag two' is removed from index 0")

// Removing Non Existent Job with "tag one" because already removed above (will not removing any jobs because tag not match)
scheduler.RemoveJobByTag("tag one")
assert.Equal(t, 1, scheduler.Len(), "Incorrect number of jobs after removing non-existent job")
}

func TestJobs(t *testing.T) {
s := NewScheduler(time.UTC)
s.Every(1).Minute().Do(task)
Expand Down

0 comments on commit 8c7e3da

Please sign in to comment.