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
11cde7ed
Commit
11cde7ed
authored
Dec 12, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renames main lock function
parent
c565b018
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
12 deletions
+14
-12
pkg/infra/serverlock/serverlock.go
+6
-3
pkg/infra/serverlock/serverlock_integration_test.go
+6
-6
pkg/services/cleanup/cleanup.go
+2
-3
No files found.
pkg/infra/serverlock/serverlock.go
View file @
11cde7ed
...
@@ -13,7 +13,10 @@ func init() {
...
@@ -13,7 +13,10 @@ func init() {
registry
.
RegisterService
(
&
ServerLockService
{})
registry
.
RegisterService
(
&
ServerLockService
{})
}
}
// ServerLockService allows servers in HA mode to execute function once over in the group
// DistributedLockService
// ServerLockService allows servers in HA mode to claim a lock
// and execute an function if the server was granted the lock
type
ServerLockService
struct
{
type
ServerLockService
struct
{
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
log
log
.
Logger
log
log
.
Logger
...
@@ -25,10 +28,10 @@ func (sl *ServerLockService) Init() error {
...
@@ -25,10 +28,10 @@ func (sl *ServerLockService) Init() error {
return
nil
return
nil
}
}
//
OncePerServerGroup
try to create a lock for this server and only executes the
//
LockAndExecute
try to create a lock for this server and only executes the
// `fn` function when successful. This should not be used at low internal. But services
// `fn` function when successful. This should not be used at low internal. But services
// that needs to be run once every ex 10m.
// that needs to be run once every ex 10m.
func
(
sl
*
ServerLockService
)
OncePerServerGroup
(
ctx
context
.
Context
,
actionName
string
,
maxInterval
time
.
Duration
,
fn
func
())
error
{
func
(
sl
*
ServerLockService
)
LockAndExecute
(
ctx
context
.
Context
,
actionName
string
,
maxInterval
time
.
Duration
,
fn
func
())
error
{
// gets or creates a lockable row
// gets or creates a lockable row
rowLock
,
err
:=
sl
.
getOrCreate
(
ctx
,
actionName
)
rowLock
,
err
:=
sl
.
getOrCreate
(
ctx
,
actionName
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/infra/serverlock/serverlock_integration_test.go
View file @
11cde7ed
...
@@ -21,19 +21,19 @@ func TestServerLok(t *testing.T) {
...
@@ -21,19 +21,19 @@ func TestServerLok(t *testing.T) {
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
//this time `fn` should be executed
//this time `fn` should be executed
So
(
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
//this should not execute `fn`
//this should not execute `fn`
So
(
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
So
(
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
),
ShouldBeNil
)
// wait 5 second.
// wait 5 second.
<-
time
.
After
(
atInterval
*
2
)
<-
time
.
After
(
atInterval
*
2
)
// now `fn` should be executed again
// now `fn` should be executed again
err
=
sl
.
OncePerServerGroup
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
)
err
=
sl
.
LockAndExecute
(
ctx
,
"test-operation"
,
atInterval
,
incCounter
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
So
(
counter
,
ShouldEqual
,
2
)
So
(
counter
,
ShouldEqual
,
2
)
})
})
...
...
pkg/services/cleanup/cleanup.go
View file @
11cde7ed
...
@@ -7,9 +7,8 @@ import (
...
@@ -7,9 +7,8 @@ import (
"path"
"path"
"time"
"time"
"github.com/grafana/grafana/pkg/infra/serverlock"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/serverlock"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/log"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/registry"
...
@@ -41,7 +40,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
...
@@ -41,7 +40,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
srv
.
cleanUpTmpFiles
()
srv
.
cleanUpTmpFiles
()
srv
.
deleteExpiredSnapshots
()
srv
.
deleteExpiredSnapshots
()
srv
.
deleteExpiredDashboardVersions
()
srv
.
deleteExpiredDashboardVersions
()
srv
.
ServerLockService
.
OncePerServerGroup
(
ctx
,
"delete old login attempts"
,
time
.
Minute
*
10
,
func
()
{
srv
.
ServerLockService
.
LockAndExecute
(
ctx
,
"delete old login attempts"
,
time
.
Minute
*
10
,
func
()
{
srv
.
deleteOldLoginAttempts
()
srv
.
deleteOldLoginAttempts
()
})
})
...
...
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