Search in sources :

Example 1 with LockNotAvailableException

use of com.github.alturkovic.lock.exception.LockNotAvailableException in project flow.service.workflow by boomerang-io.

the class LockManagerImpl method acquireLock.

@Override
public void acquireLock(Task taskExecution, String activityId) {
    long timeout = 60000;
    String key = null;
    if (taskExecution != null) {
        String workflowId = taskExecution.getWorkflowId();
        Map<String, String> properties = taskExecution.getInputs();
        if (properties.get("timeout") != null) {
            String timeoutStr = properties.get("timeout");
            if (!timeoutStr.isBlank() && NumberUtils.isCreatable(timeoutStr)) {
                timeout = Long.valueOf(timeoutStr);
            }
        }
        if (properties.get("key") != null) {
            key = properties.get("key");
            ControllerRequestProperties propertiesList = propertyManager.buildRequestPropertyLayering(null, activityId, workflowId);
            key = propertyManager.replaceValueWithProperty(key, activityId, propertiesList);
        }
        if (key != null) {
            final String test = key;
            Supplier<String> supplier = () -> test;
            String storeID = mongoConfiguration.fullCollectionName("tasks_locks");
            FlowMongoLock mongoLock = new FlowMongoLock(supplier, this.mongoTemplate);
            String storeId = key;
            final List<String> keys = new LinkedList<>();
            keys.add(storeId);
            final String token = mongoLock.acquire(keys, storeID, timeout);
            if (StringUtils.isEmpty(token)) {
                /**
                 * TODO: What to do here.
                 */
                throw new LockNotAvailableException(String.format("Lock not available for keys: %s in store %s", keys, storeId));
            }
            RetryTemplate retryTemplate = getRetryTemplate();
            retryTemplate.execute(ctx -> {
                final boolean lockExists = mongoLock.exists(storeID, token);
                if (lockExists) {
                    throw new LockNotAvailableException(String.format("Lock hasn't been released yet for: %s in store %s", keys, storeId));
                }
                return lockExists;
            });
        } else {
            LOGGER.info("No Acquire Lock Key Found!");
        }
    }
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) LockNotAvailableException(com.github.alturkovic.lock.exception.LockNotAvailableException) FlowMongoLock(io.boomerang.util.FlowMongoLock) LinkedList(java.util.LinkedList)

Aggregations

LockNotAvailableException (com.github.alturkovic.lock.exception.LockNotAvailableException)1 FlowMongoLock (io.boomerang.util.FlowMongoLock)1 LinkedList (java.util.LinkedList)1 RetryTemplate (org.springframework.retry.support.RetryTemplate)1