Skip to content
Sarj Workspace Sync

identity-sync/resource-lease

@sarj-ai/workspace-sync/identity-sync/resource-lease ยท Source

import type { D1Database } from "@cloudflare/workers-types";
import { z } from "zod";
declare const ResourceKeySchema: z.core.$ZodBranded<z.ZodString, "ResourceLeaseKey", "out">;
declare const GenerationSchema: z.core.$ZodBranded<z.ZodNumber, "ResourceLeaseGeneration", "out">;
declare const ActiveResourceLeaseSchema: z.ZodObject<{
expiresAtMs: z.core.$ZodBranded<z.ZodNumber, "EpochMilliseconds", "out">;
generation: z.core.$ZodBranded<z.ZodNumber, "ResourceLeaseGeneration", "out">;
ownerId: z.core.$ZodBranded<z.ZodUUID, "ResourceLeaseOwnerId", "out">;
resourceKey: z.core.$ZodBranded<z.ZodString, "ResourceLeaseKey", "out">;
}, z.core.$strict>;
export type ResourceLeaseKey = z.output<typeof ResourceKeySchema>;
export type ResourceLeaseGeneration = z.output<typeof GenerationSchema>;
export type ActiveResourceLease = z.output<typeof ActiveResourceLeaseSchema>;
export type ResourceLeaseAcquisition = {
readonly kind: "acquired";
readonly lease: ActiveResourceLease;
} | {
readonly kind: "busy";
};
export interface ResourceLeaseRepository {
acquire(resourceKey: ResourceLeaseKey, durationMs: number): Promise<ResourceLeaseAcquisition>;
assertActive(lease: ActiveResourceLease, minRemainingMs?: number): Promise<void>;
release(lease: ActiveResourceLease): Promise<void>;
}
export type ResourceLeaseExecution<Result> = {
readonly kind: "busy";
} | {
readonly kind: "completed";
readonly result: Result;
};
export declare class ResourceLeaseFencedError extends Error {
readonly generation: ResourceLeaseGeneration;
readonly resourceKey: ResourceLeaseKey;
constructor(lease: ActiveResourceLease, options?: ErrorOptions);
}
export declare function resourceLeaseKey(value: string): ResourceLeaseKey;
export declare function activeResourceLease(value: z.input<typeof ActiveResourceLeaseSchema>): ActiveResourceLease;
export declare function withResourceLease<Result>(repository: ResourceLeaseRepository, resourceKey: ResourceLeaseKey, durationMs: number, operation: (lease: ActiveResourceLease) => Promise<Result>): Promise<ResourceLeaseExecution<Result>>;
export declare const SLACK_USER_GROUPS_RESOURCE_LEASE_KEY: string & z.$brand<"ResourceLeaseKey">;
export declare class D1ResourceLeaseRepository implements ResourceLeaseRepository {
#private;
constructor(db: D1Database, options?: {
readonly clock?: () => number;
readonly ownerIdFactory?: () => string;
});
acquire(resourceKey: ResourceLeaseKey, durationMs: number): Promise<ResourceLeaseAcquisition>;
assertActive(lease: ActiveResourceLease, rawMinRemainingMs?: number): Promise<void>;
release(lease: ActiveResourceLease): Promise<void>;
}
export {};