- Home
- Skills
- APIs & Backend
- Lease-Fenced Single-Owner Worker with Standby Failover
Lease-Fenced Single-Owner Worker with Standby Failover
Two computers run the worker so one can reboot or lose power, and the second must never act while the first still can.
$39
Works with the AI Agents you already use
Lease-Fenced Single-Owner Worker with Standby Failover
Example session with this skill installed
Set up a lease-fenced worker for mailbox-1 on two nodes, 'desk' and 'tower', using a shared control plane. Show me the server and client implementation.
- Read your context and instructions
- Compiled the lease-fenced single-owner worker
The following implementation configures the lease server on the control plane and initializes the OwnerLeaseClient for two worker nodes.
Lease-Fenced Implementation
// Control Plane: Server Setup
import { openLeaseStore } from './scripts/lease-store.mjs';
import { createLeaseServer } from './scripts/lease-server.mjs';
const store = openLeaseStore('./leases.db');
const server = createLeaseServer({
store,
secrets: new Map([
['desk', process.env.DESK_SECRET],
['tower', process.env.TOWER_SECRET]
])
});
server.listen(8731, '127.0.0.1');
// Worker Node: Client Setup
import { OwnerLeaseClient } from './scripts/lease-client.mjs';
import { httpExchange } from './scripts/lease-server.mjs';
const client = new OwnerLeaseClient({
workerId: 'desk', // or 'tower'
accountId: 'mailbox-1',
exchange: httpExchange({
baseUrl: 'https://control.internal',
secret: process.env.DESK_SECRET
}),
onLost: async (reason) => {
console.error('Lease lost:', reason);
process.exit(1);
},
});
await client.start();
// Resource access with fencing
if (client.assertPermitted()) {
const stamp = client.stamp;
// Perform work...
store.recordStep('mailbox-1', 'desk', 'inbox open', { ready: true });
}
Next steps
- Run the 14-test suite with
node --test scripts/failover.test.mjsto verify takeover timing. - Configure a TLS reverse proxy if workers are communicating with the server over a public network.
- Adjust
HEALTH_HANDOVER_PAUSE_MSin your environment to define how long a worker stays paused after a health failure.
Connects securely to your tools. The creator never sees your data.
About this skill
Some resources allow exactly one driver: a browser signed in to a third-party account that logs out when a second session appears, a phone attached over USB, a mailbox polled by IMAP. Two computers run the worker so one can reboot or lose power, and the second must never act while the first still can. A permanent primary with a manually started backup is a cold spare, not failover, and a returning primary resumes with a dead session it believes is alive. This kit is the ownership lease, the fence every action passes through, the client that holds a grant correctly, and the rules that decide when an owner should give up.
lease-store.mjs (node:sqlite): one transaction per decision; acquire, renew and release; a 45-second lease; takeover only after expiry plus a 15-second grace; a generation incremented on every takeover and never on renewal; assertOwnership as the fence (worker, instance, generation, expiry, primary record and recovery block all checked); stuck-owner release when no ready step was recorded for 180 s or one step ran 120 s, with a five-minute pause; heartbeat rules so an idle duplicate cannot overwrite the owner's health; manual promotion that never evicts.
lease-client.mjs: wall and monotonic deadlines, transit time counted inside the lease, a 10-second safety margin before the server's expiry, a failed renewal keeps the grant and retries in two seconds (one dropped renewal used to close the owner's browser on every short server restart), standby replies honoured with the server's retry interval, onLost called once so the session is closed before anything else happens.
lease-server.mjs: loopback HTTP with HMAC authentication over method, path, timestamp, nonce and body hash; 300 s skew; nonce replay refused; a worker cannot sign for another. health-failover.mjs: yield after five unhealthy cycles in two minutes or sixty seconds without sustained recovery; three ready cycles clear the history. recovery-budget.mjs: five attempts per computer per round, rounds that close on running computers only (an offline computer cannot lock the other out), reopen for a computer that never spent its attempts, resolution after three ready observations, silent-episode detection when every heartbeat is stale and nobody holds the lease.
Reference: the contract table, the fence, eight production lessons and how to wire the budget to the store. 14 tests including an HTTP round trip. Node 22.5+, no packages.
Requires the control plane to be reachable; workers stop if the lease server is down. Default 45s/10s timings are tuned for two-node setups. Requires Node 22.5+.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 18 days ago
Requires