Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce test time, fix spelling, add Week() #125

Merged
merged 1 commit into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func task() {
fmt.Println("I am runnning task.")
fmt.Println("I am running task.")
}

func taskWithParams(a int, b string) {
Expand All @@ -53,12 +53,14 @@ func main() {
gocron.Every(2).Hours().Do(task)
gocron.Every(1).Day().Do(task)
gocron.Every(2).Days().Do(task)
gocron.Every(1).Week().Do(task)
gocron.Every(2).Weeks().Do(task)

// Do jobs on specific weekday
gocron.Every(1).Monday().Do(task)
gocron.Every(1).Thursday().Do(task)

// function At() take a string like 'hour:min'
// Do a job at a specific time - 'hour:min'
gocron.Every(1).Day().At("10:30").Do(task)
gocron.Every(1).Monday().At("18:30").Do(task)

Expand All @@ -69,14 +71,17 @@ func main() {
t := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.Local)
gocron.Every(1).Hour().From(&t).Do(task)

// remove, clear and next_run
// NextRun gets the next running time
_, time := gocron.NextRun()
fmt.Println(time)

// Remove a specific job
gocron.Remove(task)

// Clear all scheduled jobs
gocron.Clear()

// function Start start all the pending jobs
// Start all the pending jobs
<- gocron.Start()

// also, you can create a new scheduler
Expand Down
54 changes: 0 additions & 54 deletions example/example.go

This file was deleted.

28 changes: 17 additions & 11 deletions gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,53 +347,59 @@ func (j *Job) setUnit(unit string) *Job {

// Seconds set the unit with seconds
func (j *Job) Seconds() *Job {
return j.setUnit("seconds")
return j.setUnit(seconds)
}

// Minutes set the unit with minute
func (j *Job) Minutes() *Job {
return j.setUnit("minutes")
return j.setUnit(minutes)
}

// Hours set the unit with hours
func (j *Job) Hours() *Job {
return j.setUnit("hours")
return j.setUnit(hours)
}

// Days set the job's unit with days
func (j *Job) Days() *Job {
return j.setUnit("days")
return j.setUnit(days)
}

//Weeks sets the units as weeks
// Weeks sets the units as weeks
func (j *Job) Weeks() *Job {
return j.setUnit("weeks")
return j.setUnit(weeks)
}

// Second set the unit with second
// Second sets the unit with second
func (j *Job) Second() *Job {
j.mustInterval(1)
return j.Seconds()
}

// Minute set the unit with minute, which interval is 1
// Minute sets the unit with minute, which interval is 1
func (j *Job) Minute() *Job {
j.mustInterval(1)
return j.Minutes()
}

// Hour set the unit with hour, which interval is 1
// Hour sets the unit with hour, which interval is 1
func (j *Job) Hour() *Job {
j.mustInterval(1)
return j.Hours()
}

// Day set the job's unit with day, which interval is 1
// Day sets the job's unit with day, which interval is 1
func (j *Job) Day() *Job {
j.mustInterval(1)
return j.Days()
}

// Week sets the job's unit with week, which interval is 1
func (j *Job) Week() *Job {
j.mustInterval(1)
return j.Weeks()
}

// Weekday start job on specific Weekday
func (j *Job) Weekday(startDay time.Weekday) *Job {
j.mustInterval(1)
Expand Down Expand Up @@ -672,7 +678,7 @@ func Clear() {
defaultScheduler.Clear()
}

// Remove specific job
// Remove a specific job
func Remove(j interface{}) {
defaultScheduler.Remove(j)
}
Expand Down
12 changes: 6 additions & 6 deletions gocron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestSeconds(t *testing.T) {
func testJobWithInterval(t *testing.T, sched *Scheduler, job *Job, expectedTimeBetweenRuns int64) {
jobDone := make(chan bool)
executionTimes := make([]int64, 0)
numberOfIterations := 5
numberOfIterations := 2

job.Do(func() {
executionTimes = append(executionTimes, time.Now().Unix())
Expand All @@ -46,7 +46,7 @@ func testJobWithInterval(t *testing.T, sched *Scheduler, job *Job, expectedTimeB
<-jobDone // Wait job done
close(stop)

assert.Equal(t, numberOfIterations, len(executionTimes), "did not ran expected amount of times")
assert.Equal(t, numberOfIterations, len(executionTimes), "did not run expected number of times")

for i := 1; i < numberOfIterations; i++ {
durationBetweenExecutions := executionTimes[i] - executionTimes[i-1]
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestScheduled(t *testing.T) {
n := NewScheduler()
n.Every(1).Second().Do(task)
if !n.Scheduled(task) {
t.Fatal("Task was scheduled but function couldn't found it")
t.Fatal("Task was scheduled but function couldn't find it")
}
}

Expand Down Expand Up @@ -601,7 +601,7 @@ func TestLocker(t *testing.T) {
result := make([]lockerResult, 0)
task := func(key string, i int) {
s := time.Now()
time.Sleep(time.Millisecond * 1000)
time.Sleep(time.Millisecond * 100)
e := time.Now()
l.Lock()
result = append(result, lockerResult{
Expand All @@ -618,7 +618,7 @@ func TestLocker(t *testing.T) {
sync.Mutex{},
})

for i := 0; i < 20; i++ {
for i := 0; i < 5; i++ {
s1 := NewScheduler()
s1.Every(1).Seconds().Lock().Do(task, "A", i)

Expand All @@ -632,7 +632,7 @@ func TestLocker(t *testing.T) {
stop2 := s2.Start()
stop3 := s3.Start()

time.Sleep(time.Millisecond * 1500)
time.Sleep(time.Millisecond * 100)

close(stop1)
close(stop2)
Expand Down