package jnpf.flowable.service.dev;
|
|
import jnpf.flowable.util.RedisLock;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
import org.springframework.stereotype.Component;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
/** Shares normal approval's node-lock namespace; token serialization must match the acquired value. */
|
@Component
|
@RequiredArgsConstructor
|
public class DevApprovalLocks {
|
private final RedisLock nativeLocks;
|
private final RedisTemplate<String, Object> redisTemplate;
|
|
public boolean lock(String name, String token, long ttl, TimeUnit unit) {
|
return nativeLocks.lock(name, token, ttl, unit);
|
}
|
|
public boolean unlock(String name, String token) {
|
var script = new DefaultRedisScript<Long>(
|
"if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end", Long.class);
|
// Pass the token itself as the vararg, not a List serialized as a different Redis value.
|
Long result = redisTemplate.execute(script, List.of("workflow-lock:" + name), token);
|
return result != null && result > 0;
|
}
|
}
|