-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b34c4e1
commit 5241bc9
Showing
7 changed files
with
80 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 26 additions & 31 deletions
57
...ream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/NatsJetStreamLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,42 @@ | ||
package net.javacrumbs.shedlock.provider.nats.jetstream; | ||
|
||
import io.nats.client.Connection; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.nats.client.Connection; | ||
import net.javacrumbs.shedlock.core.AbstractSimpleLock; | ||
import net.javacrumbs.shedlock.core.ClockProvider; | ||
import net.javacrumbs.shedlock.core.LockConfiguration; | ||
import net.javacrumbs.shedlock.support.LockException; | ||
import net.javacrumbs.shedlock.support.annotation.NonNull; | ||
|
||
public final class NatsJetStreamLock extends AbstractSimpleLock { | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
private final Logger log = LoggerFactory.getLogger(NatsJetStreamLock.class); | ||
final class NatsJetStreamLock extends AbstractSimpleLock { | ||
|
||
private final Connection connection; | ||
private final Logger log = LoggerFactory.getLogger(NatsJetStreamLock.class); | ||
|
||
protected NatsJetStreamLock( | ||
@NonNull Connection connection, @NonNull LockConfiguration lockConfiguration) { | ||
super(lockConfiguration); | ||
this.connection = connection; | ||
} | ||
private final Connection connection; | ||
|
||
@Override | ||
protected void doUnlock() { | ||
var bucketName = String.format("SHEDLOCK-%s", lockConfiguration.getName()); | ||
log.debug("Unlocking for bucketName: {}", bucketName); | ||
var keepLockFor = getSecondUntil(lockConfiguration.getLockAtLeastUntil()); | ||
if (keepLockFor <= 0) { | ||
try { | ||
log.debug("Calling delete on key"); | ||
NatsJetStreamLock(Connection connection, LockConfiguration lockConfiguration) { | ||
super(lockConfiguration); | ||
this.connection = connection; | ||
} | ||
|
||
connection.keyValue(bucketName).delete("LOCKED"); | ||
} catch (Exception e) { | ||
throw new LockException("Can not remove node. " + e.getMessage()); | ||
} | ||
@Override | ||
protected void doUnlock() { | ||
var bucketName = String.format("SHEDLOCK-%s", lockConfiguration.getName()); | ||
log.debug("Unlocking for bucketName: {}", bucketName); | ||
var keepLockFor = geMillisUntil(lockConfiguration.getLockAtLeastUntil()); | ||
if (keepLockFor <= 0) { | ||
try { | ||
log.debug("Calling delete on key"); | ||
connection.keyValue(bucketName).delete("LOCKED"); | ||
} catch (Exception e) { | ||
throw new LockException("Can not remove node. " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static long getSecondUntil(Instant instant) { | ||
return Duration.between(ClockProvider.now(), instant).toMillis(); | ||
} | ||
private static long geMillisUntil(Instant instant) { | ||
return Duration.between(ClockProvider.now(), instant).toMillis(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...jetstream/src/main/java/net/javacrumbs/shedlock/provider/nats/jetstream/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package net.javacrumbs.shedlock.provider.nats.jetstream; | ||
|
||
import net.javacrumbs.shedlock.support.annotation.NonNullApi; | ||
import net.javacrumbs.shedlock.support.annotation.NonNullFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters