Skip to content
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
3 changes: 2 additions & 1 deletion tavern/internal/ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tavern/internal/ent/gql_collection.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tavern/internal/ent/gql_pagination.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions tavern/internal/ent/gql_where_input.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tavern/internal/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 92 additions & 2 deletions tavern/internal/ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tavern/internal/ent/runtime/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions tavern/internal/ent/schema/task.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package schema

import (
"context"
"fmt"

"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"realm.pub/tavern/internal/ent/hook"
)

// Task holds the schema definition for the Task entity.
Expand Down Expand Up @@ -37,6 +41,13 @@ func (Task) Fields() []ent.Field {
field.Text("output").
Optional().
Comment("Output from executing the task"),
field.Int("output_size").
Default(0).
Min(0).
Annotations(
entgql.OrderField("OUTPUT_SIZE"),
).
Comment("The size of the output in bytes"),
field.String("error").
Optional().
Comment("Error, if any, produced while executing the Task"),
Expand Down Expand Up @@ -70,3 +81,36 @@ func (Task) Mixin() []ent.Mixin {
MixinHistory{}, // created_at, last_modified_at
}
}

// Hooks defines middleware for mutations for the ent.
func (Task) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(HookDeriveTaskInfo(), ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne),
}
}

// HookDeriveTaskInfo will update task info (e.g. output_size) whenever it is mutated.
func HookDeriveTaskInfo() ent.Hook {
// Get the relevant methods from the Task Mutation
// See this example: https://github.com/ent/ent/blob/master/entc/integration/hooks/ent/schema/user.go#L98
type tMutation interface {
Output() (string, bool)
SetOutputSize(i int)
}

return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
// Get the mutation
t, ok := m.(tMutation)
if !ok {
return nil, fmt.Errorf("expected task mutation in schema hook, got: %+v", m)
}

// Set the new size
output, _ := t.Output()
t.SetOutputSize(len([]byte(output)))

return next.Mutate(ctx, m)
})
}
}
Loading