Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.34 KB

readme.md

File metadata and controls

50 lines (33 loc) · 1.34 KB

Mogul - locking over nodes via mongoDB

GoDoc Build Status

This packages gives you some functionality to set a global lock for a specified duration. Afterwards the lock is up for grabs again. Make sure to use an unique identifier for each gorouting on each host for the user parameter.

The package uses mongo's atomic handling of documents. A document in the locks collection will automatically represent an atomic entity which can be claimed if it does not exists, or when the associated lock has expired.

func myTaskManagement() {
..

	var m Mananger = New(session.DB(database).C(collection), session.DB(database).C(tasks))
	
	lock := m.NewMutex(name, user)
	 
	if got, _ := lock.TryLock(timeFrame); got {
	    defer lock.Unlock()
	
	    // create some tasks
	    m.Add(taskName, data)
	    m.Add(taskName2, data2)
	
	}
..
}

func myTaskHandler() {
    
    var m Mananger = New(session.DB(database).C(collection), session.DB(database).C(tasks))
    lease := time.Hour
    
    for {
    
        task, err = m.Next(user, &lease)
        
        if task != nil {
            // unmarshal task.data and do some work.
        }
    
    }
}