aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-08 01:07:15 +0900
committernsfisis <nsfisis@gmail.com>2024-08-08 04:02:37 +0900
commitd936d587df9d96f373cf8ec35f0c6c6882083f32 (patch)
tree691724bbb52674103b15a32d2e4cb60be64281c0 /backend
parent339f99a26191ede59a9eb0de2819cb5efdeb1535 (diff)
downloadiosdc-japan-2024-albatross-d936d587df9d96f373cf8ec35f0c6c6882083f32.tar.gz
iosdc-japan-2024-albatross-d936d587df9d96f373cf8ec35f0c6c6882083f32.tar.zst
iosdc-japan-2024-albatross-d936d587df9d96f373cf8ec35f0c6c6882083f32.zip
feat(backend): generate taskqueue/processor_wrapper.go by go generate
Diffstat (limited to 'backend')
-rw-r--r--backend/gen/gen.go1
-rw-r--r--backend/gen/taskqueue/processor_wrapper_gen.go132
-rw-r--r--backend/taskqueue/processor_wrapper.go2
3 files changed, 135 insertions, 0 deletions
diff --git a/backend/gen/gen.go b/backend/gen/gen.go
index 91b4550..276bc05 100644
--- a/backend/gen/gen.go
+++ b/backend/gen/gen.go
@@ -3,3 +3,4 @@ package gen
//go:generate go run github.com/sqlc-dev/sqlc/cmd/sqlc generate
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config oapi-codegen.yaml ../../openapi.yaml
//go:generate go run ./api/handler_wrapper_gen.go -i ../api/generated.go -o ../api/handler_wrapper.go
+//go:generate go run ./taskqueue/processor_wrapper_gen.go -i ../taskqueue/tasks.go -o ../taskqueue/processor_wrapper.go
diff --git a/backend/gen/taskqueue/processor_wrapper_gen.go b/backend/gen/taskqueue/processor_wrapper_gen.go
new file mode 100644
index 0000000..85960ae
--- /dev/null
+++ b/backend/gen/taskqueue/processor_wrapper_gen.go
@@ -0,0 +1,132 @@
+package main
+
+import (
+ "bytes"
+ "flag"
+ "go/ast"
+ "go/format"
+ "go/parser"
+ "go/token"
+ "os"
+ "slices"
+ "strings"
+ "text/template"
+)
+
+func main() {
+ inputFile := flag.String("i", "", "input file")
+ outputFile := flag.String("o", "", "output file")
+ flag.Parse()
+
+ if inputFile == nil || *inputFile == "" || outputFile == nil || *outputFile == "" {
+ flag.PrintDefaults()
+ os.Exit(1)
+ }
+
+ // Parse the input file
+ fileSet := token.NewFileSet()
+ parsedFile, err := parser.ParseFile(fileSet, *inputFile, nil, parser.SkipObjectResolution)
+ if err != nil {
+ panic(err)
+ }
+
+ // Find TaskType* constants.
+ var consts []string
+ for _, decl := range parsedFile.Decls {
+ genDecl, ok := decl.(*ast.GenDecl)
+ if !ok {
+ continue
+ }
+ for _, spec := range genDecl.Specs {
+ valueSpec, ok := spec.(*ast.ValueSpec)
+ if !ok {
+ continue
+ }
+ for _, name := range valueSpec.Names {
+ if !strings.HasPrefix(name.Name, "TaskType") {
+ continue
+ }
+ if name.Name == "TaskType" {
+ continue
+ }
+ consts = append(consts, strings.TrimPrefix(name.Name, "TaskType"))
+ }
+ }
+ }
+ if len(consts) == 0 {
+ panic("TaskType* constants not found")
+ }
+ slices.Sort(consts)
+
+ // Generate code.
+ tmpl, err := template.New("code").Parse(templateText)
+ if err != nil {
+ panic(err)
+ }
+
+ var buf bytes.Buffer
+ err = tmpl.Execute(&buf, consts)
+ if err != nil {
+ panic(err)
+ }
+
+ formatted, err := format.Source(buf.Bytes())
+ if err != nil {
+ panic(err)
+ }
+
+ err = os.WriteFile(*outputFile, formatted, 0644)
+ if err != nil {
+ panic(err)
+ }
+}
+
+const templateText = `// Code generated by go generate; DO NOT EDIT.
+
+package taskqueue
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ "github.com/hibiken/asynq"
+)
+
+type processorWrapper struct {
+ impl processor
+ results chan TaskResult
+}
+
+func newProcessorWrapper(impl processor) *processorWrapper {
+ return &processorWrapper{
+ impl: impl,
+ results: make(chan TaskResult),
+ }
+}
+
+{{ range . }}
+ func (p *processorWrapper) processTask{{ . }}(ctx context.Context, t *asynq.Task) error {
+ var payload TaskPayload{{ . }}
+ if err := json.Unmarshal(t.Payload(), &payload); err != nil {
+ err := fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
+ p.results <- &TaskResult{{ . }}{Err: err}
+ return err
+ }
+
+ result, err := p.impl.doProcessTask{{ . }}(ctx, &payload)
+ if err != nil {
+ retryCount, _ := asynq.GetRetryCount(ctx)
+ maxRetry, _ := asynq.GetMaxRetry(ctx)
+ isRecoverable := !errors.Is(err, asynq.SkipRetry) && retryCount < maxRetry
+ if !isRecoverable {
+ p.results <- &TaskResult{{ . }}{Err: err}
+ }
+ return err
+ }
+ p.results <- result
+ return nil
+ }
+{{ end }}
+`
diff --git a/backend/taskqueue/processor_wrapper.go b/backend/taskqueue/processor_wrapper.go
index 1ae9f2f..b1fbd16 100644
--- a/backend/taskqueue/processor_wrapper.go
+++ b/backend/taskqueue/processor_wrapper.go
@@ -1,3 +1,5 @@
+// Code generated by go generate; DO NOT EDIT.
+
package taskqueue
import (