ashb commented on code in PR #50964: URL: https://github.com/apache/airflow/pull/50964#discussion_r2104651386
########## go-sdk/worker/task.go: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package worker + +import ( + "context" + "fmt" + "log/slog" + "reflect" + "runtime" +) + +type taskFunction struct { + fn reflect.Value + fullName string +} + +var _ Task = (*taskFunction)(nil) + +func NewTaskFunction(fn any) (Task, error) { + v := reflect.ValueOf(fn) + fullName := runtime.FuncForPC(v.Pointer()).Name() + f := &taskFunction{v, fullName} + return f, f.validateFn(v.Type()) +} + +func (f *taskFunction) Execute(ctx context.Context, logger *slog.Logger) error { + fnType := f.fn.Type() + + reflectArgs := make([]reflect.Value, fnType.NumIn()) + for i := range reflectArgs { + in := fnType.In(i) + + switch { + case isContext(in): + reflectArgs[i] = reflect.ValueOf(ctx) + case isLogger(in): + reflectArgs[i] = reflect.ValueOf(logger) + default: + // TODO: deal with other value types. For now they will all be Zero values unless it's a context + reflectArgs[i] = reflect.Zero(in) + } + } + slog.Debug("Attempting to call fn", "fn", f.fn, "args", reflectArgs) + retValues := f.fn.Call(reflectArgs) + + var err error + if errResult := retValues[len(retValues)-1].Interface(); errResult != nil { + var ok bool + if err, ok = errResult.(error); !ok { + return fmt.Errorf( + "failed to extract task error result as it is not of error interface: %v", + errResult, + ) + } + } + // If there are two results, convert the first only if it's not a nil pointer + var res interface{} + if len(retValues) > 1 && (retValues[0].Kind() != reflect.Ptr || !retValues[0].IsNil()) { + res = retValues[0].Interface() + } + _ = res Review Comment: Yeah, and dealing with res at all is to start dealing with sending the result to xcom. I'll add a todo comment here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
