feat: support jobs
This commit is contained in:
@@ -2,6 +2,7 @@ package http
|
||||
|
||||
import (
|
||||
"{{.ModuleName}}/app/errorx"
|
||||
"{{.ModuleName}}/app/jobs"
|
||||
_ "{{.ModuleName}}/docs"
|
||||
"{{.ModuleName}}/pkg/service"
|
||||
"{{.ModuleName}}/providers/app"
|
||||
@@ -26,6 +27,7 @@ func defaultProviders() container.Providers {
|
||||
postgres.DefaultProvider(),
|
||||
jwt.DefaultProvider(),
|
||||
hashids.DefaultProvider(),
|
||||
job.DefaultProvider(),
|
||||
}...)
|
||||
}
|
||||
|
||||
@@ -34,7 +36,12 @@ func Command() atom.Option {
|
||||
atom.Name("serve"),
|
||||
atom.Short("run http server"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(defaultProviders()),
|
||||
atom.Providers(
|
||||
defaultProviders().
|
||||
With(
|
||||
jobs.Provide,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -42,6 +49,7 @@ type Http struct {
|
||||
dig.In
|
||||
|
||||
App *app.Config
|
||||
Job *job.Job
|
||||
Service *http.Service
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Routes []contracts.HttpRoute `group:"routes"`
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
@@ -44,6 +45,7 @@ type Service struct {
|
||||
App *app.Config
|
||||
Job *job.Job
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
CronJobs []contracts.CronJob `group:"cron_jobs"`
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
@@ -59,6 +61,26 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cronJob := range svc.CronJobs {
|
||||
log.
|
||||
WithField("module", "cron").
|
||||
WithField("name", cronJob.Description()).
|
||||
WithField("duration", cronJob.Periodic().Seconds()).
|
||||
Info("registering cron job")
|
||||
|
||||
client.PeriodicJobs().Add(river.NewPeriodicJob(
|
||||
river.PeriodicInterval(cronJob.Periodic()),
|
||||
func() (river.JobArgs, *river.InsertOpts) {
|
||||
return cronJob.JobArgs(), cronJob.InsertOpts()
|
||||
},
|
||||
&river.PeriodicJobOpts{
|
||||
RunOnStart: cronJob.RunOnStart(),
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
if err := client.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user