[**Documentation**](../../../README)

***

[Documentation](../../../README) / [zerotal](../../README) / [lock](../README) / Lock

# Class: Lock

Defined in: [packages/core/src/lock/facades/Lock.ts:57](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/lock/facades/Lock.ts#L57)

Static facade over the container-bound [LockManager](LockManager).

Resolves the live `lock` singleton on every call and forwards to the
manager, so it exposes the same three usage styles: [try](#try)
(fail fast), [block](#block) (wait for the lock), and
[make](#make) (a manual [ManagedLock](ManagedLock) handle). Requires
[LockProvider](LockProvider) to be registered.

## Example

```ts
// Fail-fast — throws immediately if the lock is busy
await Lock.try('invoice:123', 10, async () => {
  await processInvoice(123);
});

// Blocking — waits up to 30s for the lock
await Lock.block('invoice:123', 10, async () => {
  await processInvoice(123);
}, { timeout: 30 });

// Manual handle for complex flows
const handle = Lock.make('payment:456', 15);
if (await handle.acquire()) {
  try {
    await capturePayment(456);
  } finally {
    await handle.release();
  }
}
```

## Constructors

### Constructor

> **new Lock**(): `Lock`

#### Returns

`Lock`

## Acquiring

### make()

> `static` **make**(`key`, `ttlSeconds`): [`ManagedLock`](ManagedLock)

Defined in: [packages/core/src/lock/facades/Lock.ts:67](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/lock/facades/Lock.ts#L67)

Create a named lock handle for manual acquire/release flows.
Does NOT acquire the lock — call `.acquire()` or `.block()` explicitly.

#### Parameters

##### key

`string`

Logical lock name.

##### ttlSeconds

`number`

Lock time-to-live in seconds.

#### Returns

[`ManagedLock`](ManagedLock)

#### Throws

(code `E_LOCK_FACADE_UNAVAILABLE`) if [LockProvider](LockProvider) is not registered.

***

### try()

> `static` **try**\<`T`\>(`key`, `ttlSeconds`, `callback`): `Promise`\<`T`\>

Defined in: [packages/core/src/lock/facades/Lock.ts:82](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/lock/facades/Lock.ts#L82)

Acquire the lock exactly once, run `callback`, then release — always,
even if `callback` throws.

#### Type Parameters

##### T

`T`

#### Parameters

##### key

`string`

Logical lock name.

##### ttlSeconds

`number`

Lock time-to-live in seconds.

##### callback

() => `T` \| `Promise`\<`T`\>

Critical section to run while the lock is held.

#### Returns

`Promise`\<`T`\>

The value returned by `callback`.

#### Throws

Immediately, if the lock is busy.

***

### block()

> `static` **block**\<`T`\>(`key`, `ttlSeconds`, `callback`, `options?`): `Promise`\<`T`\>

Defined in: [packages/core/src/lock/facades/Lock.ts:98](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/lock/facades/Lock.ts#L98)

Wait until the lock is free (up to `options.timeout` seconds), run
`callback`, then release — always, even if `callback` throws.

#### Type Parameters

##### T

`T`

#### Parameters

##### key

`string`

Logical lock name.

##### ttlSeconds

`number`

Lock time-to-live in seconds.

##### callback

() => `T` \| `Promise`\<`T`\>

Critical section to run while the lock is held.

##### options?

[`BlockOptions`](../interfaces/BlockOptions)

Wait [timeout](../interfaces/BlockOptions#timeout) and poll interval.

#### Returns

`Promise`\<`T`\>

The value returned by `callback`.

#### Throws

If the timeout elapses before the lock is acquired.

***

### NotAcquired

> `readonly` `static` **NotAcquired**: *typeof* [`LockNotAcquiredError`](LockNotAcquiredError) = `LockNotAcquiredError`

Defined in: [packages/core/src/lock/facades/Lock.ts:113](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/lock/facades/Lock.ts#L113)

The [LockNotAcquiredError](LockNotAcquiredError) class, re-exported for convenience in
`catch` blocks (`err instanceof Lock.NotAcquired`).
