-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
48 lines (37 loc) · 1.3 KB
/
job.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
47
48
package articleJob
import (
"context"
"go.uber.org/zap"
"github.com/robfig/cron/v3"
articleDomain "github.com/infranyx/go-microservice-template/internal/article/domain"
"github.com/infranyx/go-microservice-template/pkg/wrapper"
wrapperErrorhandler "github.com/infranyx/go-microservice-template/pkg/wrapper/handlers/error_handler"
wrapperRecoveryHandler "github.com/infranyx/go-microservice-template/pkg/wrapper/handlers/recovery_handler"
wrapperSentryHandler "github.com/infranyx/go-microservice-template/pkg/wrapper/handlers/sentry_handler"
cronJob "github.com/infranyx/go-microservice-template/pkg/cron"
)
type job struct {
cron *cron.Cron
logger *zap.Logger
}
func NewJob(logger *zap.Logger) articleDomain.Job {
newCron := cron.New(cron.WithChain(
cron.SkipIfStillRunning(cronJob.NewLogger()),
))
return &job{cron: newCron, logger: logger}
}
func (j *job) StartJobs(ctx context.Context) {
j.logArticleJob(ctx)
go j.cron.Start()
}
func (j *job) logArticleJob(ctx context.Context) {
worker := wrapper.BuildChain(j.logArticleWorker(),
wrapperSentryHandler.SentryHandler,
wrapperRecoveryHandler.RecoveryHandler,
wrapperErrorhandler.ErrorHandler,
)
entryId, _ := j.cron.AddFunc("*/1 * * * *",
worker.ToWorkerFunc(ctx, nil),
)
j.logger.Sugar().Infof("Article Job Started: %v", entryId)
}