Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c5278af6
Commit
c5278af6
authored
Sep 27, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for mysql and postgres unique index error codes
parent
3fab6162
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
57 additions
and
15 deletions
+57
-15
pkg/models/alert_notifications.go
+7
-3
pkg/services/alerting/notifier.go
+2
-0
pkg/services/sqlstore/alert_notification.go
+37
-6
pkg/services/sqlstore/alert_notification_test.go
+11
-6
No files found.
pkg/models/alert_notifications.go
View file @
c5278af6
...
@@ -11,7 +11,7 @@ var (
...
@@ -11,7 +11,7 @@ var (
ErrNotificationFrequencyNotFound
=
errors
.
New
(
"Notification frequency not specified"
)
ErrNotificationFrequencyNotFound
=
errors
.
New
(
"Notification frequency not specified"
)
ErrAlertNotificationStateNotFound
=
errors
.
New
(
"alert notification state not found"
)
ErrAlertNotificationStateNotFound
=
errors
.
New
(
"alert notification state not found"
)
ErrAlertNotificationStateVersionConflict
=
errors
.
New
(
"alert notification state update version conflict"
)
ErrAlertNotificationStateVersionConflict
=
errors
.
New
(
"alert notification state update version conflict"
)
ErrAlertNotificationStateAl
lreadyExist
=
errors
.
New
(
"alert notification state al
lready exists."
)
ErrAlertNotificationStateAl
readyExist
=
errors
.
New
(
"alert notification state a
lready exists."
)
)
)
type
AlertNotificationStateType
string
type
AlertNotificationStateType
string
...
@@ -95,13 +95,17 @@ type AlertNotificationState struct {
...
@@ -95,13 +95,17 @@ type AlertNotificationState struct {
Version
int64
Version
int64
}
}
type
UpdateAlertNotificationState
Command
struct
{
type
SetAlertNotificationStateToPending
Command
struct
{
Id
int64
Id
int64
SentAt
int64
SentAt
int64
State
AlertNotificationStateType
Version
int64
Version
int64
}
}
type
SetAlertNotificationStateToCompleteCommand
struct
{
Id
int64
SentAt
int64
}
type
GetNotificationStateQuery
struct
{
type
GetNotificationStateQuery
struct
{
OrgId
int64
OrgId
int64
AlertId
int64
AlertId
int64
...
...
pkg/services/alerting/notifier.go
View file @
c5278af6
...
@@ -64,6 +64,8 @@ func (n *notificationService) sendNotifications(evalContext *EvalContext, notifi
...
@@ -64,6 +64,8 @@ func (n *notificationService) sendNotifications(evalContext *EvalContext, notifi
err
:=
bus
.
InTransaction
(
evalContext
.
Ctx
,
func
(
ctx
context
.
Context
)
error
{
err
:=
bus
.
InTransaction
(
evalContext
.
Ctx
,
func
(
ctx
context
.
Context
)
error
{
n
.
log
.
Debug
(
"trying to send notification"
,
"id"
,
not
.
GetNotifierId
())
n
.
log
.
Debug
(
"trying to send notification"
,
"id"
,
not
.
GetNotifierId
())
// insert if needed
// Verify that we can send the notification again
// Verify that we can send the notification again
// but this time within the same transaction.
// but this time within the same transaction.
// if !evalContext.IsTestRun && !not.ShouldNotify(ctx, evalContext) {
// if !evalContext.IsTestRun && !not.ShouldNotify(ctx, evalContext) {
...
...
pkg/services/sqlstore/alert_notification.go
View file @
c5278af6
...
@@ -20,6 +20,8 @@ func init() {
...
@@ -20,6 +20,8 @@ func init() {
bus
.
AddHandler
(
"sql"
,
GetAllAlertNotifications
)
bus
.
AddHandler
(
"sql"
,
GetAllAlertNotifications
)
bus
.
AddHandlerCtx
(
"sql"
,
InsertAlertNotificationState
)
bus
.
AddHandlerCtx
(
"sql"
,
InsertAlertNotificationState
)
bus
.
AddHandlerCtx
(
"sql"
,
GetAlertNotificationState
)
bus
.
AddHandlerCtx
(
"sql"
,
GetAlertNotificationState
)
bus
.
AddHandlerCtx
(
"sql"
,
SetAlertNotificationStateToCompleteCommand
)
bus
.
AddHandlerCtx
(
"sql"
,
SetAlertNotificationStateToPendingCommand
)
}
}
func
DeleteAlertNotification
(
cmd
*
m
.
DeleteAlertNotificationCommand
)
error
{
func
DeleteAlertNotification
(
cmd
*
m
.
DeleteAlertNotificationCommand
)
error
{
...
@@ -244,25 +246,54 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
...
@@ -244,25 +246,54 @@ func InsertAlertNotificationState(ctx context.Context, cmd *m.InsertAlertNotific
return
nil
return
nil
}
}
if
strings
.
HasPrefix
(
err
.
Error
(),
"UNIQUE constraint failed"
)
{
uniqenessIndexFailureCodes
:=
[]
string
{
return
m
.
ErrAlertNotificationStateAllreadyExist
"UNIQUE constraint failed"
,
"pq: duplicate key value violates unique constraint"
,
"Error 1062: Duplicate entry "
,
}
for
_
,
code
:=
range
uniqenessIndexFailureCodes
{
if
strings
.
HasPrefix
(
err
.
Error
(),
code
)
{
return
m
.
ErrAlertNotificationStateAlreadyExist
}
}
}
return
err
return
err
})
})
}
}
func
UpdateAlertNotificationState
(
ctx
context
.
Context
,
cmd
*
m
.
UpdateAlertNotificationStateCommand
)
error
{
func
SetAlertNotificationStateToCompleteCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToCompleteCommand
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
sql
:=
`UPDATE alert_notification_state SET
state= ?
WHERE
id = ?`
res
,
err
:=
sess
.
Exec
(
sql
,
m
.
AlertNotificationStateCompleted
,
cmd
.
Id
)
if
err
!=
nil
{
return
err
}
affected
,
_
:=
res
.
RowsAffected
()
if
affected
==
0
{
return
m
.
ErrAlertNotificationStateVersionConflict
}
return
nil
})
}
func
SetAlertNotificationStateToPendingCommand
(
ctx
context
.
Context
,
cmd
*
m
.
SetAlertNotificationStateToPendingCommand
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
sql
:=
`UPDATE alert_notification_state SET
sql
:=
`UPDATE alert_notification_state SET
state= ?,
state= ?,
version = ?
version = ?
WHERE
WHERE
id = ? AND
id = ? AND
version = ?
version = ?`
`
res
,
err
:=
sess
.
Exec
(
sql
,
cmd
.
State
,
cmd
.
Version
+
1
,
cmd
.
Id
,
cmd
.
Version
)
res
,
err
:=
sess
.
Exec
(
sql
,
m
.
AlertNotificationStatePending
,
cmd
.
Version
+
1
,
cmd
.
Id
,
cmd
.
Version
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
pkg/services/sqlstore/alert_notification_test.go
View file @
c5278af6
...
@@ -38,23 +38,28 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
...
@@ -38,23 +38,28 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
err
=
InsertAlertNotificationState
(
context
.
Background
(),
createCmd
)
err
=
InsertAlertNotificationState
(
context
.
Background
(),
createCmd
)
So
(
err
,
ShouldEqual
,
models
.
ErrAlertNotificationStateAl
l
readyExist
)
So
(
err
,
ShouldEqual
,
models
.
ErrAlertNotificationStateAlreadyExist
)
Convey
(
"should be able to update alert notifier state"
,
func
()
{
Convey
(
"should be able to update alert notifier state"
,
func
()
{
updateCmd
:=
&
models
.
UpdateAlertNotificationState
Command
{
updateCmd
:=
&
models
.
SetAlertNotificationStateToPending
Command
{
Id
:
1
,
Id
:
1
,
SentAt
:
1
,
SentAt
:
1
,
State
:
models
.
AlertNotificationStatePending
,
Version
:
0
,
Version
:
0
,
}
}
err
:=
UpdateAlertNotificationState
(
context
.
Background
(),
updateCmd
)
err
:=
SetAlertNotificationStateToPendingCommand
(
context
.
Background
(),
updateCmd
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
Convey
(
"should not be able to
update older versions
"
,
func
()
{
Convey
(
"should not be able to
set pending on old version
"
,
func
()
{
err
=
UpdateAlertNotificationState
(
context
.
Background
(),
updateCmd
)
err
=
SetAlertNotificationStateToPendingCommand
(
context
.
Background
(),
updateCmd
)
So
(
err
,
ShouldEqual
,
models
.
ErrAlertNotificationStateVersionConflict
)
So
(
err
,
ShouldEqual
,
models
.
ErrAlertNotificationStateVersionConflict
)
})
})
Convey
(
"should be able to set state to completed"
,
func
()
{
cmd
:=
&
models
.
SetAlertNotificationStateToCompleteCommand
{
Id
:
1
}
err
=
SetAlertNotificationStateToCompleteCommand
(
context
.
Background
(),
cmd
)
So
(
err
,
ShouldBeNil
)
})
})
})
})
})
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment