feat: add context parameter to Task.Execute method
All checks were successful
Go Tests / Run Tests (1.24.2) (push) Successful in 18s

This commit is contained in:
Jeremy Tregunna 2025-04-18 15:46:43 -06:00
parent 8c0f9deaf1
commit 990c12b3c1
Signed by: jer
GPG Key ID: 1278B36BA6F5D5E4
3 changed files with 12 additions and 8 deletions

View File

@ -28,6 +28,7 @@ go get git.canoozie.net/jer/task
package main package main
import ( import (
"context"
"fmt" "fmt"
"time" "time"
@ -45,7 +46,7 @@ func (t *MyTask) ID() string {
return t.TaskID return t.TaskID
} }
func (t *MyTask) Execute() error { func (t *MyTask) Execute(ctx context.Context) error {
fmt.Printf("Executing task: %s\n", t.Name) fmt.Printf("Executing task: %s\n", t.Name)
return nil return nil
} }
@ -135,13 +136,13 @@ To create a task, implement the `Task` interface:
```go ```go
type Task interface { type Task interface {
ID() string ID() string
Execute() error Execute(ctx context.Context) error
Dependencies() []string Dependencies() []string
} }
``` ```
- `ID()` - Returns a unique identifier for this task - `ID()` - Returns a unique identifier for this task
- `Execute()` - Performs the task's operation, returning any errors - `Execute(ctx context.Context)` - Performs the task's operation with context, returning any errors
- `Dependencies()` - Returns a list of task IDs that must complete successfully before this task can run - `Dependencies()` - Returns a list of task IDs that must complete successfully before this task can run
Any errors returned from `Execute()` will be logged and will prevent dependent tasks from running. Any errors returned from `Execute()` will be logged and will prevent dependent tasks from running.
@ -154,7 +155,7 @@ Any errors returned from `Execute()` will be logged and will prevent dependent t
```go ```go
type Task interface { type Task interface {
ID() string ID() string
Execute() error Execute(ctx context.Context) error
Dependencies() []string Dependencies() []string
} }
``` ```

View File

@ -1,6 +1,7 @@
package task package task
import ( import (
"context"
"log" "log"
"sync" "sync"
"time" "time"
@ -8,7 +9,7 @@ import (
type Task interface { type Task interface {
ID() string ID() string
Execute() error Execute(ctx context.Context) error
Dependencies() []string Dependencies() []string
} }
@ -346,8 +347,9 @@ func (te *TaskExecutor) executeTask(st *ScheduledTask) {
} }
te.completedTasksMutex.Unlock() te.completedTasksMutex.Unlock()
// Execute the task // Execute the task with a background context
err := st.task.Execute() ctx := context.Background()
err := st.task.Execute(ctx)
// Update final task status // Update final task status
te.completedTasksMutex.Lock() te.completedTasksMutex.Lock()

View File

@ -1,6 +1,7 @@
package task package task
import ( import (
"context"
"errors" "errors"
"testing" "testing"
"time" "time"
@ -16,7 +17,7 @@ func (m *mockTask) ID() string {
return m.id return m.id
} }
func (m *mockTask) Execute() error { func (m *mockTask) Execute(ctx context.Context) error {
return m.executeFunc() return m.executeFunc()
} }