-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
36 lines (32 loc) · 997 Bytes
/
executor.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
package migration
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// Executor is a function that executes a migration task and returns any changes and if it succeeded.
type Executor func(ctx context.Context, task *Task) error
// CallbackExecutor calls the callback URL if it's set.
func CallbackExecutor(_ context.Context, task *Task) error {
if task.CallbackURL != nil {
payload, err := json.Marshal(task)
if err != nil {
return fmt.Errorf("failed to marshal migration callback payload: %w", err)
}
res, err := http.Post(*task.CallbackURL, "application/json", bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("failed to httpPost migration callback: %w", err)
}
if _, err = _successful(res); err != nil {
return fmt.Errorf("migration callback: %w", err)
}
}
return nil
}
// CompletedExecutor set Task.Status to StatusCompleted.
func CompletedExecutor(_ context.Context, task *Task) error {
task.Status = StatusCompleted
return nil
}