feat: add new job cmd
This commit is contained in:
@@ -17,6 +17,7 @@ func CommandInit(root *cobra.Command) {
|
|||||||
CommandNewModule,
|
CommandNewModule,
|
||||||
CommandNewProvider,
|
CommandNewProvider,
|
||||||
CommandNewEvent,
|
CommandNewEvent,
|
||||||
|
CommandNewJob,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cmds {
|
for _, c := range cmds {
|
||||||
|
|||||||
81
cmd/new_job.go
Normal file
81
cmd/new_job.go
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"git.ipao.vip/rogeecn/atomctl/pkg/utils/gomod"
|
||||||
|
"git.ipao.vip/rogeecn/atomctl/templates"
|
||||||
|
"github.com/samber/lo"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommandNewProvider 注册 new_provider 命令
|
||||||
|
func CommandNewJob(root *cobra.Command) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "job",
|
||||||
|
Short: "创建新的 job",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: commandNewJobE,
|
||||||
|
}
|
||||||
|
|
||||||
|
root.AddCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandNewJobE(cmd *cobra.Command, args []string) error {
|
||||||
|
snakeName := lo.SnakeCase(args[0])
|
||||||
|
camelName := lo.PascalCase(args[0])
|
||||||
|
|
||||||
|
destPath := "app/jobs"
|
||||||
|
|
||||||
|
path, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
path, _ = filepath.Abs(path)
|
||||||
|
err = gomod.Parse(filepath.Join(path, "go.mod"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fs.WalkDir(templates.Jobs, "jobs", func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
destPath := filepath.Join(destPath, snakeName+".go")
|
||||||
|
tmpl, err := template.ParseFS(templates.Jobs, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
destFile, err := os.Create(destPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
return tmpl.Execute(destFile, map[string]string{
|
||||||
|
"Name": camelName,
|
||||||
|
"ModuleName": gomod.GetModuleName(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("job 已创建: %s\n", snakeName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
48
templates/jobs/job.go.tpl
Normal file
48
templates/jobs/job.go.tpl
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package jobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "git.ipao.vip/rogeecn/atom"
|
||||||
|
_ "git.ipao.vip/rogeecn/atom/contracts"
|
||||||
|
. "github.com/riverqueue/river"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ JobArgs = (*{{.Name}}Job)(nil)
|
||||||
|
_ JobArgsWithInsertOpts = (*{{.Name}}Job)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
type {{.Name}}Job struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertOpts implements JobArgsWithInsertOpts.
|
||||||
|
func (s {{.Name}}Job) InsertOpts() InsertOpts {
|
||||||
|
return InsertOpts{
|
||||||
|
Queue: QueueDefault,
|
||||||
|
Priority: PriorityDefault,
|
||||||
|
// UniqueOpts: UniqueOpts{
|
||||||
|
// ByArgs: true,
|
||||||
|
// },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ({{.Name}}Job) Kind() string {
|
||||||
|
return "{{.Name}}Job"
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Worker[{{.Name}}Job] = (*{{.Name}}JobWorker)(nil)
|
||||||
|
|
||||||
|
// @provider(job)
|
||||||
|
type {{.Name}}JobWorker struct {
|
||||||
|
WorkerDefaults[{{.Name}}Job]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *{{.Name}}JobWorker) NextRetry(job *Job[{{.Name}}Job]) time.Time {
|
||||||
|
return time.Now().Add(5 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *{{.Name}}JobWorker) Work(ctx context.Context, job *Job[{{.Name}}Job]) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -13,3 +13,6 @@ var Provider embed.FS
|
|||||||
|
|
||||||
//go:embed events
|
//go:embed events
|
||||||
var Events embed.FS
|
var Events embed.FS
|
||||||
|
|
||||||
|
//go:embed jobs
|
||||||
|
var Jobs embed.FS
|
||||||
|
|||||||
Reference in New Issue
Block a user