Commit 11cde7ed by bergquist

renames main lock function

parent c565b018
...@@ -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 {
......
...@@ -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)
}) })
......
...@@ -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()
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment