Skip to content

Latest commit

 

History

History
184 lines (139 loc) · 4.66 KB

File metadata and controls

184 lines (139 loc) · 4.66 KB

BrickSwitch Automatic Scheduler Setup

The BrickSwitch app now includes automatic schedule execution. You have several options for running schedules automatically:

Option 1: Built-in Background Scheduler (Recommended for Development)

The Streamlit app includes a built-in background scheduler that runs within the app:

  1. Start the scheduler: Click "▶️ Start Auto Scheduler" in the web interface
  2. Monitor status: The scheduler status is shown as "🟢 Running" or "🔴 Stopped"
  3. View logs: Click "📜 View Logs" to see execution history

Pros: Easy to use, integrated with the web interface Cons: Only runs while the Streamlit app is running

Option 2: Standalone Scheduler Script (Recommended for Production)

Use the standalone scheduler.py script for more robust operation:

Manual Execution

# Run once
python scheduler.py

# Run continuously (daemon mode)
python scheduler.py --daemon

# Run with custom interval (every 5 minutes)
python scheduler.py --daemon --interval 300

Cron Job Setup

Add to your crontab (crontab -e):

# Run every minute
* * * * * cd /path/to/brickswitch && python scheduler.py

# Run every 5 minutes
*/5 * * * * cd /path/to/brickswitch && python scheduler.py

# Run every hour
0 * * * * cd /path/to/brickswitch && python scheduler.py

systemd Service (Linux)

  1. Edit the service file:

    sudo cp brickswitch-scheduler.service /etc/systemd/system/
    sudo nano /etc/systemd/system/brickswitch-scheduler.service
  2. Update the paths in the service file:

    • Replace /path/to/brickswitch with your actual directory
    • Replace /path/to/your/python/env/bin with your Python environment
    • Replace your-username with your actual username
  3. Enable and start the service:

    sudo systemctl daemon-reload
    sudo systemctl enable brickswitch-scheduler
    sudo systemctl start brickswitch-scheduler
  4. Check status:

    sudo systemctl status brickswitch-scheduler
    sudo journalctl -u brickswitch-scheduler -f

Option 3: Docker Container

Create a Dockerfile for containerized deployment:

FROM python:3.9-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
CMD ["python", "scheduler.py", "--daemon"]

Logs and Monitoring

Log Files

  • Execution logs: schedule_execution.log
  • Contains: Timestamps, actions taken, errors

Log Monitoring

# View real-time logs
tail -f schedule_execution.log

# View recent logs
tail -n 50 schedule_execution.log

# Search for errors
grep -i error schedule_execution.log

Configuration Files

Schedule Storage

  • File: app_schedules.json
  • Format: JSON with app schedules
  • Backup: Recommended to backup this file regularly

Example Schedule Format

{
  "my-app": {
    "start_time": "09:00:00",
    "stop_time": "18:00:00",
    "weekend_off": true,
    "additional_off_days": ["Friday"],
    "custom_off_dates": ["2024-12-25", "2024-01-01"],
    "enabled": true,
    "created_at": "2024-01-15T10:30:00"
  }
}

Environment Variables

The scheduler respects the same Databricks authentication as the main app:

  • DATABRICKS_HOST
  • DATABRICKS_TOKEN
  • Or use ~/.databrickscfg

Troubleshooting

Common Issues

  1. Authentication errors: Ensure Databricks credentials are properly configured
  2. Permission errors: Check file permissions and user access
  3. Network issues: Verify connectivity to Databricks workspace

Debug Mode

# Run with verbose output
python scheduler.py --daemon 2>&1 | tee scheduler.log

Health Check

# Check if scheduler is working
python -c "
import json
import os
from datetime import datetime

# Check if log file exists and has recent entries
log_file = 'schedule_execution.log'
if os.path.exists(log_file):
    with open(log_file, 'r') as f:
        lines = f.readlines()
    if lines:
        print(f'Last log entry: {lines[-1].strip()}')
    else:
        print('Log file is empty')
else:
    print('No log file found')
"

Best Practices

  1. Backup schedules: Regularly backup app_schedules.json
  2. Monitor logs: Set up log rotation and monitoring
  3. Test schedules: Use manual execution to test before enabling auto-scheduler
  4. Redundancy: Consider running multiple scheduler instances with coordination
  5. Alerting: Set up alerts for scheduler failures

Security Considerations

  1. Credentials: Store Databricks credentials securely
  2. File permissions: Restrict access to schedule and log files
  3. Network: Ensure secure connection to Databricks
  4. Audit: Regularly review schedule execution logs