feat: update job template
This commit is contained in:
@@ -9,23 +9,23 @@ import (
|
||||
"go.ipao.vip/atom/contracts"
|
||||
)
|
||||
|
||||
var _ contracts.CronJob = (*CronJob)(nil)
|
||||
var _ contracts.CronJob = (*DemoCronJob)(nil)
|
||||
|
||||
// @provider(cronjob)
|
||||
type CronJob struct {
|
||||
type DemoCronJob struct {
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
// Prepare implements contracts.CronJob.
|
||||
func (CronJob) Prepare() error {
|
||||
func (DemoCronJob) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// JobArgs implements contracts.CronJob.
|
||||
func (CronJob) Args() []contracts.CronJobArg {
|
||||
func (DemoCronJob) Args() []contracts.CronJobArg {
|
||||
return []contracts.CronJobArg{
|
||||
{
|
||||
Arg: SortArgs{
|
||||
Arg: DemoJob{
|
||||
Strings: []string{"a", "b", "c", "d"},
|
||||
},
|
||||
|
||||
|
||||
@@ -12,36 +12,42 @@ import (
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
)
|
||||
|
||||
var _ contracts.JobArgs = SortArgs{}
|
||||
var _ contracts.JobArgs = DemoJob{}
|
||||
|
||||
type SortArgs struct {
|
||||
type DemoJob struct {
|
||||
Strings []string `json:"strings"`
|
||||
}
|
||||
|
||||
func (s SortArgs) InsertOpts() InsertOpts {
|
||||
func (s DemoJob) InsertOpts() InsertOpts {
|
||||
return InsertOpts{
|
||||
Queue: QueueDefault,
|
||||
Priority: PriorityDefault,
|
||||
}
|
||||
}
|
||||
|
||||
func (SortArgs) Kind() string { return "sort" }
|
||||
func (a SortArgs) UniqueID() string { return a.Kind() }
|
||||
func (DemoJob) Kind() string { return "demo_job" }
|
||||
func (a DemoJob) UniqueID() string { return a.Kind() }
|
||||
|
||||
var _ Worker[SortArgs] = (*SortWorker)(nil)
|
||||
var _ Worker[DemoJob] = (*SortWorker)(nil)
|
||||
|
||||
// @provider(job)
|
||||
type SortWorker struct {
|
||||
WorkerDefaults[SortArgs]
|
||||
type DemoJobWorker struct {
|
||||
WorkerDefaults[DemoJob]
|
||||
}
|
||||
|
||||
func (w *SortWorker) Work(ctx context.Context, job *Job[SortArgs]) error {
|
||||
sort.Strings(job.Args.Strings)
|
||||
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 {
|
||||
log = log.WithField("job", job.Kind())
|
||||
|
||||
log.Infof("[START] %s args: %v", job.Kind(), job.Args.Strings)
|
||||
defer log.Infof("[END] %s", job.Kind())
|
||||
|
||||
// modify below
|
||||
sort.Strings(job.Args.Strings)
|
||||
log.Infof("[%s] Sorted strings: %v\n", time.Now().Format(time.TimeOnly), job.Args.Strings)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *SortWorker) NextRetry(job *Job[SortArgs]) time.Time {
|
||||
return time.Now().Add(5 * time.Second)
|
||||
}
|
||||
|
||||
59
templates/project/app/jobs/demo_job_test.go.tpl
Normal file
59
templates/project/app/jobs/demo_job_test.go.tpl
Normal file
@@ -0,0 +1,59 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"{{.ModuleName}}/app/service/testx"
|
||||
"{{.ModuleName}}/providers/app"
|
||||
"{{.ModuleName}}/providers/job"
|
||||
|
||||
. "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
|
||||
Job *job.Job
|
||||
App *app.Config
|
||||
}
|
||||
|
||||
type DemoJobSuite struct {
|
||||
suite.Suite
|
||||
|
||||
DemoJobSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_DemoJob(t *testing.T) {
|
||||
providers := testx.Default().With(Provide, models.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{
|
||||
job: t.Job,
|
||||
app: t.App,
|
||||
}
|
||||
|
||||
err := worker.Work(context.Background(), job)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user