init
This commit is contained in:
36
backend/app/jobs/demo_cron.go
Normal file
36
backend/app/jobs/demo_cron.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
"github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
)
|
||||
|
||||
var _ contracts.CronJob = (*DemoCronJob)(nil)
|
||||
|
||||
// @provider(cronjob)
|
||||
type DemoCronJob struct {
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
// Prepare implements contracts.CronJob.
|
||||
func (DemoCronJob) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// JobArgs implements contracts.CronJob.
|
||||
func (DemoCronJob) Args() []contracts.CronJobArg {
|
||||
return []contracts.CronJobArg{
|
||||
{
|
||||
Arg: DemoJob{
|
||||
Strings: []string{"a", "b", "c", "d"},
|
||||
},
|
||||
|
||||
PeriodicInterval: PeriodicInterval(time.Second * 10),
|
||||
RunOnStart: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
53
backend/app/jobs/demo_job.go
Normal file
53
backend/app/jobs/demo_job.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
)
|
||||
|
||||
var _ contracts.JobArgs = DemoJob{}
|
||||
|
||||
type DemoJob struct {
|
||||
Strings []string `json:"strings"`
|
||||
}
|
||||
|
||||
func (s DemoJob) InsertOpts() InsertOpts {
|
||||
return InsertOpts{
|
||||
Queue: QueueDefault,
|
||||
Priority: PriorityDefault,
|
||||
}
|
||||
}
|
||||
|
||||
func (DemoJob) Kind() string { return "demo_job" }
|
||||
func (a DemoJob) UniqueID() string { return a.Kind() }
|
||||
|
||||
var _ Worker[DemoJob] = (*DemoJobWorker)(nil)
|
||||
|
||||
// @provider(job)
|
||||
type DemoJobWorker struct {
|
||||
WorkerDefaults[DemoJob]
|
||||
}
|
||||
|
||||
func (w *DemoJobWorker) NextRetry(job *Job[DemoJob]) time.Time {
|
||||
return time.Now().Add(30 * time.Second)
|
||||
}
|
||||
|
||||
func (w *DemoJobWorker) Work(ctx context.Context, job *Job[DemoJob]) error {
|
||||
logger := log.WithField("job", job.Args.Kind())
|
||||
|
||||
logger.Infof("[START] %s args: %v", job.Args.Kind(), job.Args.Strings)
|
||||
defer logger.Infof("[END] %s", job.Args.Kind())
|
||||
|
||||
// modify below
|
||||
sort.Strings(job.Args.Strings)
|
||||
logger.Infof("[%s] Sorted strings: %v\n", time.Now().Format(time.TimeOnly), job.Args.Strings)
|
||||
|
||||
return nil
|
||||
}
|
||||
56
backend/app/jobs/demo_job_test.go
Normal file
56
backend/app/jobs/demo_job_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
//go:build legacytests
|
||||
// +build legacytests
|
||||
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/services"
|
||||
|
||||
. "github.com/riverqueue/river"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type DemoJobSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type DemoJobSuite struct {
|
||||
suite.Suite
|
||||
|
||||
DemoJobSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_DemoJob(t *testing.T) {
|
||||
providers := testx.Default().With(Provide, services.Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p DemoJobSuiteInjectParams) {
|
||||
suite.Run(t, &DemoJobSuite{DemoJobSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DemoJobSuite) Test_Work() {
|
||||
Convey("test_work", t.T(), func() {
|
||||
Convey("step 1", func() {
|
||||
job := &Job[DemoJob]{
|
||||
Args: DemoJob{
|
||||
Strings: []string{"a", "b", "c"},
|
||||
},
|
||||
}
|
||||
|
||||
worker := &DemoJobWorker{}
|
||||
|
||||
err := worker.Work(context.Background(), job)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
41
backend/app/jobs/provider.gen.go
Executable file
41
backend/app/jobs/provider.gen.go
Executable file
@@ -0,0 +1,41 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"quyun/v2/providers/job"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
__job *job.Job,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &DemoCronJob{}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
container.Later(func() error { return __job.AddPeriodicJobs(obj) })
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
__job *job.Job,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &DemoJobWorker{}
|
||||
if err := river.AddWorkerSafely(__job.Workers, obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupInitial); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user