Skip to main content

Lock

Locks make sure that only one package execution at a time runs a specific part of a script, for example to avoid that two executions create the same record in an external system.

A package execution that requests a lock held by another execution waits until the lock is released, or until the execution reaches its maximum execution time. A lock is held until it is released with lock_release, or until the package execution that holds it ends.

A lock is identified by its name, a string of at most 100 characters.

Locks are scoped to a profile and environment, and can be used across packages within that environment.

Note that waiting package executions are NOT guaranteed to get the lock in the order in which they requested it.

lock_request​

Requests the lock with the given name, and waits until it is acquired.

Requesting a lock that the package execution already holds returns right away.

Returns a lock object, which can be passed to lock_release instead of the name.

lock_request("lock_name");

Example:

const lock_name = "create_customer";
lock_request(lock_name);
// do something that must not run in parallel
lock_release(lock_name);

lock_release​

Releases the lock with the given name, so that a waiting package execution can acquire it.

Fails when the package execution does not hold the lock.

lock_release("lock_name");

Example:

const lock_name = "create_customer";
lock_request(lock_name);
// do something that must not run in parallel
lock_release(lock_name);

Deprecated functions​

The functions in this section are deprecated. They still work, but you are encouraged to use the indicated alternatives.

request_lock​

warning

The request_lock function is deprecated. Use lock_request instead.

Same as lock_request.

const lock = request_lock("lock_name");

release_lock​

warning

The release_lock function is deprecated. Use lock_release instead.

Same as lock_release.

release_lock(lock);

refresh_lock​

warning

The refresh_lock function is deprecated. Remove it from your packages.

Does nothing. Locks are held until they are released, or until the package execution ends.

refresh_lock(lock);