forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
148 lines (127 loc) · 4.3 KB
/
task.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package monitors
import (
"context"
"fmt"
"github.com/elastic/beats/v7/heartbeat/eventext"
"github.com/elastic/beats/v7/heartbeat/monitors/jobs"
"github.com/elastic/beats/v7/heartbeat/scheduler"
"github.com/elastic/beats/v7/heartbeat/scheduler/schedule"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
)
// configuredJob represents a job combined with its config and any
// subsequent processors.
type configuredJob struct {
job jobs.Job
config jobConfig
monitor *Monitor
cancelFn context.CancelFunc
client beat.Client
}
func newConfiguredJob(job jobs.Job, config jobConfig, monitor *Monitor) (*configuredJob, error) {
return &configuredJob{
job: job,
config: config,
monitor: monitor,
}, nil
}
// jobConfig represents fields needed to execute a single job.
type jobConfig struct {
Name string `config:"pluginName"`
Type string `config:"type"`
Schedule *schedule.Schedule `config:"schedule" validate:"required"`
}
// ProcessorsError is used to indicate situations when processors could not be loaded.
// This special type is used because these errors are caught and handled gracefully.
type ProcessorsError struct{ root error }
func (e ProcessorsError) Error() string {
return fmt.Sprintf("could not load monitor processors: %s", e.root)
}
func (t *configuredJob) prepareSchedulerJob(job jobs.Job) scheduler.TaskFunc {
return func(_ context.Context) []scheduler.TaskFunc {
return runPublishJob(job, t.client)
}
}
func (t *configuredJob) makeSchedulerTaskFunc() scheduler.TaskFunc {
return t.prepareSchedulerJob(t.job)
}
// Start schedules this configuredJob for execution.
func (t *configuredJob) Start() {
var err error
t.client, err = t.monitor.pipelineConnector.Connect()
if err != nil {
logp.Err("could not start monitor: %v", err)
return
}
tf := t.makeSchedulerTaskFunc()
t.cancelFn, err = t.monitor.scheduler.Add(t.config.Schedule, t.monitor.stdFields.ID, tf)
if err != nil {
logp.Err("could not start monitor: %v", err)
}
}
// Stop unschedules this configuredJob from execution.
func (t *configuredJob) Stop() {
if t.cancelFn != nil {
t.cancelFn()
}
if t.client != nil {
t.client.Close()
}
}
func runPublishJob(job jobs.Job, client beat.Client) []scheduler.TaskFunc {
event := &beat.Event{
Fields: common.MapStr{},
}
conts, err := job(event)
if err != nil {
logp.Err("Job %v failed with: ", err)
}
hasContinuations := len(conts) > 0
if event.Fields != nil && !eventext.IsEventCancelled(event) {
// If continuations are present we defensively publish a clone of the event
// in the chance that the event shares underlying data with the events for continuations
// This prevents races where the pipeline publish could accidentally alter multiple events.
if hasContinuations {
clone := beat.Event{
Timestamp: event.Timestamp,
Meta: event.Meta.Clone(),
Fields: event.Fields.Clone(),
}
client.Publish(clone)
} else {
// no clone needed if no continuations
client.Publish(*event)
}
}
if !hasContinuations {
return nil
}
contTasks := make([]scheduler.TaskFunc, len(conts))
for i, cont := range conts {
// Move the continuation into the local block scope
// This is important since execution is deferred
// Without this only the last continuation will be executed len(conts) times
localCont := cont
contTasks[i] = func(_ context.Context) []scheduler.TaskFunc {
return runPublishJob(localCont, client)
}
}
return contTasks
}