|
|
@ -4,12 +4,13 @@ import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewTimer(baseDate time.Time, opts ...TimerOptions) StarTimer {
|
|
|
|
func NewTimer(baseDate time.Time, opts ...TimerOptions) (StarTimer, error) {
|
|
|
|
timer := StarTimer{
|
|
|
|
timer := StarTimer{
|
|
|
|
base: baseDate,
|
|
|
|
base: baseDate,
|
|
|
|
nextDate: time.Time{},
|
|
|
|
nextDate: time.Time{},
|
|
|
@ -57,6 +58,9 @@ func NewTimer(baseDate time.Time, opts ...TimerOptions) StarTimer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if op.repeats != nil {
|
|
|
|
if op.repeats != nil {
|
|
|
|
|
|
|
|
if err := timer.repeatsCheck(op.repeats); err != nil {
|
|
|
|
|
|
|
|
return timer, err
|
|
|
|
|
|
|
|
}
|
|
|
|
timer.repeat = append(timer.repeat, op.repeats)
|
|
|
|
timer.repeat = append(timer.repeat, op.repeats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if op.tasks != nil {
|
|
|
|
if op.tasks != nil {
|
|
|
@ -66,8 +70,43 @@ func NewTimer(baseDate time.Time, opts ...TimerOptions) StarTimer {
|
|
|
|
timer.runLimit = op.runLimit
|
|
|
|
timer.runLimit = op.runLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return timer
|
|
|
|
return timer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) repeatsCheck(rp *Repeats) error {
|
|
|
|
|
|
|
|
if rp == nil {
|
|
|
|
|
|
|
|
return errors.New("nil point of Repeats")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if rp.Every {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range rp.Repeat {
|
|
|
|
|
|
|
|
switch r.Unit {
|
|
|
|
|
|
|
|
case STAR_MINUTE, STAR_SECOND:
|
|
|
|
|
|
|
|
if r.Value > 60 || r.Value < 0 {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid value:%d", r.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case STAR_HOUR:
|
|
|
|
|
|
|
|
if r.Value > 24 || r.Value < 0 {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid value:%d", r.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case STAR_MONTH:
|
|
|
|
|
|
|
|
if r.Value > 12 || r.Value < 1 {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid value:%d", r.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case STAR_DAY:
|
|
|
|
|
|
|
|
if r.Value > 31 || r.Value < 1 {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid value:%d", r.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case STAR_WEEK:
|
|
|
|
|
|
|
|
if r.Value > 7 || r.Value < 0 {
|
|
|
|
|
|
|
|
return fmt.Errorf("invalid value:%d", r.Value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) IsRunning() bool {
|
|
|
|
func (t *StarTimer) IsRunning() bool {
|
|
|
|
t.mu.RLock()
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
@ -105,6 +144,9 @@ func (t *StarTimer) Stop() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) NextTimer() time.Time {
|
|
|
|
func (t *StarTimer) NextTimer() time.Time {
|
|
|
|
|
|
|
|
if t.base.Before(time.Now()) {
|
|
|
|
|
|
|
|
return t.parseNextDate(time.Now(), true)
|
|
|
|
|
|
|
|
}
|
|
|
|
return t.parseNextDate(t.base, true)
|
|
|
|
return t.parseNextDate(t.base, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -128,7 +170,7 @@ func (t *StarTimer) ImportRepeats(r string) error {
|
|
|
|
t.mu.Lock()
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if t.running {
|
|
|
|
if t.running {
|
|
|
|
return errors.New("coonot import repeats to already running timer")
|
|
|
|
return errors.New("cannot import repeats to already running timer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var rep []Repeats
|
|
|
|
var rep []Repeats
|
|
|
|
err := json.Unmarshal([]byte(r), &rep)
|
|
|
|
err := json.Unmarshal([]byte(r), &rep)
|
|
|
@ -136,6 +178,11 @@ func (t *StarTimer) ImportRepeats(r string) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.repeat = make([]*Repeats, 0, len(rep))
|
|
|
|
t.repeat = make([]*Repeats, 0, len(rep))
|
|
|
|
|
|
|
|
for _, v := range rep {
|
|
|
|
|
|
|
|
if err = t.repeatsCheck(&v); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for _, v := range rep {
|
|
|
|
for _, v := range rep {
|
|
|
|
t.repeat = append(t.repeat, &v)
|
|
|
|
t.repeat = append(t.repeat, &v)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -149,9 +196,21 @@ func (t *StarTimer) SetBaseDate(date time.Time) {
|
|
|
|
t.base = date
|
|
|
|
t.base = date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) StaticMode() bool {
|
|
|
|
|
|
|
|
return t.staticMode
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *StarTimer) SetStaticMode(s bool) {
|
|
|
|
|
|
|
|
t.staticMode = s
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) ResetWithRepeat(base time.Time, repeat []*Repeats) error {
|
|
|
|
func (t *StarTimer) ResetWithRepeat(base time.Time, repeat []*Repeats) error {
|
|
|
|
t.Stop()
|
|
|
|
t.Stop()
|
|
|
|
t.base = base
|
|
|
|
t.base = base
|
|
|
|
|
|
|
|
for _, v := range repeat {
|
|
|
|
|
|
|
|
if err := t.repeatsCheck(v); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
t.repeat = repeat
|
|
|
|
t.repeat = repeat
|
|
|
|
return t.Run()
|
|
|
|
return t.Run()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -160,13 +219,74 @@ func (t *StarTimer) Repeats() []*Repeats {
|
|
|
|
return t.repeat
|
|
|
|
return t.repeat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) runAsStaticMode() error {
|
|
|
|
|
|
|
|
t.mu.Lock()
|
|
|
|
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
if t.running {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range t.repeat {
|
|
|
|
|
|
|
|
if err := t.repeatsCheck(v); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.base = time.Now()
|
|
|
|
|
|
|
|
base := t.base
|
|
|
|
|
|
|
|
t.nextDate = t.parseNextDate(base, false)
|
|
|
|
|
|
|
|
if t.nextDate.Before(time.Now()) {
|
|
|
|
|
|
|
|
return errors.New("Invalid Timer Options,Please Check")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.running = true
|
|
|
|
|
|
|
|
t.stopCtx, t.stopFn = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if t.runLimit > 0 && t.runCount >= t.runLimit {
|
|
|
|
|
|
|
|
t.Stop()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
t.nextDate = t.parseNextDate(now, false)
|
|
|
|
|
|
|
|
if t.nextDate.Before(now) || t.runLimit > 0 && t.runCount+1 >= t.runLimit {
|
|
|
|
|
|
|
|
t.Stop()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mu.Lock()
|
|
|
|
|
|
|
|
t.timer = time.NewTimer(t.nextDate.Sub(now))
|
|
|
|
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-t.timer.C:
|
|
|
|
|
|
|
|
t.runCount++
|
|
|
|
|
|
|
|
for _, fn := range t.tasks {
|
|
|
|
|
|
|
|
go fn()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !t.running {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-t.stopCtx.Done():
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (t *StarTimer) Run() error {
|
|
|
|
func (t *StarTimer) Run() error {
|
|
|
|
|
|
|
|
if t.staticMode {
|
|
|
|
|
|
|
|
return t.runAsStaticMode()
|
|
|
|
|
|
|
|
}
|
|
|
|
t.mu.Lock()
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if t.running {
|
|
|
|
if t.running {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.nextDate = t.parseNextDate(time.Now(), false)
|
|
|
|
for _, v := range t.repeat {
|
|
|
|
|
|
|
|
if err := t.repeatsCheck(v); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
base := time.Now()
|
|
|
|
|
|
|
|
if t.base.After(base) {
|
|
|
|
|
|
|
|
base = t.base
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.nextDate = t.parseNextDate(base, false)
|
|
|
|
if t.nextDate.Before(time.Now()) {
|
|
|
|
if t.nextDate.Before(time.Now()) {
|
|
|
|
return errors.New("Invalid Timer Options,Please Check")
|
|
|
|
return errors.New("Invalid Timer Options,Please Check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|