-
Notifications
You must be signed in to change notification settings - Fork 21
/
job-vendor.go
136 lines (123 loc) · 4.36 KB
/
job-vendor.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package vendors
import (
"context"
"encoding/json"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/managers/jobs"
"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/utils"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/managers"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability"
observ_utils "github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/observability/utils"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers/pubsub"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/vendors"
"github.com/eclipse-symphony/symphony/coa/pkg/logger"
"github.com/valyala/fasthttp"
)
var jLog = logger.NewLogger("coa.runtime")
type JobVendor struct {
vendors.Vendor
myMessages []string
JobsManager *jobs.JobsManager
}
func (o *JobVendor) GetInfo() vendors.VendorInfo {
return vendors.VendorInfo{
Version: o.Vendor.Version,
Name: "Job",
Producer: "Microsoft",
}
}
func (e *JobVendor) Init(config vendors.VendorConfig, factories []managers.IManagerFactroy, providers map[string]map[string]providers.IProvider, pubsubProvider pubsub.IPubSubProvider) error {
err := e.Vendor.Init(config, factories, providers, pubsubProvider)
if err != nil {
return err
}
for _, m := range e.Managers {
if c, ok := m.(*jobs.JobsManager); ok {
e.JobsManager = c
}
}
if e.JobsManager == nil {
return v1alpha2.NewCOAError(nil, "jobs manager is not supplied", v1alpha2.MissingConfig)
}
e.myMessages = make([]string, 0)
e.Vendor.Context.Subscribe("trace", func(topic string, event v1alpha2.Event) error {
msg := utils.FormatAsString(event.Body)
e.myMessages = append(e.myMessages, msg)
if len(e.myMessages) > 20 {
e.myMessages = e.myMessages[1:]
}
return nil
})
e.Vendor.Context.Subscribe("job", func(topic string, event v1alpha2.Event) error {
err := e.JobsManager.HandleJobEvent(context.Background(), event)
if err != nil && v1alpha2.IsDelayed(err) {
go e.Vendor.Context.Publish(topic, event)
}
return err
})
e.Vendor.Context.Subscribe("heartbeat", func(topic string, event v1alpha2.Event) error {
return e.JobsManager.HandleHeartBeatEvent(context.Background(), event)
})
e.Vendor.Context.Subscribe("schedule", func(topic string, event v1alpha2.Event) error {
return e.JobsManager.HandleScheduleEvent(context.Background(), event)
})
if err != nil {
return err
}
return nil
}
func (o *JobVendor) GetEndpoints() []v1alpha2.Endpoint {
route := "jobs"
if o.Route != "" {
route = o.Route
}
return []v1alpha2.Endpoint{
{
Methods: []string{fasthttp.MethodPost},
Route: route,
Version: o.Version,
Handler: o.onHello,
},
}
}
func (c *JobVendor) onHello(request v1alpha2.COARequest) v1alpha2.COAResponse {
_, span := observability.StartSpan("Job Vendor", request.Context, &map[string]string{
"method": "onHello",
})
defer span.End()
jLog.Infof("V (Job): onHello, method: %s, traceId: %s", string(request.Method), span.SpanContext().TraceID().String())
switch request.Method {
case fasthttp.MethodPost:
var activationData v1alpha2.ActivationData
err := json.Unmarshal(request.Body, &activationData)
if err != nil {
jLog.Errorf("V (Job): onHello failed - %s, traceId: %s", span.SpanContext().TraceID().String(), err.Error())
return observ_utils.CloseSpanWithCOAResponse(span, v1alpha2.COAResponse{
State: v1alpha2.BadRequest,
Body: []byte("{\"result\":\"400 - bad request\"}"),
ContentType: "application/json",
})
}
jLog.Infof("V (Job): onHello, activationData: %v, traceId: %s", activationData, span.SpanContext().TraceID().String())
c.Vendor.Context.Publish("activation", v1alpha2.Event{
Body: activationData,
})
return observ_utils.CloseSpanWithCOAResponse(span, v1alpha2.COAResponse{
State: v1alpha2.OK,
})
}
jLog.Errorf("V (Job): onHello failed - 405 method not allowed, traceId: %s", span.SpanContext().TraceID().String())
resp := v1alpha2.COAResponse{
State: v1alpha2.MethodNotAllowed,
Body: []byte("{\"result\":\"405 - method not allowed\"}"),
ContentType: "application/json",
}
observ_utils.UpdateSpanStatusFromCOAResponse(span, resp)
return resp
}