cluster

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: Apache-2.0 Imports: 7 Imported by: 11

Documentation

Overview

package cluster exposes synchronization primitives to ensure correct behavior across multiple plugin instances in a Mattermost cluster.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	// contains filtered or unexported fields
}

Job is a scheduled job whose callback function is executed on a configured interval by at most one plugin instance at a time.

Use scheduled jobs to perform background activity on a regular interval without having to explicitly coordinate with other instances of the same plugin that might repeat that effort.

func Schedule

func Schedule(pluginAPI JobPluginAPI, key string, nextWaitInterval NextWaitInterval, callback func()) (*Job, error)

Schedule creates a scheduled job.

Example
// Use p.API from your plugin instead.
pluginAPI := plugin.API(nil)

callback := func() {
	// periodic work to do
}

job, err := Schedule(pluginAPI, "key", MakeWaitForInterval(5*time.Minute), callback)
if err != nil {
	panic("failed to schedule job")
}

// main thread

defer job.Close()
Output:

func (*Job) Close

func (j *Job) Close() error

Close terminates a scheduled job, preventing it from being scheduled on this plugin instance.

type JobConfig

type JobConfig struct {
	// Interval is the period of execution for the job.
	Interval time.Duration
}

JobConfig defines the configuration of a scheduled job.

type JobPluginAPI

type JobPluginAPI interface {
	MutexPluginAPI
	KVGet(key string) ([]byte, *model.AppError)
}

JobPluginAPI is the plugin API interface required to schedule jobs.

type Mutex

type Mutex struct {
	// contains filtered or unexported fields
}

Mutex is similar to sync.Mutex, except usable by multiple plugin instances across a cluster.

Internally, a mutex relies on an atomic key-value set operation as exposed by the Mattermost plugin API.

Mutexes with different names are unrelated. Mutexes with the same name from different plugins are unrelated. Pick a unique name for each mutex your plugin requires.

A Mutex must not be copied after first use.

Example
package main

import (
	"github.com/mattermost/mattermost-plugin-api/cluster"

	"github.com/mattermost/mattermost-server/v5/plugin"
)

func main() {
	// Use p.API from your plugin instead.
	pluginAPI := plugin.API(nil)

	m, err := cluster.NewMutex(pluginAPI, "key")
	if err != nil {
		panic(err)
	}
	m.Lock()
	// critical section
	m.Unlock()
}
Output:

func NewMutex

func NewMutex(pluginAPI MutexPluginAPI, key string) (*Mutex, error)

NewMutex creates a mutex with the given key name.

Panics if key is empty.

func (*Mutex) Lock

func (m *Mutex) Lock()

Lock locks m. If the mutex is already locked by any plugin instance, including the current one, the calling goroutine blocks until the mutex can be locked.

func (*Mutex) LockWithContext added in v0.0.8

func (m *Mutex) LockWithContext(ctx context.Context) error

LockWithContext locks m unless the context is canceled. If the mutex is already locked by any plugin instance, including the current one, the calling goroutine blocks until the mutex can be locked, or the context is canceled.

The mutex is locked only if a nil error is returned.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

Just like sync.Mutex, a locked Lock is not associated with a particular goroutine or plugin instance. It is allowed for one goroutine or plugin instance to lock a Lock and then arrange for another goroutine or plugin instance to unlock it. In practice, ownership of the lock should remain within a single plugin instance.

type MutexPluginAPI

type MutexPluginAPI interface {
	KVSetWithOptions(key string, value []byte, options model.PluginKVSetOptions) (bool, *model.AppError)
	LogError(msg string, keyValuePairs ...interface{})
}

MutexPluginAPI is the plugin API interface required to manage mutexes.

type NextWaitInterval added in v0.0.8

type NextWaitInterval func(now time.Time, metadata jobMetadata) time.Duration

NextWaitInterval is a callback computing the next wait interval for a job.

func MakeWaitForInterval added in v0.0.8

func MakeWaitForInterval(interval time.Duration) NextWaitInterval

MakeWaitForInterval creates a function to scheduling a job to run on the given interval relative to the last finished timestamp.

For example, if the job first starts at 12:01 PM, and is configured with interval 5 minutes, it will next run at:

12:06, 12:11, 12:16, ...

If the job has not previously started, it will run immediately.

func MakeWaitForRoundedInterval added in v0.0.8

func MakeWaitForRoundedInterval(interval time.Duration) NextWaitInterval

MakeWaitForRoundedInterval creates a function, scheduling a job to run on the nearest rounded interval relative to the last finished timestamp.

For example, if the job first starts at 12:04 PM, and is configured with interval 5 minutes, and is configured to round to 5 minute intervals, it will next run at:

12:05 PM, 12:10 PM, 12:15 PM, ...

If the job has not previously started, it will run immediately. Note that this wait interval strategy does not guarantee a minimum interval between runs, only that subsequent runs will be scheduled on the rounded interval.

Jump to

Keyboard shortcuts

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