Documentation ¶
Overview ¶
Package mogul provides distributed locking and task handling via mongodb.
Using mongo documents we can synchronize and do work over a number of nodes. The typical usecase would be to run a cron job or scheduler on all nodes and then perform task creation on a single node using a lock. These tasks can then be efficiently executed on all nodes that are alive.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func New ¶
func New(locks *mgo.Collection, tasks *mgo.Collection) *Manager
New creates a new manager to perform locking and task management. You can cast it to TaskHandler or MutexCreator to limit the behaviour to one of the two subjects.
func (*Manager) Add ¶
Add a task, with a name (should be unique for different jobs) and some payload for the consumer to base his/her work on.
Example ¶
session := initDB() defer clearDB(session) var m mogul.TaskHandler = mogul.New(session.DB(database).C(collection), session.DB(database).C(tasks)) payload := []byte("barfbarf") user := "testUser" name := "firstTask" // create a task m.Add(name, payload) // the only task we just created should pop task, _ := m.Next(user, nil) fmt.Println(task)
Output:
func (*Manager) NewMutex ¶
Use the new function to combine a mongo session with your lock's name and user to obtain a new mutex, which is not locked yet
func (*Manager) Next ¶
Next picks the next available task. You can give a lease time after which the job is back up for grabs for other consumers if its not completed. next task is selected via random points in a 2D array that are assigned to all tasks: http://stackoverflow.com/questions/2824157/random-record-from-mongodb at bottom
type Mutex ¶
type Mutex struct {
// contains filtered or unexported fields
}
Mutex is the object used for locking
type MutexCreator ¶
MutexCreator is a specialization of the Manager struct for locking
type Task ¶
type Task struct { Name string `bson:"_id"` User *string `bson:"user,omitempty"` Data []byte `bson:"task"` ExpiresAtUtc *time.Time `bson:"expires,omitempty"` Doc meta `bson:",inline"` // contains filtered or unexported fields }
Task is the entity we work with to regulate jobs. It consists of a name and a payload. If a task is claimed the user and optinal expiresAtUtc will be filled.