-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
53 lines (48 loc) · 1.52 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package worker
import (
"context"
"time"
"github.com/google/uuid"
)
// Service is an interface for a task manager
type Service interface {
// RegisterTask registers a new task to the worker
RegisterTask(ctx context.Context, task Task) error
// RegisterTasks registers multiple tasks to the worker
RegisterTasks(ctx context.Context, tasks ...Task)
// StartWorkers starts the task manager's workers
StartWorkers()
// Wait for all tasks to finish
Wait(timeout time.Duration)
// Stop the task manage
Stop()
// CancelAll cancels all tasks
CancelAll()
// CancelTask cancels a task by its ID
CancelTask(id uuid.UUID)
// GetActiveTasks returns the number of active tasks
GetActiveTasks() int
// StreamResults streams the `Result` channel
StreamResults() <-chan Result
// GetResults retruns the `Result` channel
GetResults() []Result
// GetCancelledTasks gets the cancelled tasks channel
GetCancelledTasks() <-chan Task
// GetTask gets a task by its ID
GetTask(id uuid.UUID) (task *Task, err error)
// GetTasks gets all tasks
GetTasks() []Task
// ExecuteTask executes a task given its ID and returns the result
ExecuteTask(id uuid.UUID, timeout time.Duration) (interface{}, error)
}
// Middleware describes a `Service` middleware.
type Middleware func(Service) Service
// RegisterMiddleware registers middlewares to the `Service`.
func RegisterMiddleware(svc Service, mw ...Middleware) Service {
// Register each middleware in the chain
for _, m := range mw {
svc = m(svc)
}
// Return the decorated service
return svc
}