-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtasks.go
More file actions
129 lines (117 loc) · 3.7 KB
/
tasks.go
File metadata and controls
129 lines (117 loc) · 3.7 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package scheduler
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/cybertec-postgresql/pg_timetable/internal/log"
"github.com/cybertec-postgresql/pg_timetable/internal/tasks"
)
// BuiltinTasks maps builtin task names with event handlers
var BuiltinTasks = map[string](func(context.Context, *Scheduler, string) (string, error)){
"NoOp": taskNoOp,
"Sleep": taskSleep,
"Log": taskLog,
"SendMail": taskSendMail,
"Download": taskDownload,
"CopyFromFile": taskCopyFromFile,
"CopyToFile": taskCopyToFile,
"Shutdown": taskShutdown}
func (sch *Scheduler) executeBuiltinTask(ctx context.Context, name string, paramValues []string) (stdout string, err error) {
var s string
f := BuiltinTasks[name]
if f == nil {
return "", errors.New("No built-in task found: " + name)
}
l := log.GetLogger(ctx)
l.WithField("name", name).Debugf("Executing builtin task with parameters %+q", paramValues)
if len(paramValues) == 0 {
return f(ctx, sch, "")
}
for _, val := range paramValues {
if s, err = f(ctx, sch, val); err != nil {
return
}
stdout += fmt.Sprintln(s)
}
return
}
func taskNoOp(_ context.Context, _ *Scheduler, val string) (stdout string, err error) {
return "NoOp task called with value: " + val, nil
}
func taskSleep(ctx context.Context, _ *Scheduler, val string) (stdout string, err error) {
var d int
if d, err = strconv.Atoi(val); err != nil {
return "", err
}
dur := time.Duration(d) * time.Second
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(dur):
return "Sleep task called for " + dur.String(), nil
}
}
func taskLog(ctx context.Context, _ *Scheduler, val string) (stdout string, err error) {
log.GetLogger(ctx).Print(val)
return "Logged: " + val, nil
}
func taskSendMail(ctx context.Context, _ *Scheduler, paramValues string) (stdout string, err error) {
conn := tasks.EmailConn{ServerPort: 587, ContentType: "text/plain"}
if err := json.Unmarshal([]byte(paramValues), &conn); err != nil {
return "", err
}
return "", tasks.SendMail(ctx, conn)
}
func taskCopyFromFile(ctx context.Context, sch *Scheduler, val string) (stdout string, err error) {
type copyFrom struct {
SQL string `json:"sql"`
Filename string `json:"filename"`
}
var ct copyFrom
if err := json.Unmarshal([]byte(val), &ct); err != nil {
return "", err
}
count, err := sch.pgengine.CopyFromFile(ctx, ct.Filename, ct.SQL)
if err == nil {
stdout = fmt.Sprintf("%d rows copied from %s", count, ct.Filename)
}
return stdout, err
}
func taskCopyToFile(ctx context.Context, sch *Scheduler, val string) (stdout string, err error) {
type copyTo struct {
SQL string `json:"sql"`
Filename string `json:"filename"`
}
var ct copyTo
if err := json.Unmarshal([]byte(val), &ct); err != nil {
return "", err
}
count, err := sch.pgengine.CopyToFile(ctx, ct.Filename, ct.SQL)
if err == nil {
stdout = fmt.Sprintf("%d rows copied to %s", count, ct.Filename)
}
return stdout, err
}
func taskDownload(ctx context.Context, _ *Scheduler, paramValues string) (stdout string, err error) {
type downloadOpts struct {
WorkersNum int `json:"workersnum"`
FileUrls []string `json:"fileurls"`
DestPath string `json:"destpath"`
}
var opts downloadOpts
if err := json.Unmarshal([]byte(paramValues), &opts); err != nil {
return "", err
}
if len(opts.FileUrls) == 0 {
return "", errors.New("files to download are not specified")
}
return tasks.DownloadUrls(ctx, opts.FileUrls, opts.DestPath, opts.WorkersNum)
}
func taskShutdown(_ context.Context, sch *Scheduler, val string) (stdout string, err error) {
sch.l.WithField("message", val).Info("Shutdown command received")
sch.Shutdown()
return "Shutdown task called", nil
}