-
Notifications
You must be signed in to change notification settings - Fork 0
/
fx_provider.go
46 lines (42 loc) · 963 Bytes
/
fx_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cccron
import (
"context"
"github.com/crosect/cc-go"
"github.com/crosect/cc-go/log"
"go.uber.org/fx"
)
func ProvideJob(job interface{}) fx.Option {
return fx.Provide(fx.Annotated{
Group: "cron_job",
Target: job,
})
}
func EnableCron() fx.Option {
return fx.Options(
ccgo.ProvideProps(NewJobsProperties),
fx.Provide(NewScheduler),
fx.Invoke(func(lc fx.Lifecycle, scheduler *Scheduler) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
scheduler.Start()
return nil
},
})
}),
)
}
func EnableGracefulShutdown() fx.Option {
return fx.Invoke(func(lc fx.Lifecycle, scheduler *Scheduler) {
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
log.Info("[cccron] Stopping Scheduler")
if err := scheduler.Stop(); err != nil {
log.Errorf("[cccron] Could not stop cron: %v", err)
return err
}
log.Info("[cccron] Scheduler Stopped")
return nil
},
})
})
}