-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from rjojjr/minor-refactoring
Release v1.1.2
- Loading branch information
Showing
7 changed files
with
128 additions
and
52 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
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
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
67 changes: 67 additions & 0 deletions
67
src/main/java/rjojjr/com/github/taskslock/entity/TaskLockEntityRepository.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,10 +1,77 @@ | ||
package rjojjr.com.github.taskslock.entity; | ||
|
||
import jakarta.persistence.LockModeType; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Lock; | ||
import org.springframework.stereotype.Repository; | ||
import rjojjr.com.github.taskslock.models.TaskLock; | ||
|
||
import java.util.Date; | ||
import java.util.function.Consumer; | ||
|
||
@ConditionalOnProperty(name = "tasks-lock.client.enabled", havingValue = "false", matchIfMissing = true) | ||
@Repository | ||
public interface TaskLockEntityRepository extends JpaRepository<TaskLockEntity, String> { | ||
|
||
/** | ||
* Attempts to acquire lock & locks 'task_locks' table while doing so. | ||
* @param taskName unique task identifier. | ||
* @param hostName hostname of requesting service/container. | ||
* @param contextId a tracing ID provided by request. | ||
* @param releaseLock Consumer function that releases lock. | ||
* @param cacheLock Consumer function that adds TaskLock object to cache. | ||
* @return resulting TaskLock object. | ||
*/ | ||
@Lock(LockModeType.PESSIMISTIC_READ) | ||
default TaskLock tryToAcquireLock(String taskName, String hostName, String contextId, Consumer<String> releaseLock, Consumer<TaskLock> cacheLock){ | ||
var lockedAt = new Date(); | ||
|
||
var entity = findById(taskName).orElseGet(() -> new TaskLockEntity(taskName, false, hostName, contextId, new Date())); | ||
if (!entity.getIsLocked()) { | ||
entity.setIsLocked(true); | ||
entity.setLockedAt(lockedAt); | ||
entity.setIsLockedByHost(hostName); | ||
entity.setContextId(contextId); | ||
save(entity); | ||
var taskLock = new TaskLock( | ||
taskName, | ||
contextId, | ||
true, | ||
lockedAt, | ||
() -> releaseLock.accept(taskName) | ||
); | ||
cacheLock.accept(taskLock); | ||
return taskLock; | ||
} | ||
flush(); | ||
return new TaskLock( | ||
taskName, | ||
contextId, | ||
false, | ||
lockedAt, | ||
() -> {} | ||
); | ||
} | ||
|
||
/** | ||
* Removes lock for task if it exists | ||
* @param taskName unique task identifier | ||
* @return contextId | ||
*/ | ||
@Lock(LockModeType.PESSIMISTIC_READ) | ||
default String releaseLock(String taskName){ | ||
var entity = findById(taskName).orElseGet(() -> new TaskLockEntity(taskName, false, null, null, new Date())); | ||
var contextId = entity.getContextId(); | ||
if (entity.getIsLocked()) { | ||
entity.setIsLocked(false); | ||
entity.setLockedAt(null); | ||
entity.setIsLockedByHost(null); | ||
entity.setContextId(null); | ||
|
||
save(entity); | ||
} | ||
flush(); | ||
return contextId; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/rjojjr/com/github/taskslock/entity/TaskLockRepository.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,40 @@ | ||
package rjojjr.com.github.taskslock.entity; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.exception.DataException; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import rjojjr.com.github.taskslock.models.TaskLock; | ||
|
||
import java.util.function.Consumer; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@ConditionalOnProperty(name = "tasks-lock.client.enabled", havingValue = "false", matchIfMissing = true) | ||
public class TaskLockRepository { | ||
|
||
private final TaskLockEntityRepository taskLockEntityRepository; | ||
|
||
@Transactional | ||
public TaskLock getTaskLock(String taskName, String hostName, String contextId, Consumer<String> releaseLock, Consumer<TaskLock> cacheLock) { | ||
try { | ||
var taskLock = taskLockEntityRepository.tryToAcquireLock(taskName, hostName, contextId, releaseLock, cacheLock); | ||
if (taskLock.getIsLocked()) { | ||
log.debug("acquired lock for task {} contextId: {}", taskName, contextId); | ||
return taskLock; | ||
} | ||
} catch (DataException e) { | ||
log.debug("Task lock not acquired for task {} because this worker lost in a race condition", taskName); | ||
} | ||
return null; | ||
} | ||
|
||
@Transactional | ||
public String releaseLock(String taskName) { | ||
return taskLockEntityRepository.releaseLock(taskName); | ||
} | ||
} |