> ## Documentation Index
> Fetch the complete documentation index at: https://botmesh.afaya.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# ScheduleStore

> Interface exported by @botmesh/core.

Defined in: [scheduler/ScheduleStore.ts:77](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L77)

Storage for durable scheduled tasks (`BotConfig.scheduler.store`). In memory by
default; `SQLiteStore` and `RedisStore` (among others) provide one through `store.schedules()`.

## Methods

### start()?

```ts theme={null}
start?(): Promise<void>;
```

Defined in: [scheduler/ScheduleStore.ts:79](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L79)

Optional setup (schema, connection) — called by the scheduler before first use. Must be idempotent.

#### Returns

`Promise`\<`void`>

***

### get()

```ts theme={null}
get(id): Promise<ScheduleRecord | null>;
```

Defined in: [scheduler/ScheduleStore.ts:80](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L80)

#### Parameters

| Parameter | Type |
| - | - |
| `id` | `string` |

#### Returns

`Promise`\<[`ScheduleRecord`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ScheduleRecord) | `null`>

***

### register()

```ts theme={null}
register(rec, now): Promise<ScheduleRecord>;
```

Defined in: [scheduler/ScheduleStore.ts:87](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L87)

Insert the task if absent. If it exists with the same `kind` and `spec`, keep it
untouched (that is how a restart keeps its `nextRunAt`); if either differs, overwrite
`kind`, `spec` and `nextRunAt` with the given ones (the fencing token is kept). Returns
the stored record.

#### Parameters

| Parameter | Type |
| - | - |
| `rec` | `Pick`\<[`ScheduleRecord`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ScheduleRecord), `"id"` \| `"kind"` \| `"spec"` \| `"nextRunAt"`> |
| `now` | `number` |

#### Returns

`Promise`\<[`ScheduleRecord`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ScheduleRecord)>

***

### claimDue()

```ts theme={null}
claimDue(
   id, 
   owner, 
   now, 
   leaseMs
): Promise<ClaimResult | null>;
```

Defined in: [scheduler/ScheduleStore.ts:94](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L94)

Atomic claim: succeeds only when `nextRunAt !== null && nextRunAt <= now` and the
record has no live lease (`leaseUntil` absent or `<= now`). Then it sets
`leaseOwner = owner`, `leaseUntil = now + leaseMs` and increments `fencingToken`.
Returns `null` otherwise (not due, leased elsewhere, or unknown id).

#### Parameters

| Parameter | Type |
| - | - |
| `id` | `string` |
| `owner` | `string` |
| `now` | `number` |
| `leaseMs` | `number` |

#### Returns

`Promise`\<[`ClaimResult`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ClaimResult) | `null`>

***

### renew()

```ts theme={null}
renew(
   id, 
   fencingToken, 
   leaseUntil
): Promise<boolean>;
```

Defined in: [scheduler/ScheduleStore.ts:96](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L96)

Set `leaseUntil` only if `fencingToken` still matches; `false` means the lease was lost.

#### Parameters

| Parameter | Type |
| - | - |
| `id` | `string` |
| `fencingToken` | `number` |
| `leaseUntil` | `number` |

#### Returns

`Promise`\<`boolean`>

***

### complete()

```ts theme={null}
complete(
   id, 
   fencingToken, 
   patch, 
   now
): Promise<boolean>;
```

Defined in: [scheduler/ScheduleStore.ts:101](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L101)

Commit an occurrence only if `fencingToken` still matches: write the patch, clear the
lease and set `updatedAt = now`. `false` when stale (claimed again since) or deleted.

#### Parameters

| Parameter | Type |
| - | - |
| `id` | `string` |
| `fencingToken` | `number` |
| `patch` | [`CompletePatch`](https://botmesh.afaya.com.co/api/core/spi/interfaces/CompletePatch) |
| `now` | `number` |

#### Returns

`Promise`\<`boolean`>

***

### delete()

```ts theme={null}
delete(id): Promise<void>;
```

Defined in: [scheduler/ScheduleStore.ts:106](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L106)

Remove the record. The id's fencing token is kept as a high-water mark: a later
`register()` of the same id starts from it, so tokens never go backwards per id.

#### Parameters

| Parameter | Type |
| - | - |
| `id` | `string` |

#### Returns

`Promise`\<`void`>

***

### list()

```ts theme={null}
list(): Promise<ScheduleRecord[]>;
```

Defined in: [scheduler/ScheduleStore.ts:108](https://github.com/andresayac/botmesh/blob/main/packages/core/src/scheduler/ScheduleStore.ts#L108)

Every stored record (orphans included), in no particular order.

#### Returns

`Promise`\<[`ScheduleRecord`](https://botmesh.afaya.com.co/api/core/spi/interfaces/ScheduleRecord)\[]>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.