uranusjr commented on code in PR #77489:
URL: https://github.com/apache/airflow/pull/77489#discussion_r4734952526


##########
go-sdk/pkg/execution/execution_timeout.go:
##########
@@ -19,17 +19,32 @@ package execution
 
 import (
        "context"
+       "errors"
        "fmt"
+       "log/slog"
+       "math"
+       "time"
 
        "github.com/apache/airflow/go-sdk/pkg/execution/genmodels"
+       "github.com/apache/airflow/go-sdk/sdk"
 )
 
-// resolveExecutionTimeout returns the task's execution_timeout in seconds 
from ti_context, or nil
-// when the task has no timeout. The coordinator has already filled in the 
worker's default.
-// As in the Python task runner, zero means no timeout and a negative value 
has already elapsed.
-func resolveExecutionTimeout(fromContext any) (*float64, error) {
+// executionTimeout is a task's execution_timeout. Call start before run.

Review Comment:
   Consider folding `start` into `run` to avoid manual enforcement. The 
`SetExecutionTimeout` failure can be handled inside.



##########
go-sdk/pkg/execution/execution_timeout.go:
##########
@@ -19,17 +19,32 @@ package execution
 
 import (
        "context"
+       "errors"
        "fmt"
+       "log/slog"
+       "math"
+       "time"
 
        "github.com/apache/airflow/go-sdk/pkg/execution/genmodels"
+       "github.com/apache/airflow/go-sdk/sdk"
 )
 
-// resolveExecutionTimeout returns the task's execution_timeout in seconds 
from ti_context, or nil
-// when the task has no timeout. The coordinator has already filled in the 
worker's default.
-// As in the Python task runner, zero means no timeout and a negative value 
has already elapsed.
-func resolveExecutionTimeout(fromContext any) (*float64, error) {
+// executionTimeout is a task's execution_timeout. Call start before run.
+type executionTimeout interface {
+       start(ctx context.Context, client *CoordinatorClient) error
+       run(
+               stop context.Context,
+               shouldRetry bool,
+               logger *slog.Logger,
+               run func(ctx context.Context) any,
+       ) any
+}

Review Comment:
   The null object takes in parameters that only one implementation uses. Those 
belong to the enabled timeout, not the interface. Set them on 
`enabledExecutionTimeout` when it is built, and narrow the interface to:
   
   ```go
   run(stop context.Context, task func(ctx context.Context) any) any
   ```



##########
go-sdk/pkg/execution/execution_timeout.go:
##########
@@ -39,17 +54,91 @@ func resolveExecutionTimeout(fromContext any) (*float64, 
error) {
                        fromContext,
                )
        case seconds == 0:
-               return nil, nil
+               return disabledExecutionTimeout{}, nil
        case seconds < 0:
                return nil, fmt.Errorf("execution_timeout of %g seconds has 
already elapsed", seconds)
        }
-       return &seconds, nil
+       return enabledExecutionTimeout{seconds: seconds}, nil
+}
+
+type disabledExecutionTimeout struct{}
+
+func (disabledExecutionTimeout) start(context.Context, *CoordinatorClient) 
error {
+       return nil
 }
 
-// setExecutionTimeout tells the supervisor the task's execution_timeout and 
restarts its clock from
-// now. The runtime does not stop the task itself: killed_task_cleanup_time 
after the timeout, the
-// supervisor sends SIGTERM, which cancels the task's ctx.
-func (c *CoordinatorClient) setExecutionTimeout(ctx context.Context, seconds 
float64) error {
-       _, err := c.comm.Communicate(ctx, 
genmodels.SetExecutionTimeout{TimeoutSeconds: seconds})
+func (disabledExecutionTimeout) run(
+       stop context.Context,
+       _ bool,
+       _ *slog.Logger,
+       run func(ctx context.Context) any,
+) any {
+       return run(stop)
+}
+
+type enabledExecutionTimeout struct {
+       seconds float64
+}
+
+// start arms the supervisor, which sends SIGTERM killed_task_cleanup_time 
after the timeout.
+func (t enabledExecutionTimeout) start(ctx context.Context, client 
*CoordinatorClient) error {
+       _, err := client.comm.Communicate(ctx, 
genmodels.SetExecutionTimeout{TimeoutSeconds: t.seconds})
        return err
 }
+
+// run cancels the task's ctx with sdk.TaskTimeout at the timeout, then waits 
until the task
+// returns or the supervisor's SIGTERM cancels stop. The task fails with the 
timeout either way.
+func (t enabledExecutionTimeout) run(
+       stop context.Context,
+       shouldRetry bool,
+       logger *slog.Logger,
+       run func(ctx context.Context) any,

Review Comment:
   Awkward an argument to function `run` is also named `run`. Alternative names 
possible?



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