Skip to content

Commit

Permalink
add do once job function
Browse files Browse the repository at this point in the history
  • Loading branch information
snowlyg committed Jun 28, 2022
1 parent e94c896 commit e10966b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/cron_server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cron_server

import (
"errors"
"fmt"
"sync"
"time"

"github.com/robfig/cron/v3"
)
Expand All @@ -24,3 +26,17 @@ func CronInstance() *cron.Cron {
})
return cc
}

// DoOnce run job once time,this job will run after 2 second
func DoOnce(job cron.Job, t ...time.Duration) error {
once := time.Now().Add(2 * time.Second)
if len(t) == 1 {
once = time.Now().Add(t[0] * time.Second)
}
onceSpec := fmt.Sprintf("%d %d %d %d %d %d", once.Second(), once.Minute(), once.Hour(), once.Day(), once.Month(), once.Weekday())
_, err := CronInstance().AddJob(onceSpec, job)
if err != nil {
return err
}
return nil
}

0 comments on commit e10966b

Please sign in to comment.