#28 - Periodically backup service state and restore state if serive is restarted #30

Merged
henry merged 5 commits from restore-state into main 2025-07-04 16:08:14 +00:00
1 changed files with 24 additions and 0 deletions
Showing only changes of commit e7aad22295 - Show all commits

View File

@ -83,6 +83,8 @@ Configuration is set in `appsettings.json` or via environment variables. The mai
- **ExpirationSeconds**: How long (in seconds) before an access ticket expires (default: 43200).
- **RollingExpiration**: If true, the expiration timer resets on activity (default: true).
- **CleanupIntervalSeconds**: How often (in seconds) the background cleanup runs to remove expired/inactive users (default: 60).
- **BackupFilePath**: The file path where the access queue state will be periodically saved (no default; optional).
- **BackupIntervalSeconds**: How often (in seconds) the state is backed up to disk (no default; optional).
Example `appsettings.json`:
```json
@ -97,6 +99,28 @@ Example `appsettings.json`:
}
```
## State Persistence and Backup
AccessQueueService automatically saves its in-memory state (active users and queue) to disk at regular intervals and restores it on startup. This helps prevent data loss in case of unexpected shutdowns or restarts.
- **Backup Location:** The backup file path is set via the `AccessQueue:BackupFilePath` configuration variable. If this is not set, no backup will be performed.
- **Backup Interval:** The frequency of backups is controlled by `AccessQueue:BackupIntervalSeconds` (in seconds). If this is not set or is zero, backups are disabled.
- **Startup Restore:** On startup, if a backup file exists at the specified path, the service will attempt to restore the previous state from this file. If the file is missing or corrupted, the service starts with an empty queue and access list.
- **Failure Handling:** Any errors during backup or restore are logged, but do not prevent the service from running.
- **Backup Format:** The backup is saved as a JSON file containing the current state of the access queue and active users.
Example configuration:
```json
{
"AccessQueue": {
"BackupFilePath": "Logs/backup.json",
"BackupIntervalSeconds": 60
}
}
```
> **Note:** Ensure the backup file path is writable by the service. Regular backups help prevent data loss in case of unexpected shutdowns.
## AccessResponse Object
The `AccessResponse` object returned by the API contains the following properties: