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

feat: optimize schedule #657

Merged
merged 3 commits into from
Sep 27, 2024
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
4 changes: 3 additions & 1 deletion contracts/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ type Route interface {
RunTLSWithCert(host, certFile, keyFile string) error
// ServeHTTP serves HTTP requests.
ServeHTTP(writer http.ResponseWriter, request *http.Request)
// Shutdown gracefully shuts down the serve.
// DEPRECATED use Stop instead.
Shutdown(ctx ...context.Context) error
// Stop gracefully stop the serve.
Stop(ctx ...context.Context) error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep consistence with Schedule.

}

type Router interface {
Expand Down
6 changes: 6 additions & 0 deletions contracts/schedule/schedule.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package schedule

import (
"context"
)

type Schedule interface {
// Call add a new callback event to the schedule.
Call(callback func()) Event
Expand All @@ -9,4 +13,6 @@ type Schedule interface {
Register(events []Event)
// Run schedules.
Run()
// Stop schedules.
Stop(ctx ...context.Context) error
}
59 changes: 59 additions & 0 deletions mocks/route/Route.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions mocks/schedule/Schedule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions schedule/application.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schedule

import (
"context"
"time"

"github.com/robfig/cron/v3"
Expand All @@ -24,8 +25,11 @@ func NewApplication(artisan console.Artisan, cache cache.Cache, log log.Log, deb
return &Application{
artisan: artisan,
cache: cache,
log: log,
debug: debug,
cron: cron.New(cron.WithParser(cron.NewParser(
cron.SecondOptional|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)), cron.WithLogger(NewLogger(log, debug))),
log: log,
debug: debug,
}
}

Expand All @@ -38,17 +42,30 @@ func (app *Application) Command(command string) schedule.Event {
}

func (app *Application) Register(events []schedule.Event) {
if app.cron == nil {
app.cron = cron.New(cron.WithLogger(NewLogger(app.log, app.debug)))
}

app.addEvents(events)
}

func (app *Application) Run() {
app.cron.Run()
}

func (app *Application) Stop(ctx ...context.Context) error {
if len(ctx) == 0 {
ctx = append(ctx, context.Background())
}

cronCtx := app.cron.Stop()

for {
select {
case <-cronCtx.Done():
return nil
case <-ctx[0].Done():
return ctx[0].Err()
}
}
}

func (app *Application) addEvents(events []schedule.Event) {
for _, event := range events {
chain := cron.NewChain(cron.Recover(NewLogger(app.log, app.debug)))
Expand Down
Loading
Loading