-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicola Strappazzon C
committed
Jan 27, 2024
1 parent
ee23e72
commit 6f6452f
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package scheduler | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
temp "github.com/debeando/go-common/time" | ||
) | ||
|
||
type Scheduler struct { | ||
Enable bool `yaml:"enable"` | ||
Start string `yaml:"start"` | ||
End string `yaml:"end"` | ||
Interval uint16 `yaml:"interval"` | ||
start time.Time | ||
end time.Time | ||
} | ||
|
||
func (s *Scheduler) Do(start func(), end func()) (err error) { | ||
if !s.Enable { | ||
return errors.New("Scheduler is disable") | ||
} | ||
|
||
if s.start, err = temp.StringToTime(s.Start); err != nil { | ||
return err | ||
} | ||
|
||
if s.end, err = temp.StringToTime(s.End); err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
if temp.BetweenNow(s.start, s.end) { | ||
start() | ||
} else { | ||
end() | ||
} | ||
|
||
time.Sleep(time.Duration(s.Interval) * time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package scheduler_test | ||
|
||
import ( | ||
mock "time" | ||
|
||
"testing" | ||
|
||
"github.com/debeando/go-common/scheduler" | ||
"github.com/debeando/go-common/time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDo(t *testing.T) { | ||
s := scheduler.Scheduler{ | ||
Start: "07:00", | ||
End: "20:00", | ||
} | ||
|
||
start, _ := time.StringToTime(s.Start) | ||
end, _ := time.StringToTime(s.End) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 06, 59, 00, 000000000, mock.UTC) | ||
} | ||
assert.False(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 07, 00, 00, 000000000, mock.UTC) | ||
} | ||
assert.False(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 07, 01, 00, 000000000, mock.UTC) | ||
} | ||
assert.True(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 18, 45, 00, 000000000, mock.UTC) | ||
} | ||
assert.True(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 19, 59, 00, 000000000, mock.UTC) | ||
} | ||
assert.True(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 20, 00, 00, 000000000, mock.UTC) | ||
} | ||
assert.False(t, time.BetweenNow(start, end)) | ||
|
||
time.Now = func() mock.Time { | ||
return mock.Date(0, 1, 1, 20, 01, 00, 000000000, mock.UTC) | ||
} | ||
assert.False(t, time.BetweenNow(start, end)) | ||
} |
File renamed without changes.