amoghrajesh commented on code in PR #50964:
URL: https://github.com/apache/airflow/pull/50964#discussion_r2103948250


##########
go-sdk/celery/app.go:
##########
@@ -0,0 +1,106 @@
+// 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 celery
+
+import (
+       "context"
+       "encoding/json"
+       "fmt"
+       "log/slog"
+       "os"
+       "os/signal"
+
+       celery "github.com/marselester/gopher-celery"
+       celeryredis "github.com/marselester/gopher-celery/goredis"
+       "github.com/redis/go-redis/v9"
+       "github.com/spf13/viper"
+
+       "github.com/apache/airflow/go-sdk/pkg/api"
+       "github.com/apache/airflow/go-sdk/pkg/sdkcontext"
+       "github.com/apache/airflow/go-sdk/worker"
+)
+
+func Run(ctx context.Context, config Config) error {
+       worker, ok := ctx.Value(sdkcontext.WorkerContextKey).(worker.Worker)
+       if !ok {
+               slog.ErrorContext(ctx, "Worker missing in context")
+               return fmt.Errorf("worker value missing in context")
+       }
+
+       worker, err := worker.WithServer(viper.GetString("execution-api-url"))
+       if err != nil {
+               slog.ErrorContext(ctx, "Error setting ExecutionAPI sxerver for 
worker", "err", err)
+               return err
+       }
+
+       // Store the updated worker in context
+       ctx = context.WithValue(ctx, sdkcontext.WorkerContextKey, worker)
+
+       ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
+       defer stop()
+
+       c := redis.NewClient(&redis.Options{
+               Addr: config.BrokerAddr,
+       })
+       defer func() {
+               if err := c.Close(); err != nil {
+                       slog.ErrorContext(ctx, "failed to close Redis client", 
"err", err)
+               }
+       }()
+
+       if _, err := c.Ping(ctx).Result(); err != nil {
+               slog.ErrorContext(ctx, "Redis connection failed", "err", err)
+               return err
+       }
+       broker := celeryredis.NewBroker(
+               celeryredis.WithClient(c),
+       )
+
+       if len(config.Queues) == 0 {
+               config.Queues = append(config.Queues, "default")
+       }
+       broker.Observe(config.Queues)
+
+       app := celery.NewApp(
+               celery.WithBroker(broker),
+       )
+
+       for _, queue := range config.Queues {
+               app.Register(
+                       "execute_workload",

Review Comment:
   Since this is a "unit of work" between tasks and the worker, extract this as 
a constant?



##########
go-sdk/README.md:
##########


Review Comment:
   ```
   airflow-sdk/
       go/
       python/
   ```
   
   I like this one better, but good call



##########
go-sdk/worker/registry.go:
##########
@@ -0,0 +1,98 @@
+// 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"
+       "strings"
+       "sync"
+)
+
+type (
+       // task is an interface of an task implementation.
+       Task interface {
+               Execute(ctx context.Context, logger *slog.Logger) error
+       }
+       registry struct {
+               sync.RWMutex
+               taskFuncMap map[string]map[string]Task
+       }
+)
+
+func newRegistry() *registry {
+       return &registry{taskFuncMap: make(map[string]map[string]Task)}
+}
+
+func getFnName(fn reflect.Value) string {
+       fullName := runtime.FuncForPC(fn.Pointer()).Name()
+       parts := strings.Split(fullName, ".")
+       fnName := parts[len(parts)-1]
+       // Go adds `-fm` suffix to a method names
+       return strings.TrimSuffix(fnName, "-fm")
+}
+
+func (r *registry) RegisterTask(dagId string, fn any) {

Review Comment:
   That would restrict users from writing tasks. For python tasks, we accept 
any function for `python_callabale` for example and we should let users 
flexibly do that in all other SDKs too.
   
   Reflect will allow to register any Go function as a task, then later invoke 
it using its name — even if the worker doesn’t know its exact type ahead of 
time.



##########
.editorconfig:
##########
@@ -58,3 +58,7 @@ indent_size = 2
 
 [*.json]
 indent_size = 4
+
+[*.go]
+indent_style = tab
+max_line_length = 110

Review Comment:
   We can add a `golangci-lint` precommit but that can come later for this



##########
go-sdk/sdk/variable.go:
##########
@@ -0,0 +1,43 @@
+// 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 sdk
+
+import (
+       "context"
+       "errors"
+       "fmt"
+
+       "github.com/apache/airflow/go-sdk/pkg/api"
+       "github.com/apache/airflow/go-sdk/pkg/sdkcontext"
+)
+
+func VariableGet(ctx context.Context, key string) (string, error) {
+       client := 
ctx.Value(sdkcontext.ApiClientContextKey).(api.ClientInterface)
+
+       resp, err := client.Variables().Get(ctx, key)
+       if err != nil {
+               var httpError *api.GeneralHTTPError
+               if errors.As(err, &httpError) && 
httpError.Response.StatusCode() == 404 {
+                       // TODO: return a custom error message!
+                       return "", fmt.Errorf("variable %q not found: %d", key, 
httpError.Response.StatusCode())
+               }
+               return "", err
+       }
+       // TODO: Handle deserialization etc!
+       return *resp.Value, nil
+}

Review Comment:
   We should instead make `Variable` as an interface, but something to that can 
be done in a follow up.
   
   ```
   type Variable interface {
        Get(ctx context.Context, key string) (string, error)
   }
   ```



##########
go-sdk/celery/cmd/root.go:
##########
@@ -0,0 +1,158 @@
+// 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 cmd
+
+import (
+       "context"
+       "fmt"
+       "os"
+       "path"
+       "strings"
+
+       cc "github.com/ivanpirog/coloredcobra"
+       "github.com/spf13/cobra"
+       "github.com/spf13/pflag"
+       "github.com/spf13/viper"
+
+       "github.com/apache/airflow/go-sdk/pkg/sdkcontext"
+       "github.com/apache/airflow/go-sdk/worker"
+)
+
+var cfgFile string
+
+// rootCmd represents the base command when called without any subcommands
+var rootCmd = &cobra.Command{
+       Use:   "airflow-go-sdk",

Review Comment:
   This is not exactly the "sdk" is it?
   
   Can we just call it "airflow-go-worker" instead?



-- 
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]

Reply via email to