Esempio n. 1
0
  @Override
  public ILock acquireLock(final ILockKey lockKey, final String user) throws LockException {
    return storage.write(
        storeProvider -> {
          LockStore.Mutable lockStore = storeProvider.getLockStore();
          Optional<ILock> existingLock = lockStore.fetchLock(lockKey);

          if (existingLock.isPresent()) {
            throw new LockException(
                String.format(
                    "Operation for: %s is already in progress. Started at: %s. Current owner: %s.",
                    formatLockKey(lockKey),
                    new Date(existingLock.get().getTimestampMs()).toString(),
                    existingLock.get().getUser()));
          }

          ILock lock =
              ILock.build(
                  new Lock()
                      .setKey(lockKey.newBuilder())
                      .setToken(tokenGenerator.createNew().toString())
                      .setTimestampMs(clock.nowMillis())
                      .setUser(user));

          lockStore.saveLock(lock);
          return lock;
        });
  }