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
|
||||
}
|
||||
|
||||
@@ -28,6 +28,6 @@ const (
|
||||
|
||||
const (
|
||||
QueueHigh = "high"
|
||||
QueueDefault = "default"
|
||||
QueueDefault = river.QueueDefault
|
||||
QueueLow = "low"
|
||||
)
|
||||
|
||||
@@ -44,4 +44,8 @@ func (c *Config) format() {
|
||||
if c.Reporter_CollectorEndpoint == "" {
|
||||
c.Reporter_CollectorEndpoint = "http://127.0.0.1:14268/api/traces"
|
||||
}
|
||||
|
||||
if c.Name == "" {
|
||||
c.Name = "default"
|
||||
}
|
||||
}
|
||||
@@ -34,8 +34,8 @@ func Provide(opts ...opt.Option) error {
|
||||
},
|
||||
Reporter: &config.ReporterConfig{
|
||||
LogSpans: true,
|
||||
LocalAgentHostPort: "127.0.0.1:6831",
|
||||
CollectorEndpoint: "http://127.0.0.1:14268/api/traces",
|
||||
LocalAgentHostPort: conf.Reporter_LocalAgentHostPort,
|
||||
CollectorEndpoint: conf.Reporter_CollectorEndpoint,
|
||||
BufferFlushInterval: 100 * time.Millisecond,
|
||||
QueueSize: 1000,
|
||||
},
|
||||
Reference in New Issue
Block a user