serverlock

package
v0.0.1-test Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2022 License: AGPL-3.0 Imports: 7 Imported by: 199

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ServerLockService

type ServerLockService struct {
	SQLStore *sqlstore.SQLStore
	// contains filtered or unexported fields
}

ServerLockService allows servers in HA mode to claim a lock and execute a function if the server was granted the lock It exposes 2 services LockAndExecute and LockExecuteAndRelease, which are intended to be used independently, don't mix them up (ie, use the same actionName for both of them).

func ProvideService

func ProvideService(sqlStore *sqlstore.SQLStore, tracer tracing.Tracer) *ServerLockService

func (*ServerLockService) LockAndExecute

func (sl *ServerLockService) LockAndExecute(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error

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 (*ServerLockService) LockExecuteAndRelease

func (sl *ServerLockService) LockExecuteAndRelease(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error

LockExecuteAndRelease Creates the lock, executes the func, and then release the locks. The locking mechanism is based on the UNIQUE constraint of the actionName in the database (column operation_uid), so a new process can not insert a new operation if already exists one. The parameter 'maxInterval' is a timeout safeguard, if the LastExecution in the database is older than maxInterval, we will assume the lock as timeouted. The 'maxInterval' parameter should be so long that is impossible for 2 processes to run at the same time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL