瀏覽代碼

renames main lock function

bergquist 7 年之前
父節點
當前提交
11cde7ed71

+ 6 - 3
pkg/infra/serverlock/serverlock.go

@@ -13,7 +13,10 @@ func init() {
 	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 {
 	SQLStore *sqlstore.SqlStore `inject:""`
 	log      log.Logger
@@ -25,10 +28,10 @@ func (sl *ServerLockService) Init() error {
 	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
 // 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
 	rowLock, err := sl.getOrCreate(ctx, actionName)
 	if err != nil {

+ 6 - 6
pkg/infra/serverlock/serverlock_integration_test.go

@@ -21,19 +21,19 @@ func TestServerLok(t *testing.T) {
 		ctx := context.Background()
 
 		//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`
-		So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
-		So(sl.OncePerServerGroup(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
-		So(sl.OncePerServerGroup(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.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
+		So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
+		So(sl.LockAndExecute(ctx, "test-operation", atInterval, incCounter), ShouldBeNil)
 
 		// wait 5 second.
 		<-time.After(atInterval * 2)
 
 		// 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(counter, ShouldEqual, 2)
 	})

+ 2 - 3
pkg/services/cleanup/cleanup.go

@@ -7,9 +7,8 @@ import (
 	"path"
 	"time"
 
-	"github.com/grafana/grafana/pkg/infra/serverlock"
-
 	"github.com/grafana/grafana/pkg/bus"
+	"github.com/grafana/grafana/pkg/infra/serverlock"
 	"github.com/grafana/grafana/pkg/log"
 	m "github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/registry"
@@ -41,7 +40,7 @@ func (srv *CleanUpService) Run(ctx context.Context) error {
 			srv.cleanUpTmpFiles()
 			srv.deleteExpiredSnapshots()
 			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()
 			})