-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.go
89 lines (74 loc) · 2.22 KB
/
driver.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2018 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/robfig/cron"
"github.com/satori/go.uuid"
"github.com/vmware/dispatch/pkg/events"
"github.com/vmware/dispatch/pkg/events/driverclient"
)
var spec = flag.String("cron", "59 59 23 31 12 ? 2099", "cron expression for sending cloudevents on a schedule")
var debug = flag.Bool("debug", false, "Enable debug mode (print more information)")
var dryRun = flag.Bool("dryrun", false, "Enable dry run (does not send event")
var source = flag.String("source", uuid.NewV4().String(), "Set custom Source for the driver")
var gateway = flag.String(driverclient.DispatchEventsGatewayFlag, "", "events gateway")
func main() {
// Parse command line flags
flag.Parse()
// Get auth token
token := os.Getenv(driverclient.AuthToken)
// Use HTTP mode of sending events
client, err := driverclient.NewHTTPClient(driverclient.WithGateway(*gateway), driverclient.WithToken(token))
if err != nil {
panic(err)
}
sendFunc := func() {
ev := event()
if *debug {
log.Printf("Sending event %+v", *ev)
}
if !*dryRun {
err := client.SendOne(ev)
if err != nil {
log.Printf("Error sending event: %s", err)
}
}
}
log.Printf("Creating new scheduled event with cron spec %s", *spec)
c := cron.New()
err = c.AddFunc(*spec, sendFunc)
if err != nil {
panic(err)
}
c.Start()
// Catch SIGTERM signal to print nice shutdown message
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
// Run infinite loop, waiting ether for ticker or for termination signal.
// for every ticker tick, send an event (unless dry run mode is enabled).
for {
select {
case <-done:
log.Printf("Shutting down...")
c.Stop()
return
}
}
}
func event() *events.CloudEvent {
return &events.CloudEvent{
EventType: "cron.trigger",
CloudEventsVersion: events.CloudEventsVersion,
Source: *source,
EventID: uuid.NewV4().String(),
EventTime: time.Now(),
}
}