-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from cd1989/cron-trigger
Cron trigger
- Loading branch information
Showing
20 changed files
with
1,748 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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,58 @@ | ||
package trigger | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/robfig/cron" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type CronScheduler interface { | ||
Submit(task func()) | ||
Start() | ||
} | ||
|
||
type cronScheduler struct { | ||
passport chan struct{} | ||
cronStr string | ||
cron *cron.Cron | ||
} | ||
|
||
func NewCronScheduler(cronStr string) CronScheduler { | ||
passport := make(chan struct{}, 1) | ||
passport <- struct{}{} | ||
return &cronScheduler{ | ||
passport: passport, | ||
cronStr: cronStr, | ||
cron: cron.New(), | ||
} | ||
} | ||
|
||
func (s *cronScheduler) Submit(task func()) { | ||
s.cron.AddFunc(s.cronStr, func() { | ||
logrus.Info("Attempt to perform cleanup task") | ||
var granted bool | ||
select { | ||
case <-s.passport: | ||
granted = true | ||
case <-time.After(time.Second): | ||
granted = false | ||
} | ||
|
||
if !granted { | ||
logrus.Info("Another cleanup task is still running, skip") | ||
return | ||
} | ||
|
||
defer func() { | ||
s.passport <- struct{}{} | ||
}() | ||
|
||
logrus.Info("Start to run cleanup task") | ||
task() | ||
}) | ||
} | ||
|
||
func (s *cronScheduler) Start() { | ||
s.cron.Start() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.