Skip to main content

File locks

In one sentenceA file lock makes the runs of a flow update a saved file one at a time: the first run takes the lock, the runs that arrive meanwhile are held in order, and each starts when the run before it ends.

Why you need one​

Two emails a minute apart can start two runs. Both read the same saved workbook, each makes a changed copy from what it read, and each saves it. The second copy is built from a workbook that no longer has the first run's changes, so approving it silently drops them. Nobody sees an error.

A file lock removes the race. The runs take turns on the file, and each one starts from what the run before it saved.

Declare the lock​

List the files in the flow's spec.locks. A file is a path in Files under saved/.

spec:
locks:
- file: saved/Price lists/prices.xlsx
  • Declare the same path on every flow that saves that workbook. Flows that list the same path wait for each other.
  • Use the file's own name, not one of the numbered copies a save leaves beside it (prices (2).xlsx). The lock is on the path exactly as written, so two flows that spell it differently do not wait for each other.
  • A flow can lock up to 8 files. A run that needs several takes all of them or none, so two runs never wait for each other in a circle.
  • The path starts with saved/, and has no . or .. parts.

Locks are set in YAML (or through the API and Terraform). The flow editor does not have a field for them.

How runs take turns​

Three runs queue for one fileTwo pictures side by side, each with three runs on the left and the shared file price-list.xlsx on the right. In the first picture, run 1 is running and holds the file's lock, shown by an arrow from run 1 to the file. Run 2 and run 3 are held, in the order they arrived, and each waits for the file: run 2 first, run 3 second. In the second picture, run 1 has ended and given the lock back. Run 1 is succeeded and has no arrow. Run 2, the oldest held run, has taken the lock and is running, with an arrow to the file. Run 3 is still held and now waits for run 2.While run 1 worksRun 1runningholds the lockRun 2held, firstwaitsRun 3held, secondwaitsprice-list.xlsxAfter run 1 endsRun 1succeededRun 2runningholds the lockRun 3heldwaits for run 2price-list.xlsx
One run at a time works on the file. The runs that arrive meanwhile are held in order, and the oldest one takes the lock the moment the holder ends.
  1. Run 1 takes the lock when it starts and works on the file.
  2. Run 2 and run 3 arrive while it works. Each finds the lock taken, so each is held, with the reason "file", and none of its steps run. They queue in the order they arrived.
  3. When run 1 ends, the lock goes back in the same moment, and the oldest held run, run 2, takes it and starts. It reads the file as run 1 saved it.
  4. Run 3 keeps waiting, now for run 2. It starts when run 2 ends.

The lock belongs to a run from its first step to its end. That includes the steps that only read the file, and any wait for an approval or a reply, because the run that holds the file is not finished until the person answers.

When a lock is released​

The lock is given back in the same transaction that ends the run, whatever the end:

  • the run succeeded;
  • the run failed after its last attempt;
  • the run was cancelled;
  • an approval was rejected, or timed out.

A run that is held for another reason, such as a used-up budget, takes its locks when it is let go, and waits for the file then if it is taken.

Test runs (the editor's Test, and evaluations) take no lock and are never held by one.

The acknowledge step​

When an email starts the run, the sender is left wondering whether it arrived and when it will be processed. Confirm receipt and take the file (acknowledge) answers that. Put it first in the flow, before any step that reads the file.

It does two things:

  1. It takes every lock of the run in one step.
  2. It emails the sender, in the same email thread that later questions and approvals use, to say what happens next:
    • the lock is free: "Received, processing now", and the run goes on;
    • the lock is taken: "Received. Waiting for an earlier update (the subject of the email the other run is working on) to finish; it will start automatically". The run is held right after this step.

When the earlier run ends and this run starts, it passes through acknowledge once more and sends a short "Starting now" in the same thread, then goes on to the step after it.

The step costs no credits. It needs a mail connection, and it sends nothing when the run has no email to answer (a schedule or a manual run).

SettingWhat it does
toWho is told. The sender of the email that started the run when empty ({email.from}).
langhe or en. Empty follows the language of the email.
startNoticeOn by default. Off means a waiting run goes straight on, without the "Starting now" email.
messagesYour own wording for the three emails (processing, waiting, starting), in en and he. {lock.waitingFor} is the subject of the email the holding run is working on.

It also adds the fields waited (true if the run had to wait), waitingFor (the subject it waited for) and file (the file it waited for), which later steps can use.

Without the step

A flow that declares locks but has no acknowledge step still takes turns: it takes its locks the moment the run is queued, and sends no email. Use acknowledge when the sender should be told.

An example flow​

This flow updates a price list from each supplier email. The lock keeps two emails from updating the same workbook at once, and acknowledge tells each sender where they stand.

locks-price-update.yaml
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
name: locks-price-update
namespace: tenant-acme
spec:
displayName: Update the price list from supplier emails
locks:
- file: saved/Price lists/prices.xlsx
triggers:
- id: email
ref: {name: email-arrives}
connectorRef: {name: invoices-inbox}
config: {attachments: together, requireAttachment: true}
next: [ack]
nodes:
- id: ack
title: Confirm receipt and take the file
stepRef: {name: acknowledge}
config: {lang: en}
next: [update]
- id: update
stepRef: {name: update-spreadsheet}
config:
file: saved/Price lists/prices.xlsx
changes:
- {sheet: Prices, match: {Item: Paper A4}, column: Price, value: "24.90", type: number, source: Supplier email}
next: [save]
- id: save
stepRef: {name: save-file}
config: {destination: lumen, folder: Price lists, ifExists: replace}

The path in locks, the file that update-spreadsheet reads and the folder and name that save-file writes are the same file. ifExists: replace keeps each earlier version in the folder's .versions folder, so the file stays at one path for the next run to read.

What you see on the run​

A held run shows the status On hold in the runs list. Its page says Waiting for saved/Price lists/prices.xlsx: another run is updating it, with an icon whose tooltip explains: "Runs that update the same file go one at a time. This one starts when the run that has the file ends." A link, Open the run that has the file, goes to the run that holds the lock. When the holder is not known there is no link.

The sender sees the same in their inbox, from acknowledge.

A lock held too long​

A run that waits on a person (an approval nobody gives) can hold a lock for a long time. After 7 days Lumen sends a notification to the workspace's admins, once per holding run: "<path> has been locked for N days". Lumen never breaks a lock by itself, because that would let two runs write the same file. Cancel the holding run, or answer its approval, and the next run starts.

A workspace admin can cancel a run that has not ended, held or not, from the run page or the runs list (the cancel icon asks for confirmation and an optional reason), or with the CLI:

Cancel the run that holds the file
lumen cancel run RUN --reason "the supplier withdrew the update"

The lock then passes to the oldest held run. A cancelled run can be run again with Retry.

Next steps​