2023-07-21 15:16:35 -06:00
|
|
|
package graphvent
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
)
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
var GQLMutationAbort = NewField(func()*graphql.Field {
|
|
|
|
gql_mutation_abort := &graphql.Field{
|
2023-07-21 17:49:19 -06:00
|
|
|
Type: GQLTypeSignal.Type,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
"id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
2023-07-21 15:16:35 -06:00
|
|
|
},
|
2023-07-21 17:49:19 -06:00
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
err = ctx.Server.Allowed("signal", "", ctx.User)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := ExtractID(p, "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var node Node = nil
|
2023-07-22 20:21:17 -06:00
|
|
|
err = UseStates(ctx.Context, ctx.User, NewLockMap(
|
|
|
|
NewLockInfo(ctx.Server, []string{"children"}),
|
|
|
|
), func(context *ReadContext) (error){
|
2023-07-22 21:24:54 -06:00
|
|
|
node = FindChild(context, ctx.User, ctx.Server, id)
|
2023-07-21 17:49:19 -06:00
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("Failed to find ID: %s as child of server thread", id)
|
|
|
|
}
|
2023-07-22 21:24:54 -06:00
|
|
|
return UseMoreStates(context, ctx.User, NewLockMap(NewLockInfo(node, []string{"signal"})), func(context *ReadContext) error {
|
|
|
|
return node.Signal(context, AbortSignal)
|
2023-07-22 20:21:17 -06:00
|
|
|
})
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return AbortSignal, nil
|
2023-07-21 17:49:19 -06:00
|
|
|
},
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
return gql_mutation_abort
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
var GQLMutationStartChild = NewField(func()*graphql.Field{
|
|
|
|
gql_mutation_start_child := &graphql.Field{
|
|
|
|
Type: GQLTypeSignal.Type,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
"parent_id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
2023-07-21 15:16:35 -06:00
|
|
|
},
|
2023-07-21 17:49:19 -06:00
|
|
|
"child_id": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
"action": &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
DefaultValue: "start",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2023-07-21 19:16:30 -06:00
|
|
|
ctx, err := PrepResolve(p)
|
2023-07-21 17:49:19 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_id, err := ExtractID(p, "parent_id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
child_id, err := ExtractID(p, "child_id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
action, err := ExtractParam[string](p, "action")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var signal GraphSignal
|
2023-07-22 20:21:17 -06:00
|
|
|
err = UseStates(ctx.Context, ctx.User, NewLockMap(
|
|
|
|
NewLockInfo(ctx.Server, []string{"children"}),
|
|
|
|
), func(context *ReadContext) error {
|
2023-07-22 21:24:54 -06:00
|
|
|
node := FindChild(context, ctx.User, ctx.Server, parent_id)
|
2023-07-21 17:49:19 -06:00
|
|
|
if node == nil {
|
|
|
|
return fmt.Errorf("Failed to find ID: %s as child of server thread", parent_id)
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
|
|
|
|
err := node.Allowed("signal", "", ctx.User)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-22 21:24:54 -06:00
|
|
|
return UseMoreStates(context, ctx.User, NewLockMap(NewLockInfo(node, []string{"start_child", "signal"})), func(context *ReadContext) error {
|
2023-07-22 20:21:17 -06:00
|
|
|
signal = NewStartChildSignal(child_id, action)
|
2023-07-22 21:24:54 -06:00
|
|
|
return node.Signal(context, signal)
|
2023-07-21 15:16:35 -06:00
|
|
|
})
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: wait for the result of the signal to send back instead of just the signal
|
|
|
|
return signal, nil
|
|
|
|
},
|
2023-07-21 15:16:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return gql_mutation_start_child
|
2023-07-21 17:49:19 -06:00
|
|
|
})
|
|
|
|
|