-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
248 lines (217 loc) · 7.26 KB
/
session.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package jobrunner
import (
"encoding/json"
"runtime"
"strconv"
"github.com/google/uuid"
)
// Session is the storage for the current HTTP request session, containing information needed for logging, monitoring, etc.
type Session interface {
SessionMeta
SessionAttachment
SessionLogging
SessionWebcall
}
// SessionMeta is a subset of Session interface, containing only meta data related methods
type SessionMeta interface {
// GetID returns the ID of this registered session object
GetID() uuid.UUID
// GetIndex returns the instance index registered to session object for given session ID
GetIndex() int
// GetReruns returns the rerun count for the same instance since first scheduled
GetReruns() int
}
// SessionAttachment is a subset of Session interface, containing only attachment related methods
type SessionAttachment interface {
// Attach attaches any value object into the given session associated to the session ID
Attach(name string, value interface{}) bool
// Detach detaches any value object from the given session associated to the session ID
Detach(name string) bool
// GetRawAttachment retrieves any value object from the given session associated to the session ID and returns the raw interface (consumer needs to manually cast, but works for struct with private fields)
GetRawAttachment(name string) (interface{}, bool)
// GetAttachment retrieves any value object from the given session associated to the session ID and unmarshals the content to given data template (only works for structs with exported fields)
GetAttachment(name string, dataTemplate interface{}) bool
}
// SessionLogging is a subset of Session interface, containing only logging related methods
type SessionLogging interface {
// LogMethodEnter sends a logging entry of MethodEnter log type for the given session associated to the session ID
LogMethodEnter()
// LogMethodParameter sends a logging entry of MethodParameter log type for the given session associated to the session ID
LogMethodParameter(parameters ...interface{})
// LogMethodLogic sends a logging entry of MethodLogic log type for the given session associated to the session ID
LogMethodLogic(logLevel LogLevel, category string, subcategory string, messageFormat string, parameters ...interface{})
// LogMethodReturn sends a logging entry of MethodReturn log type for the given session associated to the session ID
LogMethodReturn(returns ...interface{})
// LogMethodExit sends a logging entry of MethodExit log type for the given session associated to the session ID
LogMethodExit()
}
// SessionWebcall is a subset of Session interface, containing only webcall related methods
type SessionWebcall interface {
// CreateWebcallRequest generates a webcall request object to the targeted external web service for the given session associated to the session ID
CreateWebcallRequest(method string, url string, payload string, sendClientCert bool) WebRequest
}
type session struct {
id uuid.UUID
index int
reruns int
attachment map[string]interface{}
customization Customization
}
// GetID returns the ID of this registered session object
func (session *session) GetID() uuid.UUID {
if session == nil {
return uuid.Nil
}
return session.id
}
// GetIndex returns the instance index registered to session object for given session ID
func (session *session) GetIndex() int {
if session == nil {
return 0
}
return session.index
}
// GetReruns returns the rerun count for the same instance since first scheduled
func (session *session) GetReruns() int {
if session == nil {
return 0
}
return session.reruns
}
// Attach attaches any value object into the given session associated to the session ID
func (session *session) Attach(name string, value interface{}) bool {
if session == nil {
return false
}
if session.attachment == nil {
session.attachment = map[string]interface{}{}
}
session.attachment[name] = value
return true
}
// Detach detaches any value object from the given session associated to the session ID
func (session *session) Detach(name string) bool {
if session == nil {
return false
}
if session.attachment != nil {
delete(session.attachment, name)
}
return true
}
// GetRawAttachment retrieves any value object from the given session associated to the session ID and returns the raw interface (consumer needs to manually cast, but works for struct with private fields)
func (session *session) GetRawAttachment(name string) (interface{}, bool) {
if session == nil {
return nil, false
}
var attachment, found = session.attachment[name]
if !found {
return nil, false
}
return attachment, true
}
// GetAttachment retrieves any value object from the given session associated to the session ID and unmarshals the content to given data template
func (session *session) GetAttachment(name string, dataTemplate interface{}) bool {
if session == nil {
return false
}
var attachment, found = session.GetRawAttachment(name)
if !found {
return false
}
var bytes, marshalError = json.Marshal(attachment)
if marshalError != nil {
return false
}
var unmarshalError = json.Unmarshal(
bytes,
dataTemplate,
)
return unmarshalError == nil
}
func getMethodName() string {
var pc, _, _, ok = runtime.Caller(3)
if !ok {
return "?"
}
var fn = runtime.FuncForPC(pc)
return fn.Name()
}
// LogMethodEnter sends a logging entry of MethodEnter log type for the given session associated to the session ID
func (session *session) LogMethodEnter() {
var methodName = getMethodName()
logMethodEnter(
session,
methodName,
"",
"",
)
}
// LogMethodParameter sends a logging entry of MethodParameter log type for the given session associated to the session ID
func (session *session) LogMethodParameter(parameters ...interface{}) {
var methodName = getMethodName()
for index, parameter := range parameters {
logMethodParameter(
session,
methodName,
strconv.Itoa(index),
"%v",
parameter,
)
}
}
// LogMethodLogic sends a logging entry of MethodLogic log type for the given session associated to the session ID
func (session *session) LogMethodLogic(logLevel LogLevel, category string, subcategory string, messageFormat string, parameters ...interface{}) {
logMethodLogic(
session,
logLevel,
category,
subcategory,
messageFormat,
parameters...,
)
}
// LogMethodReturn sends a logging entry of MethodReturn log type for the given session associated to the session ID
func (session *session) LogMethodReturn(returns ...interface{}) {
var methodName = getMethodName()
for index, returnValue := range returns {
logMethodReturn(
session,
methodName,
strconv.Itoa(index),
"%v",
returnValue,
)
}
}
// LogMethodExit sends a logging entry of MethodExit log type for the given session associated to the session ID
func (session *session) LogMethodExit() {
var methodName = getMethodName()
logMethodExit(
session,
methodName,
"",
"",
)
}
// CreateWebcallRequest generates a webcall request object to the targeted external web service for the given session associated to the session ID
func (session *session) CreateWebcallRequest(
method string,
url string,
payload string,
sendClientCert bool,
) WebRequest {
return &webRequest{
session,
method,
url,
payload,
map[string][]string{},
map[string][]string{},
0,
nil,
sendClientCert,
0,
[]dataReceiver{},
}
}