-
Notifications
You must be signed in to change notification settings - Fork 21
/
events.go
123 lines (105 loc) · 3.29 KB
/
events.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package v1alpha2
import (
"encoding/json"
"time"
)
type Event struct {
Metadata map[string]string `json:"metadata"`
Body interface{} `json:"body"`
}
func (e Event) MarshalBinary() (data []byte, err error) {
return json.Marshal(e)
}
type EventHandler func(topic string, message Event) error
type JobAction string
const (
JobUpdate JobAction = "UPDATE"
JobDelete JobAction = "DELETE"
JobRun JobAction = "RUN"
)
type JobData struct {
Id string `json:"id"`
Scope string `json:"scope,omitempty"`
Action JobAction `json:"action"`
Body interface{} `json:"body,omitempty"`
Data []byte `json:"data"`
}
type ActivationData struct {
Campaign string `json:"campaign"`
Namespace string `json:"namespace,omitempty"`
Activation string `json:"activation"`
ActivationGeneration string `json:"activationGeneration"`
Stage string `json:"stage"`
Inputs map[string]interface{} `json:"inputs,omitempty"`
Outputs map[string]map[string]interface{} `json:"outputs,omitempty"`
Provider string `json:"provider,omitempty"`
Config interface{} `json:"config,omitempty"`
TriggeringStage string `json:"triggeringStage,omitempty"`
Schedule *ScheduleSpec `json:"schedule,omitempty"`
NeedsReport bool `json:"needsReport,omitempty"`
}
type HeartBeatAction string
const (
HeartBeatUpdate HeartBeatAction = "UPDATE"
HeartBeatDelete HeartBeatAction = "DELETE"
)
type HeartBeatData struct {
JobId string `json:"id"`
Scope string `json:"scope,omitempty"`
Action HeartBeatAction `json:"action"`
Time time.Time `json:"time"`
}
type ScheduleSpec struct {
Date string `json:"date"`
Time string `json:"time"`
Zone string `json:"zone"`
}
func (s ScheduleSpec) ShouldFireNow() (bool, error) {
dt, err := s.GetTime()
if err != nil {
return false, err
}
dtNow := time.Now().UTC()
dtUTC := dt.In(time.UTC)
return dtUTC.Before(dtNow), nil
}
func (s ScheduleSpec) GetTime() (time.Time, error) {
dt, err := parseTimeWithZone(s.Time, s.Date, s.Zone)
if err != nil {
return time.Time{}, err
}
return dt, nil
}
func parseTimeWithZone(timeStr string, dateStr string, zoneStr string) (time.Time, error) {
dtStr := dateStr + " " + timeStr
switch zoneStr {
case "LOCAL":
zoneStr = ""
case "PST", "PDT":
zoneStr = "America/Los_Angeles"
case "EST", "EDT":
zoneStr = "America/New_York"
case "CST", "CDT":
zoneStr = "America/Chicago"
case "MST", "MDT":
zoneStr = "America/Denver"
}
loc, err := time.LoadLocation(zoneStr)
if err != nil {
return time.Time{}, err
}
dt, err := time.ParseInLocation("2006-01-02 3:04:05PM", dtStr, loc)
if err != nil {
return time.Time{}, err
}
return dt, nil
}
type InputOutputData struct {
Inputs map[string]interface{} `json:"inputs,omitempty"`
Outputs map[string]map[string]interface{} `json:"outputs,omitempty"`
}