/
listener.go
111 lines (102 loc) · 3.73 KB
/
listener.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
/*
* Copyright (c) 2022 AlertAvert.com. All rights reserved.
*
* Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Author: Marco Massenzio (marco@alertavert.com)
*/
package pubsub
import (
"fmt"
log "github.com/massenz/slf4go/logging"
"github.com/massenz/go-statemachine/pkg/storage"
protos "github.com/massenz/statemachine-proto/golang/api"
)
func NewEventsListener(options *ListenerOptions) *EventsListener {
return &EventsListener{
logger: log.NewLog("Listener"),
events: options.EventsChannel,
store: options.StatemachinesStore,
notifications: options.NotificationsChannel,
}
}
// SetLogLevel to implement the log.Loggable interface
func (listener *EventsListener) SetLogLevel(level log.LogLevel) {
listener.logger.Level = level
}
func (listener *EventsListener) PostNotificationAndReportOutcome(eventResponse *protos.EventResponse) {
if eventResponse.Outcome.Code != protos.EventOutcome_Ok {
listener.logger.Error("event [%s]: %s",
eventResponse.GetEventId(), eventResponse.GetOutcome().Details)
}
if listener.notifications != nil {
listener.logger.Debug("posting notification: %v", eventResponse.GetEventId())
listener.notifications <- *eventResponse
}
listener.logger.Debug("Reporting outcome: %v", eventResponse.GetEventId())
listener.reportOutcome(eventResponse)
}
func (listener *EventsListener) ListenForMessages() {
listener.logger.Info("Events message listener started")
for request := range listener.events {
listener.logger.Debug("Received request %s", request.Event.String())
fsmId := request.GetId()
if fsmId == "" {
listener.PostNotificationAndReportOutcome(makeResponse(&request,
protos.EventOutcome_MissingDestination,
fmt.Sprintf("no statemachine ID specified")))
continue
}
cfgName := request.GetConfig()
if cfgName == "" {
listener.PostNotificationAndReportOutcome(makeResponse(&request,
protos.EventOutcome_MissingDestination,
fmt.Sprintf("no Configuration name specified")))
continue
}
// The event is well-formed, we can store for later retrieval
if err := listener.store.PutEvent(request.Event, cfgName, storage.NeverExpire); err != nil {
listener.PostNotificationAndReportOutcome(makeResponse(&request,
protos.EventOutcome_InternalError,
fmt.Sprintf("could not store event: %v", err)))
continue
}
listener.logger.Debug("preparing to send event `%s` for FSM [%s]",
request.Event.Transition.Event, fsmId)
if err := listener.store.TxProcessEvent(fsmId, cfgName, request.Event); err != nil {
var errCode protos.EventOutcome_StatusCode
if storage.IsNotFoundErr(err) {
errCode = protos.EventOutcome_FsmNotFound
} else {
errCode = protos.EventOutcome_InternalError
}
listener.PostNotificationAndReportOutcome(makeResponse(&request,
errCode,
fmt.Sprintf("could not update statemachine [%s#%s] in store: %v",
cfgName, fsmId, err)))
continue
}
listener.logger.Debug("Event `%s` successfully changed FSM [%s] state",
request.Event.Transition.Event, fsmId)
listener.reportOutcome(makeResponse(&request, protos.EventOutcome_Ok, ""))
}
}
func (listener *EventsListener) reportOutcome(response *protos.EventResponse) {
if err := listener.store.AddEventOutcome(response.EventId, response.GetOutcome().GetConfig(),
response.Outcome, storage.NeverExpire); err != nil {
listener.logger.Error("could not save event outcome: %v", err)
}
}
func makeResponse(request *protos.EventRequest, code protos.EventOutcome_StatusCode,
details string) *protos.EventResponse {
return &protos.EventResponse{
EventId: request.GetEvent().GetEventId(),
Outcome: &protos.EventOutcome{
Code: code,
Details: details,
Config: request.Config,
Id: request.Id,
},
}
}