Skip to content

Commit

Permalink
distributed lock plugin gorm provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Apr 6, 2017
1 parent 9728d45 commit bb4ccc0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
@@ -1,11 +1,11 @@
package com.bertram.lock

class DistributedLock {
String key
String name
String value
Long timeout //epoch timeout
static constraints = {
key unique:true, nullable:false
name unique:true, nullable:false
value nullable:true
timeout nullable:true
}
Expand Down
Expand Up @@ -34,8 +34,8 @@ public class GormLockProvider extends LockProvider {
Promises.tasks {
def now = new Date().time
DistributedLock.withNewSession { session ->
DistributedLock.where{key == buildKey(name,ns) && timeout < now}.deleteAll()
def lock = new DistributedLock(key:buildKey(name,ns), value: keyValue,timeout: now + expires)
DistributedLock.where{name == buildKey(name,ns) && timeout < now}.deleteAll()
def lock = new DistributedLock(name:buildKey(name,ns), value: keyValue,timeout: now + expires)
lock.save(flush:true,failOnError:true)
}
}.get()
Expand Down Expand Up @@ -74,7 +74,7 @@ public class GormLockProvider extends LockProvider {
def now = new Date().time
DistributedLock.withNewSession { session ->
def lock = DistributedLock.withCriteria(uniqueResult:true) {
eq('key',buildKey(name,ns))
eq('name',buildKey(name,ns))
or {
isNull('timeout')
gte('timeout',now)
Expand All @@ -86,7 +86,7 @@ public class GormLockProvider extends LockProvider {
log.warn("Someone else has the lock ${name}")
return
}
DistributedLock.where{key == lock.key}.deleteAll()
DistributedLock.where{name == lock.key}.deleteAll()
}
}.get()
}
Expand All @@ -110,7 +110,7 @@ public class GormLockProvider extends LockProvider {
def now = new Date().time
DistributedLock.withNewSession { session ->
def lock = DistributedLock.withCriteria(uniqueResult:true) {
eq('key',buildKey(name,ns))
eq('name',buildKey(name,ns))
or {
isNull('timeout')
gte('timeout',now)
Expand Down Expand Up @@ -144,7 +144,7 @@ public class GormLockProvider extends LockProvider {
def now = new Date().time
DistributedLock.withNewSession { session ->
def lock = DistributedLock.withCriteria(uniqueResult:true) {
eq('key',buildKey(name,ns))
eq('name',buildKey(name,ns))
or {
isNull('timeout')
gte('timeout',now)
Expand All @@ -153,9 +153,9 @@ public class GormLockProvider extends LockProvider {
}
if(lock) {
if (expires > 0)
DistributedLock.where { key == buildKey(name,ns) && (timeout == null || timeout > now)}.updateAll(timeout:now + expires)
DistributedLock.where { name == buildKey(name,ns) && (timeout == null || timeout > now)}.updateAll(timeout:now + expires)
else
DistributedLock.where{ key == buildKey(name,ns) && (timeout == null || timeout > now)}.updateAll(timeout:null)
DistributedLock.where{ name == buildKey(name,ns) && (timeout == null || timeout > now)}.updateAll(timeout:null)
return true
} else {
return false
Expand All @@ -180,7 +180,7 @@ public class GormLockProvider extends LockProvider {
try {
return Promises.tasks {
DistributedLock.withNewSession { session ->
return DistributedLock.exeuteQuery("select key from DistributedLock distributedlock where distributedlock.key like ${namespace + '.%'} distributedlock.timeout IS NULL OR distributedlock.timeout < ${new Date().time}")
return DistributedLock.executeQuery("select key from DistributedLock distributedlock where distributedlock.key like ${namespace + '.%'} distributedlock.timeout IS NULL OR distributedlock.timeout < ${new Date().time}")
}
}.get()
}
Expand Down
Expand Up @@ -37,7 +37,9 @@ class LockServiceConfigurer {
config.provider.type = Class.forName(config.provider.type)
}
this.beanBuilder."${getLockServiceBeanName()}"(config.provider.type) {
redisBeanName = config.provider?.connect ?: 'redisService'
if(config.provider.type == RedisLockProvider) {
redisBeanName = config.provider?.connect ?: 'redisService'
}
grailsApp = app
if (config.namespace)
namespace = config.namespace
Expand Down

0 comments on commit bb4ccc0

Please sign in to comment.