/
job.go
199 lines (170 loc) · 5.05 KB
/
job.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cli
import (
"fmt"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/cmdx"
"github.com/odpf/salt/log"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/config"
"github.com/odpf/siren/core/notification"
"github.com/odpf/siren/internal/jobs"
"github.com/odpf/siren/pkg/errors"
"github.com/odpf/siren/plugins/queues"
"github.com/odpf/siren/plugins/queues/postgresq"
"github.com/spf13/cobra"
)
func jobCmd(cmdxConfig *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "job <command>",
Aliases: []string{"jobs"},
Short: "Manage siren jobs",
Long: heredoc.Doc(`
Execute a siren's job.
A job is a task in Siren that could be executed and stopped once the task is done.
The Job is usually run as a CronJob to be executed on a specified time.
`),
Example: heredoc.Doc(`
$ siren job run cleanup_idempotency
$ siren job run cleanup_queue
`),
}
cmd.AddCommand(
jobRunCommand(cmdxConfig),
)
return cmd
}
func jobRunCommand(cmdxConfig *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Trigger a job",
Long: heredoc.Doc(`
Trigger a job
`),
Args: cobra.ExactValidArgs(1),
ValidArgs: []string{
"cleanup_queue",
"cleanup_idempotency",
},
Example: heredoc.Doc(`
$ siren job run cleanup_idempotency
$ siren job run cleanup_queue
`),
}
cmd.AddCommand(
jobRunCleanupQueueCommand(),
jobRunCleanupIdempotencyCommand(),
)
return cmd
}
func jobRunCleanupQueueCommand() *cobra.Command {
var (
configFile string
publishedDuration string
pendingDuration string
)
cmd := &cobra.Command{
Use: "cleanup_queue",
Short: "Cleanup stale messages in queue",
Long: heredoc.Doc(`
Cleaning up all published messages in queue with last updated
more than specific threshold (default 7 days) from now() and
(Optional) cleaning up all pending messages in queue with last updated
more than specific threshold (default 7 days) from now().
`),
Example: heredoc.Doc(`
$ siren job run cleanup_queue
`),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configFile)
if err != nil {
return err
}
var queue notification.Queuer
switch cfg.Notification.Queue.Kind {
case queues.KindPostgres:
queue, err = postgresq.New(log.NewZap(), cfg.DB)
if err != nil {
return err
}
default:
printer.Info("Cleanup queue job only works for postgres queue")
return nil
}
spinner := printer.Spin("")
defer spinner.Stop()
printer.Infof("Running job cleanup_queue(%s)\n", cfg.Notification.Queue.Kind.String())
if err := queue.Cleanup(cmd.Context(), queues.FilterCleanup{
MessagePendingTimeThreshold: pendingDuration,
MessagePublishedTimeThreshold: publishedDuration,
}); err != nil {
return err
}
spinner.Stop()
printer.Success(fmt.Sprintf("Job cleanup_queue(%s) finished", cfg.Notification.Queue.Kind.String()))
printer.Space()
printer.SuccessIcon()
return nil
},
}
cmd.Flags().StringVarP(&configFile, "config", "c", "config.yaml", "Config file path")
cmd.Flags().StringVarP(&publishedDuration, "published", "p", "168h", "Cleanup treshold for published messages in string (e.g. 10h, 30m)")
cmd.Flags().StringVarP(&publishedDuration, "pending", "s", "", "Cleanup treshold for pending messages in string (e.g. 10h, 30m)")
return cmd
}
func jobRunCleanupIdempotencyCommand() *cobra.Command {
var (
configFile string
ttlDuration string
)
cmd := &cobra.Command{
Use: "cleanup_idempotency",
Short: "Cleanup idempotencies outside TTL",
Long: heredoc.Doc(`
Cleaning up all idempotencies data outside TTL.
Default value is 24 hours.
`),
Example: heredoc.Doc(`
$ siren job run cleanup_idempotency
`),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(configFile)
if err != nil {
return err
}
logger := initLogger(cfg.Log)
apiDeps, _, pgClient, _, err := InitDeps(cmd.Context(), logger, cfg, nil)
if err != nil {
return err
}
spinner := printer.Spin("")
defer spinner.Stop()
printer.Infof("Running job cleanup_idempotency with ttl %s\n", ttlDuration)
ttlInTimeDuration, err := time.ParseDuration(ttlDuration)
if err != nil {
return err
}
jobHandler := jobs.NewHandler(logger, apiDeps.NotificationService)
if err := jobHandler.CleanupIdempotencies(cmd.Context(), ttlInTimeDuration); err != nil {
spinner.Stop()
if errors.Is(err, errors.ErrNotFound) {
printer.Success(fmt.Sprintf("No data outside TTL %s", ttlDuration))
} else {
logger.Error(err.Error())
}
} else {
spinner.Stop()
printer.Success("Job cleanup_idempotency finished")
printer.Space()
printer.SuccessIcon()
}
if err := pgClient.Close(); err != nil {
logger.Error(err.Error())
}
return nil
},
}
cmd.Flags().StringVarP(&configFile, "config", "c", "config.yaml", "Config file path")
cmd.Flags().StringVarP(&ttlDuration, "ttl", "t", "24h", "TTL duration of idempotency data in golang duration format (e.g. 10h, 30m)")
return cmd
}