Files
atomctl/templates/project/app/service/queue/river.go.tpl
2024-12-31 15:46:13 +08:00

95 lines
1.9 KiB
Smarty

package queue
import (
"context"
"{{.ModuleName}}/app/jobs"
"{{.ModuleName}}/pkg/service"
"{{.ModuleName}}/providers/app"
"{{.ModuleName}}/providers/job"
"{{.ModuleName}}/providers/postgres"
"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"
)
func defaultProviders() container.Providers {
return service.Default(container.Providers{
postgres.DefaultProvider(),
job.DefaultProvider(),
}...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("queue"),
atom.Short("start queue processor"),
atom.RunE(Serve),
atom.Providers(
defaultProviders().
With(
jobs.Provide,
),
),
)
}
type Service struct {
dig.In
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 {
return container.Container.Invoke(func(ctx context.Context, svc Service) error {
log.SetFormatter(&log.JSONFormatter{})
if svc.App.IsDevMode() {
log.SetLevel(log.DebugLevel)
}
client, err := svc.Job.Client()
if err != nil {
return err
}
for _, cronJob := range svc.CronJobs {
log.
WithField("module", "cron").
WithField("name", cronJob.Description()).
WithField("duration", cronJob.Periodic().Seconds()).
Info("registering cron job")
for _, jobArgs := range cronJob.JobArgs() {
client.PeriodicJobs().Add(
river.NewPeriodicJob(
river.PeriodicInterval(cronJob.Periodic()),
func() (river.JobArgs, *river.InsertOpts) {
return jobArgs, cronJob.InsertOpts()
},
&river.PeriodicJobOpts{
RunOnStart: cronJob.RunOnStart(),
},
),
)
}
}
if err := client.Start(ctx); err != nil {
return err
}
defer client.StopAndCancel(ctx)
<-ctx.Done()
return nil
})
}