# ๐Ÿ’พ 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