1
1
package incident
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"github.com/icinga/icinga-notifications/internal/event"
6
7
"github.com/icinga/icinga-notifications/internal/recipient"
@@ -14,7 +15,7 @@ import (
14
15
// Sync initiates an *incident.IncidentRow from the current incident state and syncs it with the database.
15
16
// Before syncing any incident related database entries, this method should be called at least once.
16
17
// Returns an error on db failure.
17
- func (i * Incident ) Sync (tx * sqlx.Tx ) error {
18
+ func (i * Incident ) Sync (ctx context. Context , tx * sqlx.Tx ) error {
18
19
incidentRow := & IncidentRow {
19
20
ID : i .incidentRowID ,
20
21
ObjectID : i .Object .ID ,
@@ -23,7 +24,7 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
23
24
Severity : i .Severity (),
24
25
}
25
26
26
- err := incidentRow .Sync (tx , i .db , i .incidentRowID != 0 )
27
+ err := incidentRow .Sync (ctx , tx , i .db , i .incidentRowID != 0 )
27
28
if err != nil {
28
29
return err
29
30
}
@@ -33,19 +34,19 @@ func (i *Incident) Sync(tx *sqlx.Tx) error {
33
34
return nil
34
35
}
35
36
36
- func (i * Incident ) AddHistory (tx * sqlx.Tx , historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
37
+ func (i * Incident ) AddHistory (ctx context. Context , tx * sqlx.Tx , historyRow * HistoryRow , fetchId bool ) (types.Int , error ) {
37
38
historyRow .IncidentID = i .incidentRowID
38
39
39
40
stmt := utils .BuildInsertStmtWithout (i .db , historyRow , "id" )
40
41
if fetchId {
41
- historyId , err := utils .InsertAndFetchId (tx , stmt , historyRow )
42
+ historyId , err := utils .InsertAndFetchId (ctx , tx , stmt , historyRow )
42
43
if err != nil {
43
44
return types.Int {}, err
44
45
}
45
46
46
47
return utils .ToDBInt (historyId ), nil
47
48
} else {
48
- _ , err := tx .NamedExec ( stmt , historyRow )
49
+ _ , err := tx .NamedExecContext ( ctx , stmt , historyRow )
49
50
if err != nil {
50
51
return types.Int {}, err
51
52
}
@@ -54,30 +55,30 @@ func (i *Incident) AddHistory(tx *sqlx.Tx, historyRow *HistoryRow, fetchId bool)
54
55
return types.Int {}, nil
55
56
}
56
57
57
- func (i * Incident ) AddEscalationTriggered (tx * sqlx.Tx , state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
58
+ func (i * Incident ) AddEscalationTriggered (ctx context. Context , tx * sqlx.Tx , state * EscalationState , hr * HistoryRow ) (types.Int , error ) {
58
59
state .IncidentID = i .incidentRowID
59
60
60
61
stmt , _ := i .db .BuildUpsertStmt (state )
61
- _ , err := tx .NamedExec ( stmt , state )
62
+ _ , err := tx .NamedExecContext ( ctx , stmt , state )
62
63
if err != nil {
63
64
return types.Int {}, err
64
65
}
65
66
66
- return i .AddHistory (tx , hr , true )
67
+ return i .AddHistory (ctx , tx , hr , true )
67
68
}
68
69
69
70
// AddEvent Inserts incident history record to the database and returns an error on db failure.
70
- func (i * Incident ) AddEvent (tx * sqlx.Tx , ev * event.Event ) error {
71
+ func (i * Incident ) AddEvent (ctx context. Context , tx * sqlx.Tx , ev * event.Event ) error {
71
72
ie := & EventRow {IncidentID : i .incidentRowID , EventID : ev .ID }
72
73
stmt , _ := i .db .BuildInsertStmt (ie )
73
- _ , err := tx .NamedExec ( stmt , ie )
74
+ _ , err := tx .NamedExecContext ( ctx , stmt , ie )
74
75
75
76
return err
76
77
}
77
78
78
79
// AddRecipient adds recipient from the given *rule.Escalation to this incident.
79
80
// Syncs also all the recipients with the database and returns an error on db failure.
80
- func (i * Incident ) AddRecipient (tx * sqlx.Tx , escalation * rule.Escalation , eventId int64 ) error {
81
+ func (i * Incident ) AddRecipient (ctx context. Context , tx * sqlx.Tx , escalation * rule.Escalation , eventId int64 ) error {
81
82
newRole := RoleRecipient
82
83
if i .HasManager () {
83
84
newRole = RoleSubscriber
@@ -110,7 +111,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
110
111
OldRecipientRole : oldRole ,
111
112
}
112
113
113
- _ , err := i .AddHistory (tx , hr , false )
114
+ _ , err := i .AddHistory (ctx , tx , hr , false )
114
115
if err != nil {
115
116
return err
116
117
}
@@ -119,7 +120,7 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
119
120
}
120
121
121
122
stmt , _ := i .db .BuildUpsertStmt (cr )
122
- _ , err := tx .NamedExec ( stmt , cr )
123
+ _ , err := tx .NamedExecContext ( ctx , stmt , cr )
123
124
if err != nil {
124
125
return fmt .Errorf ("failed to upsert incident contact %s: %w" , r , err )
125
126
}
@@ -130,18 +131,18 @@ func (i *Incident) AddRecipient(tx *sqlx.Tx, escalation *rule.Escalation, eventI
130
131
131
132
// AddRuleMatchedHistory syncs the given *rule.Rule and history entry to the database.
132
133
// Returns an error on database failure.
133
- func (i * Incident ) AddRuleMatchedHistory (tx * sqlx.Tx , r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
134
+ func (i * Incident ) AddRuleMatchedHistory (ctx context. Context , tx * sqlx.Tx , r * rule.Rule , hr * HistoryRow ) (types.Int , error ) {
134
135
rr := & RuleRow {IncidentID : i .incidentRowID , RuleID : r .ID }
135
136
stmt , _ := i .db .BuildUpsertStmt (rr )
136
- _ , err := tx .NamedExec ( stmt , rr )
137
+ _ , err := tx .NamedExecContext ( ctx , stmt , rr )
137
138
if err != nil {
138
139
return types.Int {}, err
139
140
}
140
141
141
- return i .AddHistory (tx , hr , true )
142
+ return i .AddHistory (ctx , tx , hr , true )
142
143
}
143
144
144
- func (i * Incident ) AddSourceSeverity (tx * sqlx.Tx , severity event.Severity , sourceID int64 ) error {
145
+ func (i * Incident ) AddSourceSeverity (ctx context. Context , tx * sqlx.Tx , severity event.Severity , sourceID int64 ) error {
145
146
i .SeverityBySource [sourceID ] = severity
146
147
147
148
sourceSeverity := & SourceSeverity {
@@ -151,7 +152,7 @@ func (i *Incident) AddSourceSeverity(tx *sqlx.Tx, severity event.Severity, sourc
151
152
}
152
153
153
154
stmt , _ := i .db .BuildUpsertStmt (sourceSeverity )
154
- _ , err := tx .NamedExec ( stmt , sourceSeverity )
155
+ _ , err := tx .NamedExecContext ( ctx , stmt , sourceSeverity )
155
156
156
157
return err
157
158
}
0 commit comments