# ๐พ Job State Persistence
**๐ Resume interrupted tasks and track progress across runs**
*Never lose your progress again*
---
## ๐ฏ What is Job State Persistence?
Job state persistence allows the script to **resume interrupted tasks** and **track progress** across multiple runs, ensuring no work is lost when the script is stopped or crashes.
### **Key Features**
- ๐ **Resumable tasks** โ Pick up exactly where you left off
- ๐
**Daily tracking** โ Date-specific progress monitoring
- ๐ค **Per-account isolation** โ Independent progress for each account
- ๐ก๏ธ **Corruption protection** โ Atomic writes prevent data loss
- ๐ **Performance optimized** โ Minimal overhead
---
## โ๏ธ Configuration
### **Basic Setup**
```json
{
"jobState": {
"enabled": true,
"dir": ""
}
}
```
### **Configuration Options**
| Setting | Description | Default |
|---------|-------------|---------|
| `enabled` | Enable job state persistence | `true` |
| `dir` | Custom directory for state files | `""` (uses `sessions/job-state`) |
---
## ๐๏ธ How It Works
### **State Tracking**
- ๐ **Monitors completion** status of individual activities
- ๐ **Tracks progress** for daily sets, searches, and promotional tasks
- โ **Prevents duplicates** when script restarts
### **Storage Structure**
```
sessions/job-state/
โโโ account1@email.com/
โ โโโ daily-set-2025-01-20.json
โ โโโ desktop-search-2025-01-20.json
โ โโโ mobile-search-2025-01-20.json
โโโ account2@email.com/
โโโ daily-set-2025-01-20.json
โโโ promotional-tasks-2025-01-20.json
```
### **State File Format**
```json
{
"date": "2025-01-20",
"account": "user@email.com",
"type": "daily-set",
"completed": [
"daily-check-in",
"quiz-1",
"poll-1"
],
"remaining": [
"quiz-2",
"search-desktop"
],
"lastUpdate": "2025-01-20T10:30:00.000Z"
}
```
---
## ๐ Key Benefits
### **Resumable Tasks**
- โ
**Script restarts** pick up where they left off
- โ
**Individual completion** is remembered
- โ
**Avoid re-doing** completed activities
### **Daily Reset**
- ๐
**Date-specific** state files
- ๐
**New day** automatically starts fresh tracking
- ๐ **History preserved** for analysis
### **Account Isolation**
- ๐ค **Separate state** per account
- โก **Parallel processing** doesn't interfere
- ๐ **Independent progress** tracking
---
## ๐ Use Cases
### **Interrupted Executions**
| Scenario | Benefit |
|----------|---------|
| **Network issues** | Resume when connection restored |
| **System reboots** | Continue after restart |
| **Manual termination** | Pick up from last checkpoint |
| **Resource exhaustion** | Recover without losing progress |
### **Selective Reruns**
| Feature | Description |
|---------|-------------|
| **Skip completed sets** | Avoid redoing finished daily activities |
| **Resume searches** | Continue partial search sessions |
| **Retry failed tasks** | Target only problematic activities |
| **Account targeting** | Process specific accounts only |
### **Progress Monitoring**
- ๐ **Track completion rates** across accounts
- ๐ **Identify problematic** activities
- โฑ๏ธ **Monitor task duration** trends
- ๐ **Debug stuck** or slow tasks
---
## ๐ ๏ธ Technical Implementation
### **Checkpoint Strategy**
- ๐พ **State saved** after each completed activity
- โ๏ธ **Atomic writes** prevent corruption
- ๐ **Lock-free design** for concurrent access
### **Performance Optimization**
- โก **Minimal I/O overhead** โ Fast state updates
- ๐ง **In-memory caching** โ Reduce disk access
- ๐ฅ **Lazy loading** โ Load state files on demand
### **Error Handling**
- ๐ง **Corrupted files** are rebuilt automatically
- ๐ **Missing directories** created as needed
- ๐ฏ **Graceful degradation** when disabled
---
## ๐๏ธ File Management
### **Automatic Behavior**
- ๐
**Date-specific files** โ New files for each day
- ๐พ **Preserved history** โ Old files remain for reference
- ๐ **No auto-deletion** โ Manual cleanup recommended
### **Manual Maintenance**
```powershell
# Clean state files older than 7 days
Get-ChildItem sessions/job-state -Recurse -Filter "*.json" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item
# Reset all job state (start fresh)
Remove-Item -Recurse -Force sessions/job-state/
# Reset specific account state
Remove-Item -Recurse -Force sessions/job-state/user@email.com/
```
---
## ๐ Example Workflows
### **Interrupted Daily Run**
```
Day 1 - 10:30 AM:
โ
Account A: Daily set completed
๐ Account B: 3/5 daily tasks done
โ Script crashes
Day 1 - 2:00 PM:
๐ Script restarts
โ
Account A: Skipped (already complete)
๐ Account B: Resumes with 2 remaining tasks
```
### **Multi-Day Tracking**
```
Monday:
๐
daily-set-2025-01-20.json created
โ
All tasks completed
Tuesday:
๐
daily-set-2025-01-21.json created
๐ Fresh start for new day
๐ Monday's progress preserved
```
---
## ๐ Debugging Job State
### **State Inspection**
```powershell
# View current state for account
Get-Content sessions/job-state/user@email.com/daily-set-2025-01-20.json | ConvertFrom-Json
# List all state files
Get-ChildItem sessions/job-state -Recurse -Filter "*.json"
```
### **Debug Output**
Enable verbose logging to see state operations:
```powershell
$env:DEBUG_REWARDS_VERBOSE=1; npm start
```
Sample output:
```
[INFO] Loading job state for user@email.com (daily-set)
[INFO] Found 3 completed tasks, 2 remaining
[INFO] Skipping completed task: daily-check-in
[INFO] Starting task: quiz-2
```
---
## ๐ ๏ธ Troubleshooting
| Problem | Cause | Solution |
|---------|-------|----------|
| **Tasks not resuming** | Missing/corrupt state files | Check file permissions; verify directory exists |
| **Duplicate execution** | Clock sync issues | Ensure system time is accurate |
| **Excessive files** | No cleanup schedule | Implement regular state file cleanup |
| **Permission errors** | Write access denied | Verify sessions/ directory is writable |
### **Common Issues**
#### **Tasks Not Resuming**
```
[ERROR] Failed to load job state: Permission denied
```
**Solutions:**
- โ
Check file/directory permissions
- โ
Verify state directory exists
- โ
Ensure write access to sessions/
#### **Duplicate Task Execution**
```
[WARN] Task appears to be running twice
```
**Solutions:**
- โ
Check for corrupt state files
- โ
Verify system clock synchronization
- โ
Clear state for affected account
#### **Storage Growth**
```
[INFO] Job state directory: 2.3GB (1,247 files)
```
**Solutions:**
- โ
Implement regular cleanup schedule
- โ
Remove old state files (7+ days)
- โ
Monitor disk space usage
---
## ๐ค Integration Features
### **Session Persistence**
- ๐ช **Works alongside** browser session storage
- ๐ **Complements** cookie and fingerprint persistence
- ๐ **Independent of** proxy and authentication state
### **Clustering**
- โก **Isolated state** per cluster worker
- ๐ซ **No shared state** between parallel processes
- ๐ **Worker-specific** directories
### **Scheduling**
- โฐ **Persists across** scheduled runs
- ๐
**Daily reset** at midnight automatically
- ๐ **Long-running continuity** maintained
---
## โ๏ธ Advanced Configuration
### **Custom State Directory**
```json
{
"jobState": {
"enabled": true,
"dir": "/custom/path/to/state"
}
}
```
### **Disabling Job State**
```json
{
"jobState": {
"enabled": false
}
}
```
**Effects when disabled:**
- โ **Tasks restart** from beginning each run
- โ **No progress tracking** between sessions
- โ **Potential duplicate work** on interruptions
- โ
**Slightly faster startup** (no state loading)
---
## ๐ Best Practices
### **Development**
- โ
**Enable for testing** โ Consistent behavior
- ๐งน **Clear between changes** โ Fresh state for major updates
- ๐ **Monitor for debugging** โ State files reveal execution flow
### **Production**
- โ
**Always enabled** โ Reliability is critical
- ๐พ **Regular backups** โ State directory backups
- ๐ **Monitor disk usage** โ Prevent storage growth
### **Maintenance**
- ๐๏ธ **Weekly cleanup** โ Remove old state files
- ๐ **Health checks** โ Verify state integrity
- ๐ **Usage monitoring** โ Track storage trends
---
## ๐ Related Guides
- **[Getting Started](./getting-started.md)** โ Initial setup and configuration
- **[Scheduler](./schedule.md)** โ Automated timing and execution
- **[Diagnostics](./diagnostics.md)** โ Error capture and debugging
- **[Security](./security.md)** โ Privacy and data protection