-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (55 loc) · 1.35 KB
/
main.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
package main
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"github.com/altipla-consulting/errors"
"github.com/altipla-consulting/telemetry"
"github.com/altipla-consulting/telemetry/logging"
"github.com/altipla-consulting/telemetry/sentry"
)
func main() {
telemetry.Configure(sentry.Reporter(), logging.Standard())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()
if err := run(ctx); err != nil {
telemetry.ReportError(ctx, err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
u := os.Getenv("CRON_URL")
if u == "" {
return errors.New("CRON_URL env variable required")
}
token := os.Getenv("CRON_TOKEN")
slog.Info("Invoke cron", slog.String("url", u))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, nil)
if err != nil {
return errors.Trace(err)
}
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Trace(err)
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Trace(err)
}
fmt.Println(string(content))
if resp.StatusCode != http.StatusOK {
return errors.Errorf("cron unexpected status %v", resp.Status)
}
slog.Info("Cron invoked successfully!")
return nil
}