feat: support jobs

This commit is contained in:
Rogee
2024-12-30 17:37:36 +08:00
parent a3f7f566d7
commit 89fb7db933
5 changed files with 38 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package http
import ( import (
"{{.ModuleName}}/app/errorx" "{{.ModuleName}}/app/errorx"
"{{.ModuleName}}/app/jobs"
_ "{{.ModuleName}}/docs" _ "{{.ModuleName}}/docs"
"{{.ModuleName}}/pkg/service" "{{.ModuleName}}/pkg/service"
"{{.ModuleName}}/providers/app" "{{.ModuleName}}/providers/app"
@@ -26,6 +27,7 @@ func defaultProviders() container.Providers {
postgres.DefaultProvider(), postgres.DefaultProvider(),
jwt.DefaultProvider(), jwt.DefaultProvider(),
hashids.DefaultProvider(), hashids.DefaultProvider(),
job.DefaultProvider(),
}...) }...)
} }
@@ -34,7 +36,12 @@ func Command() atom.Option {
atom.Name("serve"), atom.Name("serve"),
atom.Short("run http server"), atom.Short("run http server"),
atom.RunE(Serve), atom.RunE(Serve),
atom.Providers(defaultProviders()), atom.Providers(
defaultProviders().
With(
jobs.Provide,
),
),
) )
} }
@@ -42,6 +49,7 @@ type Http struct {
dig.In dig.In
App *app.Config App *app.Config
Job *job.Job
Service *http.Service Service *http.Service
Initials []contracts.Initial `group:"initials"` Initials []contracts.Initial `group:"initials"`
Routes []contracts.HttpRoute `group:"routes"` Routes []contracts.HttpRoute `group:"routes"`

View File

@@ -12,6 +12,7 @@ import (
"git.ipao.vip/rogeecn/atom" "git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container" "git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts" "git.ipao.vip/rogeecn/atom/contracts"
"github.com/riverqueue/river"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.uber.org/dig" "go.uber.org/dig"
@@ -44,6 +45,7 @@ type Service struct {
App *app.Config App *app.Config
Job *job.Job Job *job.Job
Initials []contracts.Initial `group:"initials"` Initials []contracts.Initial `group:"initials"`
CronJobs []contracts.CronJob `group:"cron_jobs"`
} }
func Serve(cmd *cobra.Command, args []string) error { func Serve(cmd *cobra.Command, args []string) error {
@@ -59,6 +61,26 @@ func Serve(cmd *cobra.Command, args []string) error {
return err 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 { if err := client.Start(ctx); err != nil {
return err return err
} }

View File

@@ -28,6 +28,6 @@ const (
const ( const (
QueueHigh = "high" QueueHigh = "high"
QueueDefault = "default" QueueDefault = river.QueueDefault
QueueLow = "low" QueueLow = "low"
) )

View File

@@ -44,4 +44,8 @@ func (c *Config) format() {
if c.Reporter_CollectorEndpoint == "" { if c.Reporter_CollectorEndpoint == "" {
c.Reporter_CollectorEndpoint = "http://127.0.0.1:14268/api/traces" c.Reporter_CollectorEndpoint = "http://127.0.0.1:14268/api/traces"
} }
if c.Name == "" {
c.Name = "default"
}
} }

View File

@@ -34,8 +34,8 @@ func Provide(opts ...opt.Option) error {
}, },
Reporter: &config.ReporterConfig{ Reporter: &config.ReporterConfig{
LogSpans: true, LogSpans: true,
LocalAgentHostPort: "127.0.0.1:6831", LocalAgentHostPort: conf.Reporter_LocalAgentHostPort,
CollectorEndpoint: "http://127.0.0.1:14268/api/traces", CollectorEndpoint: conf.Reporter_CollectorEndpoint,
BufferFlushInterval: 100 * time.Millisecond, BufferFlushInterval: 100 * time.Millisecond,
QueueSize: 1000, QueueSize: 1000,
}, },