Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dynamic table columns, names or joins #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -377,10 +378,17 @@ func (co *Core) makeJob(j models.JobReq, taskName string) (tasqueue.Job, error)
args[i] = j.Args[i]
}

// DynamicParams
var dynamicParams = make([]interface{}, len(j.DynamicParams))
for i := range j.DynamicParams {
dynamicParams[i] = j.DynamicParams[i]
}

b, err := msgpack.Marshal(taskMeta{
Args: args,
DB: j.DB,
TTL: int(ttl),
Args: args,
DB: j.DB,
TTL: int(ttl),
DynamicParams: dynamicParams,
})
if err != nil {
return tasqueue.Job{}, err
Expand All @@ -395,9 +403,10 @@ func (co *Core) makeJob(j models.JobReq, taskName string) (tasqueue.Job, error)
}

type taskMeta struct {
Args []interface{} `json:"args"`
DB string `json:"db"`
TTL int `json:"ttl"`
Args []interface{} `json:"args"`
DB string `json:"db"`
TTL int `json:"ttl"`
DynamicParams []interface{} `json:"dynamic_params"`
}

// initQueue creates and returns a distributed queue system (Tasqueue) and registers
Expand Down Expand Up @@ -428,6 +437,12 @@ func (co *Core) initQueue() (*tasqueue.Server, error) {
return fmt.Errorf("could not unmarshal args : %w", err)
}

// replace dynamic params in query (args.DynamicParams)
// example dynamic columns, dynamic joins, dynamic table names
for i := 0; i < len(args.DynamicParams); i++ {
query.Raw = strings.Replace(query.Raw, "$"+strconv.Itoa(i+1), fmt.Sprintf("%v", args.DynamicParams[i]), -1)
}

count, err := co.execJob(jctx.Meta.ID, name, args.DB, time.Duration(args.TTL)*time.Second, args.Args, query)
if err != nil {
return fmt.Errorf("could not execute job : %w", err)
Expand Down
3 changes: 3 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type JobReq struct {
Args []string `json:"args"`
DB string `json:"db"`

// Optional params
DynamicParams []string `json:"dynamic_params"`

ttlDuration time.Duration
}

Expand Down