blob: aea08efe7ee4978822453c717502634542f769ef (
plain) (
blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package rutina
import (
"context"
"log"
"os"
"syscall"
)
// Mixin interface
type Mixin interface {
apply(*Rutina)
}
// MixinContext propagates user defined context to rutina
type MixinContext struct {
Context context.Context
}
// WithContext propagates user defined context to rutina
func WithContext(context context.Context) *MixinContext {
return &MixinContext{Context: context}
}
func (o MixinContext) apply(r *Rutina) {
ctx, cancel := context.WithCancel(o.Context)
r.ctx = ctx
r.Cancel = cancel
}
// MixinLogger adds logger to rutina
type MixinLogger struct {
Logger *log.Logger
}
// WithLogger adds custom logger to rutina
func WithLogger(logger *log.Logger) *MixinLogger {
return &MixinLogger{Logger: logger}
}
// WithStdLogger adds standard logger to rutina
func WithStdLogger() *MixinLogger {
return &MixinLogger{Logger: log.New(os.Stdout, "[rutina]", log.LstdFlags)}
}
func (o MixinLogger) apply(r *Rutina) {
r.logger = o.Logger
}
// MixinErrChan turns on errors channel on rutina
type MixinErrChan struct {
}
// WithErrChan turns on errors channel on rutina
func WithErrChan() *MixinErrChan {
return &MixinErrChan{}
}
func (o MixinErrChan) apply(r *Rutina) {
r.errCh = make(chan error, 1)
}
type LifecycleListener func(event Event, routineID int)
type LifecycleMixin struct {
Listener LifecycleListener
}
func (l LifecycleMixin) apply(r *Rutina) {
r.lifecycleListener = l.Listener
}
func WithLifecycleListener(listener LifecycleListener) *LifecycleMixin {
return &LifecycleMixin{Listener: listener}
}
type ListenOsSignalsMixin struct {
Signals []os.Signal
}
func (l ListenOsSignalsMixin) apply(r *Rutina) {
r.autoListenSignals = l.Signals
}
func WithListenOsSignals(signals ...os.Signal) *ListenOsSignalsMixin {
if len(signals) == 0 {
signals = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}
return &ListenOsSignalsMixin{Signals: signals}
}
|