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

fix: go-sdk improvements and set default timeouts #64

Merged
merged 1 commit into from
Jan 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions api/v1/server/oas/transformers/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/internal/datautils"
"github.com/hatchet-dev/hatchet/internal/iter"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/dbsqlc"
"github.com/hatchet-dev/hatchet/internal/services/shared/defaults"
"github.com/hatchet-dev/hatchet/pkg/client/types"
)

Expand Down Expand Up @@ -228,6 +230,8 @@ func ToJob(job *db.JobModel) (*gen.Job, error) {

if timeout, ok := job.Timeout(); ok {
res.Timeout = &timeout
} else {
res.Timeout = repository.StringPtr(defaults.DefaultJobRunTimeout)
}

if steps := job.Steps(); steps != nil {
Expand Down Expand Up @@ -269,6 +273,8 @@ func ToStep(step *db.StepModel) *gen.Step {

if timeout, ok := step.Timeout(); ok {
res.Timeout = &timeout
} else {
res.Timeout = repository.StringPtr(defaults.DefaultStepRunTimeout)
}

if next, ok := step.NextID(); ok {
Expand Down
13 changes: 3 additions & 10 deletions internal/services/jobscontroller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hatchet-dev/hatchet/internal/datautils"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/services/shared/defaults"
"github.com/hatchet-dev/hatchet/internal/services/shared/tasktypes"
"github.com/hatchet-dev/hatchet/internal/taskqueue"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -1016,16 +1017,12 @@ func stepRunAssignedTask(tenantId, stepRunId string, worker *db.WorkerModel) *ta
}

func scheduleStepRunTimeoutTask(ticker *db.TickerModel, stepRun *db.StepRunModel) (*taskqueue.Task, error) {
var durationStr string
durationStr := defaults.DefaultStepRunTimeout

if timeout, ok := stepRun.Step().Timeout(); ok {
durationStr = timeout
}

if durationStr == "" {
durationStr = "300s"
}

// get a duration
duration, err := time.ParseDuration(durationStr)

Expand Down Expand Up @@ -1054,16 +1051,12 @@ func scheduleStepRunTimeoutTask(ticker *db.TickerModel, stepRun *db.StepRunModel
}

func scheduleJobRunTimeoutTask(ticker *db.TickerModel, jobRun *db.JobRunModel) (*taskqueue.Task, error) {
var durationStr string
durationStr := defaults.DefaultJobRunTimeout

if timeout, ok := jobRun.Job().Timeout(); ok {
durationStr = timeout
}

if durationStr == "" {
durationStr = "300s"
}

// get a duration
duration, err := time.ParseDuration(durationStr)

Expand Down
6 changes: 6 additions & 0 deletions internal/services/shared/defaults/timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package defaults

const (
DefaultJobRunTimeout = "300s"
DefaultStepRunTimeout = "300s"
)
8 changes: 5 additions & 3 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ func (w *Worker) registerAction(name string, method any) error {
return fmt.Errorf("could not get function from method: %w", err)
}

// ensure action has not been registered
if _, ok := w.actions[name]; ok {
return fmt.Errorf("action %s already registered", name)
// if action has already been registered, ensure that the method is the same
if currMethod, ok := w.actions[name]; ok {
if reflect.ValueOf(currMethod).Pointer() != reflect.ValueOf(method).Pointer() {
return fmt.Errorf("action %s is already registered with function %s", name, getFnName(currMethod.MethodFn()))
}
}

w.actions[name] = &actionImpl{
Expand Down
15 changes: 14 additions & 1 deletion pkg/worker/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Workflow struct {

type workflowFn struct {
Function any

name string
}

func Fn(f any) workflowFn {
Expand All @@ -72,12 +74,23 @@ func Fn(f any) workflowFn {
}
}

func (w workflowFn) SetName(name string) workflowFn {
w.name = name
return w
}

func (w workflowFn) ToWorkflow(svcName string) types.Workflow {
jobName := w.name

if jobName == "" {
jobName = getFnName(w.Function)
}
workflowJob := &WorkflowJob{
Name: getFnName(w.Function),
Name: jobName,
Steps: []WorkflowStep{
{
Function: w.Function,
ID: w.name,
},
},
}
Expand Down